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

refactor: Extract utility functions into utils library

These are reusable functions, and I want to avoid duplicate implementations
of them.
This commit is contained in:
Akira Komamura 2022-01-02 15:44:00 +09:00
parent 3f27b47528
commit 95e19914cb
2 changed files with 30 additions and 21 deletions

View file

@ -2,27 +2,11 @@
pred:
with builtins;
let
dropWhile = import ./dropWhile.nix;
splitListWith = import ./splitWith.nix;
isHeadline = s: substring 0 1 s == "*";
genericHeadlineRegexp = "(\\*+)[[:space:]].+";
getHeadlineLevel = headline:
stringLength (head (match genericHeadlineRegexp headline));
prependOptionalStars = n: rest:
if n == 0
then rest
else prependOptionalStars (n - 1) ("\\*?" + rest);
makeSubtreeEndRegexp = outlineLevel:
prependOptionalStars (outlineLevel - 1) "\\*[[:space:]].+";
dropTillSubtreeEnd = level:
dropWhile (s: !(isHeadline s && match (makeSubtreeEndRegexp level) s != null));
inherit (import ./utils.nix)
dropTillSubtreeEnd
getHeadlineLevel
splitListWith
isHeadline;
go_ = cut:
cut.before

25
nix/utils.nix Normal file
View file

@ -0,0 +1,25 @@
with builtins;
let
genericHeadlineRegexp = "(\\*+)[[:space:]].+";
prependOptionalStars = n: rest:
if n == 0
then rest
else prependOptionalStars (n - 1) ("\\*?" + rest);
in
rec {
dropWhile = import ./dropWhile.nix;
splitListWith = import ./splitWith.nix;
isHeadline = s: substring 0 1 s == "*";
getHeadlineLevel = headline:
stringLength (head (match genericHeadlineRegexp headline));
makeSubtreeEndRegexp = outlineLevel:
prependOptionalStars (outlineLevel - 1) "\\*[[:space:]].+";
dropTillSubtreeEnd = level:
dropWhile (s: !(isHeadline s && match (makeSubtreeEndRegexp level) s != null));
}