mirror of
https://github.com/emacs-twist/org-babel.git
synced 2025-03-16 21:38:14 +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:
|
pred:
|
||||||
with builtins;
|
with builtins;
|
||||||
let
|
let
|
||||||
dropWhile = import ./dropWhile.nix;
|
inherit (import ./utils.nix)
|
||||||
|
dropTillSubtreeEnd
|
||||||
splitListWith = import ./splitWith.nix;
|
getHeadlineLevel
|
||||||
|
splitListWith
|
||||||
isHeadline = s: substring 0 1 s == "*";
|
isHeadline;
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
go_ = cut:
|
go_ = cut:
|
||||||
cut.before
|
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…
Add table
Reference in a new issue