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:
parent
3f27b47528
commit
95e19914cb
2 changed files with 30 additions and 21 deletions
|
@ -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
25
nix/utils.nix
Normal 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));
|
||||
}
|
Loading…
Reference in a new issue