mirror of
https://github.com/emacs-twist/org-babel.git
synced 2025-03-16 05:18:14 +00:00
fix: Ignore blocks that have :tangle param other than yes
This commit is contained in:
parent
5d4fc523d5
commit
3adb6617b1
4 changed files with 102 additions and 2 deletions
35
nix/parseParamsString.nix
Normal file
35
nix/parseParamsString.nix
Normal 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)
|
|
@ -12,9 +12,27 @@ let
|
||||||
blockStartRegexp =
|
blockStartRegexp =
|
||||||
"[[:space:]]*\#\\+[Bb][Ee][Gg][Ii][Nn]_[Ss][Rr][Cc][[:space:]]+"
|
"[[:space:]]*\#\\+[Bb][Ee][Gg][Ii][Nn]_[Ss][Rr][Cc][[:space:]]+"
|
||||||
+ "(" + (concatStringsSep "|" languages) + ")"
|
+ "(" + (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;
|
splitListWith = import ./splitWith.nix;
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ let
|
||||||
matchOrgTag = import ../nix/matchOrgTag.nix;
|
matchOrgTag = import ../nix/matchOrgTag.nix;
|
||||||
matchOrgHeadline = import ../nix/matchOrgHeadline.nix;
|
matchOrgHeadline = import ../nix/matchOrgHeadline.nix;
|
||||||
dropUntil = import ../nix/dropUntil.nix;
|
dropUntil = import ../nix/dropUntil.nix;
|
||||||
|
tangleOrgBabel = import ../nix/tangleOrgBabel.nix;
|
||||||
lines = filter (s: isString s && s != "") (split "\n" (readFile ./test.org));
|
lines = filter (s: isString s && s != "") (split "\n" (readFile ./test.org));
|
||||||
in
|
in
|
||||||
pkgs.lib.runTests {
|
pkgs.lib.runTests {
|
||||||
|
@ -39,4 +40,19 @@ pkgs.lib.runTests {
|
||||||
expr = pkgs.lib.last (select (matchOrgTag "optional") lines);
|
expr = pkgs.lib.last (select (matchOrgTag "optional") lines);
|
||||||
expected = "/Zaijian/ means goodbye in Mandarin Chinese.";
|
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
31
test/testTangle.org
Normal 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
|
Loading…
Add table
Reference in a new issue