I'm in the process of writing a programming book.
..
The usual literate programming tools however leave room for improvement, and so I found Leo.
So I have a section of code annotated with
@language assembly_x86
which displays well in the code branch.
When I clone this section into the book branch it needs the sphinx annotation.. code:: nasm
or else it
isn't recognized for block layout (all lines of assembly are displayed on a single line in the browser) and syntax highlighting.
So far I have failed to get Leo to use the cloned section to display properly in both contexts.
Here is a tested prototype:
.. code:: nasm
LEOUNL:: <here paste leo unl path to desired node>
@first # -*- coding:utf-8 -*-
import re
import os
LEOUNLpat = re.compile(u'LEOUNL::([^#]*)#(.*)$', re.M)
def process_rst_files():
for p1 in c.allNodes_iter():
if p1.h.startswith(u'@rst '):
fname = p1.h[5:].strip()
g.es(u'processing %s'%fname)
do_one_file(p1, fname)
def do_one_file(p1, fname):
c.rstCommands.processTree(p1, None, toString=True, justOneFile=True)[0]
fname = os.path.join(c.getNodePath(p), fname + '.txt')
s = c.rstCommands.source.decode('utf-8')
s = putLeoCode(s).encode('utf-8')
s1 = getfile(fname)
if s1 != s:
putfile(fname, s)
g.es('changed')
else:
g.es('unchanged')
def getfile(fname):
try:
s = open(fname,'rb').read()
except IOError:
s = ""
return s
def putfile(fname, s):
out = open(fname, 'wb')
out.write(s)
out.close()
def putLeoCode(txt):
i = 0
res = []
while i < len(txt):
m = LEOUNLpat.search(txt, i)
if not m:
res.append(txt[i:])
break
else:
res.append(txt[i:m.start()])
ind = u'\n' + indentation(txt, m.start())
leofile = m.group(1)
unl = m.group(2)
found, depth, p1 = g.recursiveUNLSearch(unl.split(u"-->"), c)
if found:
kod = c.p.b.splitlines(False)
res.append(ind.join(kod))
else:
print (u"Leofile:"+leofile)
print (u"unl:[%s]"%unl)
print (u'not found')
raise ValueError, unl
i = m.end()
return u''.join(res)
def indentation(txt, i):
j = i
while j > 0 and txt[j-1] == ' ':
j -= 1
return ' '*(i-j)
process_rst_files()
...your solution of copying nodes isn't quite as elegant as cloning.
So I kept fooling around a bit and found out that when using the @clean node to create the rst files and then invoking make by hand I get very satisfactory results.
One possible solution would be to use Leo-UNL instead of clones. For example in your @rst file you put somewhere following:
.. code:: nasm
LEOUNL:: <here paste leo unl path to desired node>
Then run the following script (could be turned in to button) to generate your rst files: