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

fix: Ignore blocks that have :tangle param other than yes

This commit is contained in:
Akira Komamura 2022-01-03 18:24:30 +09:00
parent 5d4fc523d5
commit 3adb6617b1
4 changed files with 102 additions and 2 deletions

35
nix/parseParamsString.nix Normal file
View file

@ -0,0 +1,35 @@
with builtins;
let
listToAttrs = xs:
if length xs == 0
then { }
else if length xs == 1
then throw "parseParamsString: Found an odd number of items: ${head xs}"
else { ${head xs} = elemAt xs 1; } // listToAttrs (tail (tail xs));
stripPat = "[[:space:]]+(.*)";
stripLeft = string:
if match stripPat string != null
then head (match stripPat string)
else string;
pat1 = "\"([^\"]+)\"(.*)";
pat2 = "([^\"][^[:space:]]*)(.*)";
go = m: [ (elemAt m 0) ] ++ parse (elemAt m 1);
parse' = string:
if string == ""
then [ ]
else if match pat1 string != null
then go (match pat1 string)
else if match pat2 string != null
then go (match pat2 string)
else throw "Match nothing: ${string}";
parse = string: parse' (stripLeft string);
in
string:
listToAttrs (parse string)

View file

@ -12,9 +12,27 @@ let
blockStartRegexp =
"[[:space:]]*\#\\+[Bb][Ee][Gg][Ii][Nn]_[Ss][Rr][Cc][[:space:]]+"
+ "(" + (concatStringsSep "|" languages) + ")"
+ "([[:space:]].*)?";
+ "(([[:space:]].*)?)";
isBlockStart = line: match blockStartRegexp line != null;
parseParamsString = import ./parseParamsString.nix;
parseParamsString' = s:
if s == null
then { }
else parseParamsString s;
checkBlockParams = attrs:
foldl' (acc: value: acc && value) true
(attrValues
(mapAttrs (name: value:
if name == ":tangle"
then value == "yes"
else true
) attrs));
isBlockStart = line:
(match blockStartRegexp line != null)
&& checkBlockParams (parseParamsString' (elemAt (match blockStartRegexp line) 2));
splitListWith = import ./splitWith.nix;

View file

@ -7,6 +7,7 @@ let
matchOrgTag = import ../nix/matchOrgTag.nix;
matchOrgHeadline = import ../nix/matchOrgHeadline.nix;
dropUntil = import ../nix/dropUntil.nix;
tangleOrgBabel = import ../nix/tangleOrgBabel.nix;
lines = filter (s: isString s && s != "") (split "\n" (readFile ./test.org));
in
pkgs.lib.runTests {
@ -39,4 +40,19 @@ pkgs.lib.runTests {
expr = pkgs.lib.last (select (matchOrgTag "optional") lines);
expected = "/Zaijian/ means goodbye in Mandarin Chinese.";
};
testTangleOrgBabel = {
expr = pkgs.lib.pipe (tangleOrgBabel { } (readFile ./testTangle.org)) [
(split "\n")
(filter isString)
];
expected = [
"Default"
"Alternative language name"
"Upper case"
":tangle yes"
"Extra spaces around params"
];
};
}

31
test/testTangle.org Normal file
View file

@ -0,0 +1,31 @@
#+begin_src
No language
#+end_src
#+begin_src emacs-lisp
Default
#+end_src
#+begin_src elisp
Alternative language name
#+end_src elisp
#+BEGIN_SRC emacs-lisp
Upper case
#+END_SRC
#+BEGIN_SRC shell
Different language
#+END_SRC
#+begin_src emacs-lisp :tangle yes
:tangle yes
#+end_src
#+begin_src emacs-lisp :tangle yes
Extra spaces around params
#+end_src
#+begin_src emacs-lisp :tangle no
:tangle no
#+end_src