[cig-commits] r15798 - doc/geodynamics.org/benchmarks/trunk

luis at geodynamics.org luis at geodynamics.org
Sun Oct 11 18:42:58 PDT 2009


Author: luis
Date: 2009-10-11 18:42:58 -0700 (Sun, 11 Oct 2009)
New Revision: 15798

Modified:
   doc/geodynamics.org/benchmarks/trunk/generate.py
Log:
Made sure that LaTeX equations are rendered in my local html files

In order to insert a custom <script> tag, we had to deconstruct
the docutils.core.publish_file() method right up to the point
where the HTML content is translated from an internal document
representation.

Modified: doc/geodynamics.org/benchmarks/trunk/generate.py
===================================================================
--- doc/geodynamics.org/benchmarks/trunk/generate.py	2009-10-11 20:08:54 UTC (rev 15797)
+++ doc/geodynamics.org/benchmarks/trunk/generate.py	2009-10-12 01:42:58 UTC (rev 15798)
@@ -9,8 +9,10 @@
 # http://docs.python.org/library/os.path.html#os.path.getmtime
 #
 
-import os
+import os, time
 import docutils.core
+import docutils.io
+import docutils.languages
 
 
 # -----------------------------------------------------------------------------
@@ -56,6 +58,81 @@
 
 # -----------------------------------------------------------------------------
 
+def publish_file(source_path,
+                 destination_path,
+                 settings_overrides=None):
+    """
+    Based on `docutils.core.publish_file`
+    """
+
+    reader_name = 'standalone'
+    parser_name = 'restructuredtext'
+    writer_name = 'html'
+
+    source_class = docutils.io.FileInput
+    destination_class = docutils.io.FileOutput
+
+    pub = docutils.core.Publisher(
+        source_class=source_class,
+        destination_class=destination_class
+    )
+
+    pub.set_components(reader_name, parser_name, writer_name)
+
+    pub.process_programmatic_settings(
+        settings_spec=None,
+        settings_overrides=settings_overrides,
+        config_section=None,
+    )
+
+    pub.set_source(source_path=source_path)
+
+    pub.set_destination(destination_path=destination_path)
+
+    # The following was deconstructed from the method `pub.publish()`:
+    #  (1) read input file (rst format)
+    #  (2) apply transforms (using parser)
+    #  (3) write output file (html format)
+    #
+    # We do this because there's no way to modify the information in <head>,
+    # so we must parse the rst file, insert our content, and only then
+    # create the html file.
+
+    # Step (1) is straightforward enough. We read the rst file.
+    # Here, 'pub.document` is a `docutils.node.document`.
+    pub.document = pub.reader.read(pub.source, pub.parser, pub.settings)
+
+    # Step (2) is where the parsing happens.
+    pub.apply_transforms()
+
+    # Step (3) gets tricky, since we must translate the parser representation
+    # to html format before inserting our content. Everything here was
+    # deconstructed from `pub.writer.write(pub.document, pub.destination)`.
+    # In this case, `pub.writer` is a `docutils.writers.html4css1.Writer`.
+    pub.writer.document = pub.document
+    pub.writer.language = docutils.languages.get_language(pub.document.settings.language_code)
+    pub.writer.destination = pub.destination
+
+    # Next, we need to deconstruct `pub.writer.translate()` so we can insert
+    # our <script> tag, used for parsing TeX [;EXPRESSIONS;].
+    visitor = pub.writer.visitor = pub.writer.translator_class(pub.writer.document)
+    pub.writer.document.walkabout(visitor)
+    for attr in pub.writer.visitor_attributes:
+        setattr(pub.writer, attr, getattr(visitor, attr))
+    pub.writer.head += """<script type="text/javascript" src="../js/textheworld6.user.js"></script>"""
+    pub.writer.output = pub.writer.apply_template()
+
+    # ...and with this line, we are done with Step (3).
+    output = pub.writer.destination.write(pub.writer.output)
+
+    # This line completes our deconstruction of `pub.publish()`
+    # Although, it's technically not necessary for our purposes.
+    pub.writer.assemble_parts()
+
+    # We're done! Check contents of <head> to see if our extra <script> tag was inserted
+    #print pub.writer.parts['head']
+
+
 def regenerate(filename):
     """
     Given an .html filename, regenerate it from its .rst source.
@@ -69,14 +146,14 @@
         stylesheet_path='./css/voidspace.css',
     )
 
-    docutils.core.publish_file(
+    publish_file(
         source_path=rst,
         destination_path=filename,
-        writer_name='html',
         settings_overrides=overrides,
     )
 
-    print "Regenerated %s" % filename
+    timestamp = time.strftime("%H:%M:%S")
+    print "%s Regenerated %s" % (timestamp, filename)
 
 
 def regenerate_all_html_files():



More information about the CIG-COMMITS mailing list