Permalink
Browse files

Teach python directive to include external files.

No unit tests yet, this is purely temporary, I hope to move to
pygments via ulif.rest's "sourcecode" directive.
  • Loading branch information...
1 parent 036cb34 commit 3863178d44a77b321cfe83fec75ba069cc5244ec @tv42 committed Jun 30, 2008
Showing with 34 additions and 4 deletions.
  1. +34 −4 atomat/rst2entry.py
View
@@ -1,4 +1,6 @@
+import os
from docutils.core import publish_string
+from docutils import utils
from xml.dom import minidom
import xml.dom
import email.Utils
@@ -11,13 +13,41 @@
def python(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
- inp = StringIO('\n'.join(content).encode('utf-8'))
+ filename = options.get('filename', None)
+ if filename is None:
+ inp = StringIO('\n'.join(content).encode('utf-8'))
+ else:
+ source = state_machine.input_lines.source(
+ lineno - state_machine.input_offset - 1)
+ source_dir = os.path.dirname(os.path.abspath(source))
+ filename = os.path.normpath(os.path.join(source_dir, filename))
+ filename = utils.relative_path(None, filename)
+ state.document.settings.record_dependencies.add(filename)
+ inp = file(filename)
outp = StringIO()
- outp.write('<div class="py-listing">')
htmlizer.filter(inp, outp, writer=htmlizer.SmallerHTMLWriter)
- outp.write('</div>')
html = outp.getvalue()
- return [nodes.raw('', html, format='html')]
+
+ if arguments:
+ title_text = arguments[0]
+ text_nodes, messages = state.inline_text(title_text, lineno)
+ title = nodes.caption('', '# ', *text_nodes)
+ else:
+ messages = []
+ title = None
+
+ fig = nodes.figure('')
+ fig['classes'].append('py-listing')
+ if title is not None:
+ fig += title
+
+ fig += nodes.raw('', html, format='html')
+
+ return [fig] + messages
+
+python.arguments = (0, 1, True)
+python.options = dict(filename=rst.directives.path,
+ )
python.content = 1
rst.directives.register_directive('python', python)

0 comments on commit 3863178

Please sign in to comment.