1
0
Fork 0
mirror of https://github.com/emacs-twist/org-babel.git synced 2024-12-14 11:07:30 +00:00

feat: Add selectHeadlines function

This commit is contained in:
Akira Komamura 2022-01-02 16:22:04 +09:00
parent 95e19914cb
commit 629afa4408
3 changed files with 49 additions and 1 deletions

View file

@ -1,6 +1,7 @@
with builtins;
let
excludeOrgSubtreesOnHeadlines = import ./excludeOrgSubtreesOnHeadlines.nix;
selectHeadlines = import ./selectHeadlines.nix;
matchOrgTag = import ./matchOrgTag.nix;
matchOrgHeadline = import ./matchOrgHeadline.nix;
@ -12,6 +13,7 @@ in
{
# Newer concise APIs
excludeHeadlines = excludeOrgSubtreesOnHeadlines;
inherit selectHeadlines;
tag = matchOrgTag;
headlineText = matchOrgHeadline;
allP = predicates: x: builtins.all (p: p x) predicates;

35
nix/selectHeadlines.nix Normal file
View file

@ -0,0 +1,35 @@
# Select headlines matching a tag/headline
pred:
with builtins;
let
inherit (import ./utils.nix)
makeSubtreeEndRegexp
getHeadlineLevel
splitListWith
dropWhile
isHeadline;
makeEndPred = headline:
s: isHeadline s
&& match (makeSubtreeEndRegexp (getHeadlineLevel headline)) s != null;
go0 = lines:
if length lines == 0
then [ ]
else go1 (head lines) (splitListWith (makeEndPred (head lines)) (tail lines));
go1 = initial: cut:
[ initial ]
++
cut.before
++
(if cut.sep == null
then [ ]
else go ([ cut.sep ] ++ cut.after));
go = lines:
if length lines == 0
then [ ]
else go0 (dropWhile (s: !(isHeadline s && pred s)) lines);
in
go

View file

@ -3,10 +3,11 @@ with builtins;
let
pkgs = import <nixpkgs> { };
exclude = import ../nix/excludeOrgSubtreesOnHeadlines.nix;
select = import ../nix/selectHeadlines.nix;
matchOrgTag = import ../nix/matchOrgTag.nix;
matchOrgHeadline = import ../nix/matchOrgHeadline.nix;
dropUntil = import ../nix/dropUntil.nix;
lines = filter isString (split "\n" (readFile ./test.org));
lines = filter (s: isString s && s != "") (split "\n" (readFile ./test.org));
in
pkgs.lib.runTests {
testTagPredicate = {
@ -28,4 +29,14 @@ pkgs.lib.runTests {
expr = pkgs.lib.last (exclude (matchOrgTag "optional") lines);
expected = "It was a good day!";
};
testSelect1 = {
expr = pkgs.lib.last (select (matchOrgTag "irregular") lines);
expected = "Why did he come to the office in the first place?";
};
testSelect2 = {
expr = pkgs.lib.last (select (matchOrgTag "optional") lines);
expected = "/Zaijian/ means goodbye in Mandarin Chinese.";
};
}