Commit 034db044 authored by Mikaël Salson's avatar Mikaël Salson

org-babel-tangle: Provide a Python script to extract tangles from org-mode.

It removes the dependency we had on emacs org-babel mode to launch the tests.
Now examples in .org files are automatically extracted using this script
parent 5457c5ea
......@@ -4,4 +4,4 @@
emacs -batch $^ -f "org-html-export-to-html"
analysis-example%: format-analysis.org
emacs -batch $^ -f "org-babel-tangle"
python ../tools/org-babel-tangle.py --all $^
import argparse
import re
import os
import sys
parser = argparse.ArgumentParser(description = 'Simulate org-babel-tangle emacs function')
parser.add_argument('--all', '-a', action='store_true', help='Extract all the tangle org babel code')
parser.add_argument('--extract', '-e', default=[], action='append', help='Extract the org-babel code whose :tangle value is provided by this option')
parser.add_argument('--directory', '-d', default='.', help = 'Directory where the output files are stored (default is %(default)s). The directory must exist.')
parser.add_argument('--test', '-t', action='store_true', help='Launch self-tests.')
parser.add_argument('file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='.org file')
args = parser.parse_args()
def export_tangle(filename, content):
file = open(filename, "w")
file.write(content)
file.close()
def extract_tangle(content, names=None):
r'''
Extract a tangle from an .org file
>>> extract_tangle('\n#+BEGIN_SRC sh :tangle ex1 :var ex=2\necho $ex\n#+END_SRC')
[{'content': 'echo $ex', 'filename': 'ex1'}]
>>> extract_tangle('\n#+BEGIN_SRC sh :tangle ex2 :var ex=2\necho $ex\n#+END_SRC', ['ex2'])
[{'content': 'echo $ex', 'filename': 'ex2'}]
'''
if names == None:
names = ['[^ \t\n]+']
tangles = []
for name in names:
regex = '\n\#\+BEGIN_SRC[^\n]*:tangle ('+name+')[^\n]*\n(.*?)\n#\+END_SRC'
compiled_regex = re.compile(regex, re.DOTALL)
results = compiled_regex.findall(content)
for result in results:
current_tangle = {'filename': result[0],
'content': result[1]}
tangles.append(current_tangle)
return tangles
def extract_tangle_and_output(content, dir, names=None):
tangles = extract_tangle(content, names)
for result in tangles:
export_tangle(dir + os.sep + result['filename'],
result['content'])
if args.test:
import doctest
doctest.testmod(verbose = True)
sys.exit(0)
content = args.file.read()
if args.all:
extract_tangle_and_output(content, args.directory)
else:
extract_tangle_and_output(content, args.directory, args.extract)
......@@ -14,6 +14,7 @@ doctests:
@echo "*** Launching python tests..."
python -m doctest -v ../fuse.py
python -m doctest -v ../utils.py
python ../org-babel-tangle.py --test
@echo "*** All python tests passed"
force:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment