[Python-checkins] r66965 - in doctools/trunk: CHANGES sphinx/addnodes.py sphinx/builder.py sphinx/environment.py sphinx/htmlwriter.py sphinx/templates/layout.html

georg.brandl python-checkins at python.org
Sat Oct 18 11:29:49 CEST 2008


Author: georg.brandl
Date: Sat Oct 18 11:29:49 2008
New Revision: 66965
Log:
Support <meta> tags via meta directive.
Modified:
 doctools/trunk/CHANGES
 doctools/trunk/sphinx/addnodes.py
 doctools/trunk/sphinx/builder.py
 doctools/trunk/sphinx/environment.py
 doctools/trunk/sphinx/htmlwriter.py
 doctools/trunk/sphinx/templates/layout.html
Modified: doctools/trunk/CHANGES
==============================================================================
--- doctools/trunk/CHANGES	(original)
+++ doctools/trunk/CHANGES	Sat Oct 18 11:29:49 2008
@@ -15,6 +15,9 @@
 - The JavaScript search now searches for objects before searching in
 the full text.
 
+ - HTML ``<meta>`` tags via the docutils ``meta`` directive are now
+ supported.
+
 - ``SerializingHTMLBuilder`` was added as new abstract builder that
 can be subclassed to serialize build HTML in a specific format. The
 ``PickleHTMLBuilder`` is a concrete subclass of it that uses pickle
Modified: doctools/trunk/sphinx/addnodes.py
==============================================================================
--- doctools/trunk/sphinx/addnodes.py	(original)
+++ doctools/trunk/sphinx/addnodes.py	Sat Oct 18 11:29:49 2008
@@ -85,10 +85,13 @@
 # tabular column specification, used for the LaTeX writer
 class tabular_col_spec(nodes.Element): pass
 
+# meta directive -- same as docutils' standard meta node, but pickleable
+class meta(nodes.Special, nodes.PreBibliographic, nodes.Element): pass
+
 # make them known to docutils. this is needed, because the HTML writer
 # will choke at some point if these are not added
 nodes._add_node_class_names("""index desc desc_content desc_signature desc_type
 desc_addname desc_name desc_parameterlist desc_parameter desc_optional
 centered versionmodified seealso productionlist production toctree
 pending_xref compact_paragraph highlightlang literal_emphasis
- glossary acks module start_of_file tabular_col_spec""".split())
+ glossary acks module start_of_file tabular_col_spec meta""".split())
Modified: doctools/trunk/sphinx/builder.py
==============================================================================
--- doctools/trunk/sphinx/builder.py	(original)
+++ doctools/trunk/sphinx/builder.py	Sat Oct 18 11:29:49 2008
@@ -450,7 +450,7 @@
 favicon = favicon,
 )
 
- def get_doc_context(self, docname, body):
+ def get_doc_context(self, docname, body, metatags):
 """Collect items for the template context of a page."""
 # find out relations
 prev = next = None
@@ -502,6 +502,7 @@
 title = title,
 meta = meta,
 body = body,
+ metatags = metatags,
 rellinks = rellinks,
 sourcename = sourcename,
 toc = self.render_partial(self.env.get_toc_for(docname))['fragment'],
@@ -518,8 +519,9 @@
 self.docwriter.write(doctree, destination)
 self.docwriter.assemble_parts()
 body = self.docwriter.parts['fragment']
+ metatags = self.docwriter.clean_meta
 
- ctx = self.get_doc_context(docname, body)
+ ctx = self.get_doc_context(docname, body, metatags)
 self.index_page(docname, doctree, ctx.get('title', ''))
 self.handle_page(docname, ctx, event_arg=doctree)
 
Modified: doctools/trunk/sphinx/environment.py
==============================================================================
--- doctools/trunk/sphinx/environment.py	(original)
+++ doctools/trunk/sphinx/environment.py	Sat Oct 18 11:29:49 2008
@@ -30,12 +30,14 @@
 md5 = md5.new
 
 from docutils import nodes
-from docutils.io import FileInput
-from docutils.core import publish_doctree
+from docutils.io import FileInput, NullOutput
+from docutils.core import Publisher
 from docutils.utils import Reporter
 from docutils.readers import standalone
 from docutils.parsers.rst import roles
 from docutils.parsers.rst.languages import en as english
+from docutils.parsers.rst.directives.html import MetaBody
+from docutils.writers import UnfilteredWriter
 from docutils.transforms import Transform
 from docutils.transforms.parts import ContentsFilter
 
@@ -142,9 +144,17 @@
 Add our own transforms.
 """
 transforms = [DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks]
+
 def get_transforms(self):
- tf = standalone.Reader.get_transforms(self)
- return tf + self.transforms
+ return standalone.Reader.get_transforms(self) + self.transforms
+
+
+class SphinxDummyWriter(UnfilteredWriter):
+ supported = ('html',) # needed to keep "meta" nodes
+
+ def translate(self):
+ pass
+
 
 
 class SphinxContentsFilter(ContentsFilter):
@@ -486,6 +496,9 @@
 self.warn(docname, 'default role %s not found' %
 self.config.default_role)
 
+ self.docname = docname
+ self.settings['input_encoding'] = self.config.source_encoding
+
 class SphinxSourceClass(FileInput):
 def read(self):
 data = FileInput.read(self)
@@ -495,12 +508,18 @@
 data = arg[0]
 return data
 
- self.docname = docname
- self.settings['input_encoding'] = self.config.source_encoding
+ # publish manually
+ pub = Publisher(reader=SphinxStandaloneReader(),
+ writer=SphinxDummyWriter(),
+ source_class=SphinxSourceClass,
+ destination_class=NullOutput)
+ pub.set_components(None, 'restructuredtext', None)
+ pub.process_programmatic_settings(None, self.settings, None)
+ pub.set_source(None, src_path)
+ pub.set_destination(None, None)
 try:
- doctree = publish_doctree(None, src_path, SphinxSourceClass,
- settings_overrides=self.settings,
- reader=SphinxStandaloneReader())
+ pub.publish()
+ doctree = pub.document
 except UnicodeError, err:
 from sphinx.application import SphinxError
 raise SphinxError(err.message)
@@ -525,6 +544,9 @@
 doctree.settings.warning_stream = None
 doctree.settings.env = None
 doctree.settings.record_dependencies = None
+ for metanode in doctree.traverse(MetaBody.meta):
+ # docutils' meta nodes aren't picklable because the class is nested
+ metanode.__class__ = addnodes.meta
 
 # cleanup
 self.docname = None
Modified: doctools/trunk/sphinx/htmlwriter.py
==============================================================================
--- doctools/trunk/sphinx/htmlwriter.py	(original)
+++ doctools/trunk/sphinx/htmlwriter.py	Sat Oct 18 11:29:49 2008
@@ -37,6 +37,7 @@
 'footer', 'html_prolog', 'html_head', 'html_title',
 'html_subtitle', 'html_body', ):
 setattr(self, attr, getattr(visitor, attr, None))
+ self.clean_meta = ''.join(visitor.meta[2:])
 
 
 class HTMLTranslator(BaseTranslator):
Modified: doctools/trunk/sphinx/templates/layout.html
==============================================================================
--- doctools/trunk/sphinx/templates/layout.html	(original)
+++ doctools/trunk/sphinx/templates/layout.html	Sat Oct 18 11:29:49 2008
@@ -87,6 +87,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ {{ metatags }}
 {%- if builder != 'htmlhelp' %}
 {%- set titlesuffix = " &mdash; " + docstitle %}
 {%- endif %}


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /