diff -r 000000000000 -r b97547f5f1fa common/html4zope.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/html4zope.py Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,153 @@ +# Author: David Goodger +# Contact: goodger@users.sourceforge.net +# Revision: $Revision: 1.2 $ +# Date: $Date: 2005-07-04 16:36:50 $ +# Copyright: This module has been placed in the public domain. + +""" +Simple HyperText Markup Language document tree Writer. + +The output conforms to the HTML 4.01 Transitional DTD and to the Extensible +HTML version 1.0 Transitional DTD (*almost* strict). The output contains a +minimum of formatting information. A cascading style sheet ("default.css" by +default) is required for proper viewing with a modern graphical browser. + +http://cvs.zope.org/Zope/lib/python/docutils/writers/Attic/html4zope.py?rev=1.1.2.2&only_with_tag=ajung-restructuredtext-integration-branch&content-type=text/vnd.viewcvs-markup +""" + +__docformat__ = 'reStructuredText' + +from logilab.mtconverter import html_escape + +from docutils import nodes +from docutils.writers.html4css1 import Writer as CSS1Writer +from docutils.writers.html4css1 import HTMLTranslator as CSS1HTMLTranslator +import os + +default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3)) + +class Writer(CSS1Writer): + """css writer using our html translator""" + def __init__(self, base_url): + CSS1Writer.__init__(self) + self.translator_class = URLBinder(base_url, HTMLTranslator) + + def apply_template(self): + """overriding this is necessary with docutils >= 0.5""" + return self.visitor.astext() + +class URLBinder: + def __init__(self, url, klass): + self.base_url = url + self.translator_class = HTMLTranslator + + def __call__(self, document): + translator = self.translator_class(document) + translator.base_url = self.base_url + return translator + +class HTMLTranslator(CSS1HTMLTranslator): + """ReST tree to html translator""" + + def astext(self): + """return the extracted html""" + return ''.join(self.body) + + def visit_title(self, node): + """Only 6 section levels are supported by HTML.""" + if isinstance(node.parent, nodes.topic): + self.body.append( + self.starttag(node, 'p', '', CLASS='topic-title')) + if node.parent.hasattr('id'): + self.body.append( + self.starttag({}, 'a', '', name=node.parent['id'])) + self.context.append('
\n') + else: + self.context.append('\n') + elif self.section_level == 0: + # document title + self.head.append('