diff -r 02b52bf9f5f8 -r 0865e1e90674 ext/rest.py --- a/ext/rest.py Wed Mar 24 10:23:31 2010 +0100 +++ b/ext/rest.py Wed Apr 28 11:54:13 2010 +0200 @@ -1,3 +1,20 @@ +# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of CubicWeb. +# +# CubicWeb is free software: you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 2.1 of the License, or (at your option) +# any later version. +# +# logilab-common is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with CubicWeb. If not, see . """rest publishing functions contains some functions and setup of docutils for cubicweb. Provides the @@ -12,10 +29,6 @@ * `sourcecode` (if pygments is installed), source code colorization -:organization: Logilab -:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. -:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr -:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses """ __docformat__ = "restructuredtext en" @@ -25,7 +38,7 @@ from os.path import join from docutils import statemachine, nodes, utils, io -from docutils.core import publish_string +from docutils.core import Publisher from docutils.parsers.rst import Parser, states, directives from docutils.parsers.rst.roles import register_canonical_role, set_classes @@ -92,14 +105,15 @@ in `docutils.parsers.rst.directives.misc` """ context = state.document.settings.context + cw = context._cw source = state_machine.input_lines.source( lineno - state_machine.input_offset - 1) #source_dir = os.path.dirname(os.path.abspath(source)) fid = arguments[0] - for lang in chain((context._cw.lang, context.vreg.property_value('ui.language')), - context.config.available_languages()): + for lang in chain((cw.lang, cw.vreg.property_value('ui.language')), + cw.vreg.config.available_languages()): rid = '%s_%s.rst' % (fid, lang) - resourcedir = context.config.locate_doc_file(rid) + resourcedir = cw.vreg.config.locate_doc_file(rid) if resourcedir: break else: @@ -196,6 +210,15 @@ self.finish_parse() +# XXX docutils keep a ref on context, can't find a correct way to remove it +class CWReSTPublisher(Publisher): + def __init__(self, context, settings, **kwargs): + Publisher.__init__(self, **kwargs) + self.set_components('standalone', 'restructuredtext', 'pseudoxml') + self.process_programmatic_settings(None, settings, None) + self.settings.context = context + + def rest_publish(context, data): """publish a string formatted as ReStructured Text to HTML @@ -218,7 +241,7 @@ # remove unprintable characters unauthorized in xml data = data.translate(ESC_CAR_TABLE) settings = {'input_encoding': encoding, 'output_encoding': 'unicode', - 'warning_stream': StringIO(), 'context': context, + 'warning_stream': StringIO(), # dunno what's the max, severe is 4, and we never want a crash # (though try/except may be a better option...) 'halt_level': 10, @@ -233,9 +256,17 @@ else: base_url = None try: - return publish_string(writer=Writer(base_url=base_url), - parser=CubicWebReSTParser(), source=data, - settings_overrides=settings) + pub = CWReSTPublisher(context, settings, + parser=CubicWebReSTParser(), + writer=Writer(base_url=base_url), + source_class=io.StringInput, + destination_class=io.StringOutput) + pub.set_source(data) + pub.set_destination() + res = pub.publish(enable_exit_status=None) + # necessary for proper garbage collection, else a ref is kept somewhere in docutils... + del pub.settings.context + return res except Exception: LOGGER.exception('error while publishing ReST text') if not isinstance(data, unicode):