author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Mon, 25 Mar 2013 15:28:18 +0100 | |
changeset 8787 | 1b3b7284377f |
parent 8711 | 64a400eea6dd |
child 8852 | 59a29405688c |
permissions | -rw-r--r-- |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7732
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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 |
|
6938
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
50 |
from cubicweb.web.views import vid_from_rset # XXX better not to import c.w.views here... |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
51 |
|
0 | 52 |
# We provide our own parser as an attempt to get rid of |
53 |
# state machine reinstanciation |
|
54 |
||
55 |
import re |
|
56 |
# compile states.Body patterns |
|
57 |
for k, v in states.Body.patterns.items(): |
|
58 |
if isinstance(v, str): |
|
59 |
states.Body.patterns[k] = re.compile(v) |
|
60 |
||
61 |
# register ReStructured Text mimetype / extensions |
|
62 |
import mimetypes |
|
63 |
mimetypes.add_type('text/rest', '.rest') |
|
64 |
mimetypes.add_type('text/rest', '.rst') |
|
65 |
||
66 |
||
67 |
LOGGER = getLogger('cubicweb.rest') |
|
68 |
||
69 |
def eid_reference_role(role, rawtext, text, lineno, inliner, |
|
70 |
options={}, content=[]): |
|
71 |
try: |
|
72 |
try: |
|
73 |
eid_num, rest = text.split(u':', 1) |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7732
diff
changeset
|
74 |
except ValueError: |
0 | 75 |
eid_num, rest = text, '#'+text |
76 |
eid_num = int(eid_num) |
|
77 |
if eid_num < 0: |
|
78 |
raise ValueError |
|
79 |
except ValueError: |
|
80 |
msg = inliner.reporter.error( |
|
81 |
'EID number must be a positive number; "%s" is invalid.' |
|
82 |
% text, line=lineno) |
|
83 |
prb = inliner.problematic(rawtext, rawtext, msg) |
|
84 |
return [prb], [msg] |
|
85 |
# Base URL mainly used by inliner.pep_reference; so this is correct: |
|
86 |
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
|
87 |
try: |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
88 |
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
|
89 |
except UnknownEid: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
90 |
ref = '#' |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
91 |
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
|
92 |
else: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
93 |
ref = refedentity.absolute_url() |
0 | 94 |
set_classes(options) |
95 |
return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref, |
|
96 |
**options)], [] |
|
97 |
||
6938
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
98 |
def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]): |
8710
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
99 |
""":rql:`<rql-expr>` or :rql:`<rql-expr>:<vid>` |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
100 |
|
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
101 |
Example: :rql:`Any X,Y WHERE X is CWUser, X login Y:table` |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
102 |
|
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
103 |
Replace the directive with the output of applying the view to the resultset |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
104 |
returned by the query. |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
105 |
|
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
106 |
"X eid %(userid)s" can be used in the RQL query for this query will be |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
107 |
executed with the argument {'userid': _cw.user.eid}. |
becbbbc840b8
[ext/rest] fix docstring of rql_role()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
108 |
""" |
6938
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
109 |
_cw = inliner.document.settings.context._cw |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
110 |
text = text.strip() |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
111 |
if ':' in text: |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
112 |
rql, vid = text.rsplit(u':', 1) |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
113 |
rql = rql.strip() |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
114 |
else: |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
115 |
rql, vid = text, None |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
116 |
_cw.ensure_ro_rql(rql) |
7732
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
117 |
try: |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
118 |
rset = _cw.execute(rql, {'userid': _cw.user.eid}) |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
119 |
if rset: |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
120 |
if vid is None: |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
121 |
vid = vid_from_rset(_cw, rset, _cw.vreg.schema) |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
122 |
else: |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
123 |
vid = 'noresult' |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
124 |
view = _cw.vreg['views'].select(vid, _cw, rset=rset) |
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
125 |
content = view.render() |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
126 |
except Exception as exc: |
7732
5430d0db52ab
[web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7577
diff
changeset
|
127 |
content = 'an error occured while interpreting this rql directive: %r' % exc |
6938
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
128 |
set_classes(options) |
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
129 |
return [nodes.raw('', content, format='html')], [] |
0 | 130 |
|
131 |
def winclude_directive(name, arguments, options, content, lineno, |
|
132 |
content_offset, block_text, state, state_machine): |
|
133 |
"""Include a reST file as part of the content of this reST file. |
|
134 |
||
135 |
same as standard include directive but using config.locate_doc_resource to |
|
136 |
get actual file to include. |
|
137 |
||
138 |
Most part of this implementation is copied from `include` directive defined |
|
139 |
in `docutils.parsers.rst.directives.misc` |
|
140 |
""" |
|
141 |
context = state.document.settings.context |
|
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
142 |
cw = context._cw |
0 | 143 |
source = state_machine.input_lines.source( |
144 |
lineno - state_machine.input_offset - 1) |
|
145 |
#source_dir = os.path.dirname(os.path.abspath(source)) |
|
146 |
fid = arguments[0] |
|
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
147 |
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
|
148 |
cw.vreg.config.available_languages()): |
0 | 149 |
rid = '%s_%s.rst' % (fid, lang) |
4769
5b878b02b67b
[rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
150 |
resourcedir = cw.vreg.config.locate_doc_file(rid) |
0 | 151 |
if resourcedir: |
152 |
break |
|
153 |
else: |
|
154 |
severe = state_machine.reporter.severe( |
|
155 |
'Problems with "%s" directive path:\nno resource matching %s.' |
|
156 |
% (name, fid), |
|
157 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
158 |
return [severe] |
|
159 |
path = join(resourcedir, rid) |
|
160 |
encoding = options.get('encoding', state.document.settings.input_encoding) |
|
161 |
try: |
|
162 |
state.document.settings.record_dependencies.add(path) |
|
163 |
include_file = io.FileInput( |
|
164 |
source_path=path, encoding=encoding, |
|
165 |
error_handler=state.document.settings.input_encoding_error_handler, |
|
166 |
handle_io_errors=None) |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
167 |
except IOError as error: |
0 | 168 |
severe = state_machine.reporter.severe( |
169 |
'Problems with "%s" directive path:\n%s: %s.' |
|
170 |
% (name, error.__class__.__name__, error), |
|
171 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
172 |
return [severe] |
|
173 |
try: |
|
174 |
include_text = include_file.read() |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
175 |
except UnicodeError as error: |
0 | 176 |
severe = state_machine.reporter.severe( |
177 |
'Problem with "%s" directive:\n%s: %s' |
|
178 |
% (name, error.__class__.__name__, error), |
|
179 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
180 |
return [severe] |
|
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
181 |
if 'literal' in options: |
0 | 182 |
literal_block = nodes.literal_block(include_text, include_text, |
183 |
source=path) |
|
184 |
literal_block.line = 1 |
|
185 |
return literal_block |
|
186 |
else: |
|
187 |
include_lines = statemachine.string2lines(include_text, |
|
188 |
convert_whitespace=1) |
|
189 |
state_machine.insert_input(include_lines, path) |
|
190 |
return [] |
|
191 |
||
192 |
winclude_directive.arguments = (1, 0, 1) |
|
193 |
winclude_directive.options = {'literal': directives.flag, |
|
194 |
'encoding': directives.encoding} |
|
195 |
||
996 | 196 |
try: |
197 |
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
|
198 |
from pygments.lexers import get_lexer_by_name |
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
199 |
from pygments.formatters.html import HtmlFormatter |
996 | 200 |
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
|
201 |
pygments_directive = None |
996 | 202 |
else: |
203 |
_PYGMENTS_FORMATTER = HtmlFormatter() |
|
204 |
||
205 |
def pygments_directive(name, arguments, options, content, lineno, |
|
206 |
content_offset, block_text, state, state_machine): |
|
207 |
try: |
|
208 |
lexer = get_lexer_by_name(arguments[0]) |
|
209 |
except ValueError: |
|
210 |
# no lexer found |
|
211 |
lexer = get_lexer_by_name('text') |
|
212 |
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
|
213 |
# 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
|
214 |
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
|
215 |
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
|
216 |
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
|
217 |
except AttributeError: |
7577
9892937d9041
[all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6938
diff
changeset
|
218 |
# used outside cubicweb XXX use hasattr instead |
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
|
219 |
pass |
996 | 220 |
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
|
221 |
|
996 | 222 |
pygments_directive.arguments = (1, 0, 1) |
223 |
pygments_directive.content = 1 |
|
224 |
||
225 |
||
0 | 226 |
class CubicWebReSTParser(Parser): |
227 |
"""The (customized) reStructuredText parser.""" |
|
228 |
||
229 |
def __init__(self): |
|
230 |
self.initial_state = 'Body' |
|
231 |
self.state_classes = states.state_classes |
|
232 |
self.inliner = states.Inliner() |
|
233 |
self.statemachine = states.RSTStateMachine( |
|
234 |
state_classes=self.state_classes, |
|
235 |
initial_state=self.initial_state, |
|
236 |
debug=0) |
|
237 |
||
238 |
def parse(self, inputstring, document): |
|
239 |
"""Parse `inputstring` and populate `document`, a document tree.""" |
|
240 |
self.setup_parse(inputstring, document) |
|
241 |
inputlines = statemachine.string2lines(inputstring, |
|
242 |
convert_whitespace=1) |
|
243 |
self.statemachine.run(inputlines, document, inliner=self.inliner) |
|
244 |
self.finish_parse() |
|
245 |
||
246 |
||
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
|
247 |
# 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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
|
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
|
255 |
|
0 | 256 |
def rest_publish(context, data): |
257 |
"""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
|
258 |
|
0 | 259 |
:type context: a cubicweb application object |
260 |
||
261 |
:type data: str |
|
262 |
:param data: some ReST text |
|
263 |
||
264 |
:rtype: unicode |
|
265 |
: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
|
266 |
the data formatted as HTML or the original data if an error occurred |
0 | 267 |
""" |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
268 |
req = context._cw |
0 | 269 |
if isinstance(data, unicode): |
270 |
encoding = 'unicode' |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
271 |
# 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
|
272 |
data = data.translate(ESC_UCAR_TABLE) |
0 | 273 |
else: |
274 |
encoding = req.encoding |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
275 |
# 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
|
276 |
data = data.translate(ESC_CAR_TABLE) |
0 | 277 |
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
|
278 |
'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
|
279 |
'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
|
280 |
'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
|
281 |
# 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
|
282 |
# 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
|
283 |
# used *at import time* |
0 | 284 |
# 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
|
285 |
# (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
|
286 |
# 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
|
287 |
'halt_level': 10, |
0 | 288 |
} |
289 |
if context: |
|
290 |
if hasattr(req, 'url'): |
|
291 |
base_url = req.url() |
|
292 |
elif hasattr(context, 'absolute_url'): |
|
293 |
base_url = context.absolute_url() |
|
294 |
else: |
|
295 |
base_url = req.base_url() |
|
296 |
else: |
|
297 |
base_url = None |
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
# 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
|
308 |
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
|
309 |
return res |
0 | 310 |
except Exception: |
311 |
LOGGER.exception('error while publishing ReST text') |
|
312 |
if not isinstance(data, unicode): |
|
313 |
data = unicode(data, encoding, 'replace') |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2311
diff
changeset
|
314 |
return xml_escape(req._('error while publishing ReST text') |
0 | 315 |
+ '\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
|
316 |
|
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
|
317 |
|
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
|
318 |
_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
|
319 |
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
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
_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
|
324 |
register_canonical_role('eid', eid_reference_role) |
6938
6c1a960735f5
add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6275
diff
changeset
|
325 |
register_canonical_role('rql', rql_role) |
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
|
326 |
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
|
327 |
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
|
328 |
directives.register_directive('sourcecode', pygments_directive) |