author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 15 Oct 2010 11:33:06 +0200 | |
changeset 6500 | e288df2e2f18 |
parent 6275 | 20f30e01ae59 |
child 6938 | 6c1a960735f5 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4771
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""rest publishing functions |
19 |
||
996 | 20 |
contains some functions and setup of docutils for cubicweb. Provides the |
21 |
following ReST directives: |
|
22 |
||
23 |
* `eid`, create link to entity in the repository by their eid |
|
24 |
||
25 |
* `card`, create link to card entity in the repository by their wikiid |
|
26 |
(proposing to create it when the refered card doesn't exist yet) |
|
27 |
||
28 |
* `winclude`, reference to a web documentation file (in wdoc/ directories) |
|
29 |
||
30 |
* `sourcecode` (if pygments is installed), source code colorization |
|
0 | 31 |
|
32 |
""" |
|
33 |
__docformat__ = "restructuredtext en" |
|
34 |
||
35 |
from cStringIO import StringIO |
|
36 |
from itertools import chain |
|
37 |
from logging import getLogger |
|
38 |
from os.path import join |
|
39 |
||
40 |
from docutils import statemachine, nodes, utils, io |
|
4771
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
41 |
from docutils.core import Publisher |
0 | 42 |
from docutils.parsers.rst import Parser, states, directives |
43 |
from docutils.parsers.rst.roles import register_canonical_role, set_classes |
|
44 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2311
diff
changeset
|
45 |
from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape |
0 | 46 |
|
2467
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
47 |
from cubicweb import UnknownEid |
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
48 |
from cubicweb.ext.html4zope import Writer |
0 | 49 |
|
50 |
# We provide our own parser as an attempt to get rid of |
|
51 |
# state machine reinstanciation |
|
52 |
||
53 |
import re |
|
54 |
# compile states.Body patterns |
|
55 |
for k, v in states.Body.patterns.items(): |
|
56 |
if isinstance(v, str): |
|
57 |
states.Body.patterns[k] = re.compile(v) |
|
58 |
||
59 |
# register ReStructured Text mimetype / extensions |
|
60 |
import mimetypes |
|
61 |
mimetypes.add_type('text/rest', '.rest') |
|
62 |
mimetypes.add_type('text/rest', '.rst') |
|
63 |
||
64 |
||
65 |
LOGGER = getLogger('cubicweb.rest') |
|
66 |
||
67 |
def eid_reference_role(role, rawtext, text, lineno, inliner, |
|
68 |
options={}, content=[]): |
|
69 |
try: |
|
70 |
try: |
|
71 |
eid_num, rest = text.split(u':', 1) |
|
72 |
except: |
|
73 |
eid_num, rest = text, '#'+text |
|
74 |
eid_num = int(eid_num) |
|
75 |
if eid_num < 0: |
|
76 |
raise ValueError |
|
77 |
except ValueError: |
|
78 |
msg = inliner.reporter.error( |
|
79 |
'EID number must be a positive number; "%s" is invalid.' |
|
80 |
% text, line=lineno) |
|
81 |
prb = inliner.problematic(rawtext, rawtext, msg) |
|
82 |
return [prb], [msg] |
|
83 |
# Base URL mainly used by inliner.pep_reference; so this is correct: |
|
84 |
context = inliner.document.settings.context |
|
2467
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
85 |
try: |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
86 |
refedentity = context._cw.entity_from_eid(eid_num) |
2467
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
87 |
except UnknownEid: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
88 |
ref = '#' |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
89 |
rest += u' ' + context._cw._('(UNEXISTANT EID)') |
2467
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
90 |
else: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
91 |
ref = refedentity.absolute_url() |
0 | 92 |
set_classes(options) |
93 |
return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref, |
|
94 |
**options)], [] |
|
95 |
||
96 |
||
97 |
def winclude_directive(name, arguments, options, content, lineno, |
|
98 |
content_offset, block_text, state, state_machine): |
|
99 |
"""Include a reST file as part of the content of this reST file. |
|
100 |
||
101 |
same as standard include directive but using config.locate_doc_resource to |
|
102 |
get actual file to include. |
|
103 |
||
104 |
Most part of this implementation is copied from `include` directive defined |
|
105 |
in `docutils.parsers.rst.directives.misc` |
|
106 |
""" |
|
107 |
context = state.document.settings.context |
|
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
108 |
cw = context._cw |
0 | 109 |
source = state_machine.input_lines.source( |
110 |
lineno - state_machine.input_offset - 1) |
|
111 |
#source_dir = os.path.dirname(os.path.abspath(source)) |
|
112 |
fid = arguments[0] |
|
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
113 |
for lang in chain((cw.lang, cw.vreg.property_value('ui.language')), |
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
114 |
cw.vreg.config.available_languages()): |
0 | 115 |
rid = '%s_%s.rst' % (fid, lang) |
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
116 |
resourcedir = cw.vreg.config.locate_doc_file(rid) |
0 | 117 |
if resourcedir: |
118 |
break |
|
119 |
else: |
|
120 |
severe = state_machine.reporter.severe( |
|
121 |
'Problems with "%s" directive path:\nno resource matching %s.' |
|
122 |
% (name, fid), |
|
123 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
124 |
return [severe] |
|
125 |
path = join(resourcedir, rid) |
|
126 |
encoding = options.get('encoding', state.document.settings.input_encoding) |
|
127 |
try: |
|
128 |
state.document.settings.record_dependencies.add(path) |
|
129 |
include_file = io.FileInput( |
|
130 |
source_path=path, encoding=encoding, |
|
131 |
error_handler=state.document.settings.input_encoding_error_handler, |
|
132 |
handle_io_errors=None) |
|
133 |
except IOError, error: |
|
134 |
severe = state_machine.reporter.severe( |
|
135 |
'Problems with "%s" directive path:\n%s: %s.' |
|
136 |
% (name, error.__class__.__name__, error), |
|
137 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
138 |
return [severe] |
|
139 |
try: |
|
140 |
include_text = include_file.read() |
|
141 |
except UnicodeError, error: |
|
142 |
severe = state_machine.reporter.severe( |
|
143 |
'Problem with "%s" directive:\n%s: %s' |
|
144 |
% (name, error.__class__.__name__, error), |
|
145 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
146 |
return [severe] |
|
147 |
if options.has_key('literal'): |
|
148 |
literal_block = nodes.literal_block(include_text, include_text, |
|
149 |
source=path) |
|
150 |
literal_block.line = 1 |
|
151 |
return literal_block |
|
152 |
else: |
|
153 |
include_lines = statemachine.string2lines(include_text, |
|
154 |
convert_whitespace=1) |
|
155 |
state_machine.insert_input(include_lines, path) |
|
156 |
return [] |
|
157 |
||
158 |
winclude_directive.arguments = (1, 0, 1) |
|
159 |
winclude_directive.options = {'literal': directives.flag, |
|
160 |
'encoding': directives.encoding} |
|
161 |
||
996 | 162 |
try: |
163 |
from pygments import highlight |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4483
diff
changeset
|
164 |
from pygments.lexers import get_lexer_by_name |
996 | 165 |
from pygments.formatters import HtmlFormatter |
166 |
except ImportError: |
|
4118
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
167 |
pygments_directive = None |
996 | 168 |
else: |
169 |
_PYGMENTS_FORMATTER = HtmlFormatter() |
|
170 |
||
171 |
def pygments_directive(name, arguments, options, content, lineno, |
|
172 |
content_offset, block_text, state, state_machine): |
|
173 |
try: |
|
174 |
lexer = get_lexer_by_name(arguments[0]) |
|
175 |
except ValueError: |
|
176 |
# no lexer found |
|
177 |
lexer = get_lexer_by_name('text') |
|
178 |
parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER) |
|
4483
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
179 |
# don't fail if no context set on the sourcecode directive |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
180 |
try: |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
181 |
context = state.document.settings.context |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
182 |
context._cw.add_css('pygments.css') |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
183 |
except AttributeError: |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
184 |
# used outside cubicweb |
918fd9931cb7
cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4334
diff
changeset
|
185 |
pass |
996 | 186 |
return [nodes.raw('', parsed, format='html')] |
1447
a1ca676294f0
don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents:
1323
diff
changeset
|
187 |
|
996 | 188 |
pygments_directive.arguments = (1, 0, 1) |
189 |
pygments_directive.content = 1 |
|
190 |
||
191 |
||
0 | 192 |
class CubicWebReSTParser(Parser): |
193 |
"""The (customized) reStructuredText parser.""" |
|
194 |
||
195 |
def __init__(self): |
|
196 |
self.initial_state = 'Body' |
|
197 |
self.state_classes = states.state_classes |
|
198 |
self.inliner = states.Inliner() |
|
199 |
self.statemachine = states.RSTStateMachine( |
|
200 |
state_classes=self.state_classes, |
|
201 |
initial_state=self.initial_state, |
|
202 |
debug=0) |
|
203 |
||
204 |
def parse(self, inputstring, document): |
|
205 |
"""Parse `inputstring` and populate `document`, a document tree.""" |
|
206 |
self.setup_parse(inputstring, document) |
|
207 |
inputlines = statemachine.string2lines(inputstring, |
|
208 |
convert_whitespace=1) |
|
209 |
self.statemachine.run(inputlines, document, inliner=self.inliner) |
|
210 |
self.finish_parse() |
|
211 |
||
212 |
||
4771
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
213 |
# XXX docutils keep a ref on context, can't find a correct way to remove it |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
214 |
class CWReSTPublisher(Publisher): |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
215 |
def __init__(self, context, settings, **kwargs): |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
216 |
Publisher.__init__(self, **kwargs) |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
217 |
self.set_components('standalone', 'restructuredtext', 'pseudoxml') |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
218 |
self.process_programmatic_settings(None, settings, None) |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
219 |
self.settings.context = context |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
220 |
|
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
221 |
|
0 | 222 |
def rest_publish(context, data): |
223 |
"""publish a string formatted as ReStructured Text to HTML |
|
1447
a1ca676294f0
don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents:
1323
diff
changeset
|
224 |
|
0 | 225 |
:type context: a cubicweb application object |
226 |
||
227 |
:type data: str |
|
228 |
:param data: some ReST text |
|
229 |
||
230 |
:rtype: unicode |
|
231 |
:return: |
|
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
232 |
the data formatted as HTML or the original data if an error occurred |
0 | 233 |
""" |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
234 |
req = context._cw |
0 | 235 |
if isinstance(data, unicode): |
236 |
encoding = 'unicode' |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
237 |
# remove unprintable characters unauthorized in xml |
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
238 |
data = data.translate(ESC_UCAR_TABLE) |
0 | 239 |
else: |
240 |
encoding = req.encoding |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
241 |
# remove unprintable characters unauthorized in xml |
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
242 |
data = data.translate(ESC_CAR_TABLE) |
0 | 243 |
settings = {'input_encoding': encoding, 'output_encoding': 'unicode', |
4771
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
244 |
'warning_stream': StringIO(), |
6275
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
245 |
'traceback': True, # don't sys.exit |
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
246 |
'stylesheet': None, # don't try to embed stylesheet (may cause |
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
247 |
# obscure bug due to docutils computing |
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
248 |
# relative path according to the directory |
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
249 |
# used *at import time* |
0 | 250 |
# dunno what's the max, severe is 4, and we never want a crash |
6275
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
251 |
# (though try/except may be a better option...). May be the |
20f30e01ae59
[ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
252 |
# above traceback option will avoid this? |
1447
a1ca676294f0
don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents:
1323
diff
changeset
|
253 |
'halt_level': 10, |
0 | 254 |
} |
255 |
if context: |
|
256 |
if hasattr(req, 'url'): |
|
257 |
base_url = req.url() |
|
258 |
elif hasattr(context, 'absolute_url'): |
|
259 |
base_url = context.absolute_url() |
|
260 |
else: |
|
261 |
base_url = req.base_url() |
|
262 |
else: |
|
263 |
base_url = None |
|
264 |
try: |
|
4771
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
265 |
pub = CWReSTPublisher(context, settings, |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
266 |
parser=CubicWebReSTParser(), |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
267 |
writer=Writer(base_url=base_url), |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
268 |
source_class=io.StringInput, |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
269 |
destination_class=io.StringOutput) |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
270 |
pub.set_source(data) |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
271 |
pub.set_destination() |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
272 |
res = pub.publish(enable_exit_status=None) |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
273 |
# necessary for proper garbage collection, else a ref is kept somewhere in docutils... |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
274 |
del pub.settings.context |
e27d23f875c6
custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4769
diff
changeset
|
275 |
return res |
0 | 276 |
except Exception: |
277 |
LOGGER.exception('error while publishing ReST text') |
|
278 |
if not isinstance(data, unicode): |
|
279 |
data = unicode(data, encoding, 'replace') |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2311
diff
changeset
|
280 |
return xml_escape(req._('error while publishing ReST text') |
0 | 281 |
+ '\n\n' + data) |
4118
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
282 |
|
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
283 |
|
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
284 |
_INITIALIZED = False |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
285 |
def cw_rest_init(): |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
286 |
global _INITIALIZED |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
287 |
if _INITIALIZED: |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
288 |
return |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
289 |
_INITIALIZED = True |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
290 |
register_canonical_role('eid', eid_reference_role) |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
291 |
directives.register_directive('winclude', winclude_directive) |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
292 |
if pygments_directive is not None: |
8a9a00a9405c
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
293 |
directives.register_directive('sourcecode', pygments_directive) |