author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 02 Dec 2009 11:57:38 +0100 | |
branch | stable |
changeset 3962 | 15d9d47f5434 |
parent 2680 | 66472d85d548 |
child 3418 | 7b49fa7e942d |
child 4118 | 8a9a00a9405c |
permissions | -rw-r--r-- |
0 | 1 |
"""rest publishing functions |
2 |
||
996 | 3 |
contains some functions and setup of docutils for cubicweb. Provides the |
4 |
following ReST directives: |
|
5 |
||
6 |
* `eid`, create link to entity in the repository by their eid |
|
7 |
||
8 |
* `card`, create link to card entity in the repository by their wikiid |
|
9 |
(proposing to create it when the refered card doesn't exist yet) |
|
10 |
||
11 |
* `winclude`, reference to a web documentation file (in wdoc/ directories) |
|
12 |
||
13 |
* `sourcecode` (if pygments is installed), source code colorization |
|
0 | 14 |
|
15 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1643
diff
changeset
|
16 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 17 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1643
diff
changeset
|
18 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 19 |
""" |
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
from cStringIO import StringIO |
|
23 |
from itertools import chain |
|
24 |
from logging import getLogger |
|
25 |
from os.path import join |
|
26 |
||
27 |
from docutils import statemachine, nodes, utils, io |
|
28 |
from docutils.core import publish_string |
|
29 |
from docutils.parsers.rst import Parser, states, directives |
|
30 |
from docutils.parsers.rst.roles import register_canonical_role, set_classes |
|
31 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2311
diff
changeset
|
32 |
from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape |
0 | 33 |
|
2467
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
34 |
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
|
35 |
from cubicweb.ext.html4zope import Writer |
0 | 36 |
|
37 |
# We provide our own parser as an attempt to get rid of |
|
38 |
# state machine reinstanciation |
|
39 |
||
40 |
import re |
|
41 |
# compile states.Body patterns |
|
42 |
for k, v in states.Body.patterns.items(): |
|
43 |
if isinstance(v, str): |
|
44 |
states.Body.patterns[k] = re.compile(v) |
|
45 |
||
46 |
# register ReStructured Text mimetype / extensions |
|
47 |
import mimetypes |
|
48 |
mimetypes.add_type('text/rest', '.rest') |
|
49 |
mimetypes.add_type('text/rest', '.rst') |
|
50 |
||
51 |
||
52 |
LOGGER = getLogger('cubicweb.rest') |
|
53 |
||
54 |
def eid_reference_role(role, rawtext, text, lineno, inliner, |
|
55 |
options={}, content=[]): |
|
56 |
try: |
|
57 |
try: |
|
58 |
eid_num, rest = text.split(u':', 1) |
|
59 |
except: |
|
60 |
eid_num, rest = text, '#'+text |
|
61 |
eid_num = int(eid_num) |
|
62 |
if eid_num < 0: |
|
63 |
raise ValueError |
|
64 |
except ValueError: |
|
65 |
msg = inliner.reporter.error( |
|
66 |
'EID number must be a positive number; "%s" is invalid.' |
|
67 |
% text, line=lineno) |
|
68 |
prb = inliner.problematic(rawtext, rawtext, msg) |
|
69 |
return [prb], [msg] |
|
70 |
# Base URL mainly used by inliner.pep_reference; so this is correct: |
|
71 |
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
|
72 |
try: |
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2467
diff
changeset
|
73 |
refedentity = context.req.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
|
74 |
except UnknownEid: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
75 |
ref = '#' |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
76 |
rest += u' ' + context.req._('(UNEXISTANT EID)') |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
77 |
else: |
6983631f5d0d
don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
78 |
ref = refedentity.absolute_url() |
0 | 79 |
set_classes(options) |
80 |
return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref, |
|
81 |
**options)], [] |
|
82 |
||
83 |
register_canonical_role('eid', eid_reference_role) |
|
84 |
||
85 |
||
86 |
def winclude_directive(name, arguments, options, content, lineno, |
|
87 |
content_offset, block_text, state, state_machine): |
|
88 |
"""Include a reST file as part of the content of this reST file. |
|
89 |
||
90 |
same as standard include directive but using config.locate_doc_resource to |
|
91 |
get actual file to include. |
|
92 |
||
93 |
Most part of this implementation is copied from `include` directive defined |
|
94 |
in `docutils.parsers.rst.directives.misc` |
|
95 |
""" |
|
96 |
context = state.document.settings.context |
|
97 |
source = state_machine.input_lines.source( |
|
98 |
lineno - state_machine.input_offset - 1) |
|
99 |
#source_dir = os.path.dirname(os.path.abspath(source)) |
|
100 |
fid = arguments[0] |
|
101 |
for lang in chain((context.req.lang, context.vreg.property_value('ui.language')), |
|
102 |
context.config.available_languages()): |
|
103 |
rid = '%s_%s.rst' % (fid, lang) |
|
104 |
resourcedir = context.config.locate_doc_file(rid) |
|
105 |
if resourcedir: |
|
106 |
break |
|
107 |
else: |
|
108 |
severe = state_machine.reporter.severe( |
|
109 |
'Problems with "%s" directive path:\nno resource matching %s.' |
|
110 |
% (name, fid), |
|
111 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
112 |
return [severe] |
|
113 |
path = join(resourcedir, rid) |
|
114 |
encoding = options.get('encoding', state.document.settings.input_encoding) |
|
115 |
try: |
|
116 |
state.document.settings.record_dependencies.add(path) |
|
117 |
include_file = io.FileInput( |
|
118 |
source_path=path, encoding=encoding, |
|
119 |
error_handler=state.document.settings.input_encoding_error_handler, |
|
120 |
handle_io_errors=None) |
|
121 |
except IOError, error: |
|
122 |
severe = state_machine.reporter.severe( |
|
123 |
'Problems with "%s" directive path:\n%s: %s.' |
|
124 |
% (name, error.__class__.__name__, error), |
|
125 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
126 |
return [severe] |
|
127 |
try: |
|
128 |
include_text = include_file.read() |
|
129 |
except UnicodeError, error: |
|
130 |
severe = state_machine.reporter.severe( |
|
131 |
'Problem with "%s" directive:\n%s: %s' |
|
132 |
% (name, error.__class__.__name__, error), |
|
133 |
nodes.literal_block(block_text, block_text), line=lineno) |
|
134 |
return [severe] |
|
135 |
if options.has_key('literal'): |
|
136 |
literal_block = nodes.literal_block(include_text, include_text, |
|
137 |
source=path) |
|
138 |
literal_block.line = 1 |
|
139 |
return literal_block |
|
140 |
else: |
|
141 |
include_lines = statemachine.string2lines(include_text, |
|
142 |
convert_whitespace=1) |
|
143 |
state_machine.insert_input(include_lines, path) |
|
144 |
return [] |
|
145 |
||
146 |
winclude_directive.arguments = (1, 0, 1) |
|
147 |
winclude_directive.options = {'literal': directives.flag, |
|
148 |
'encoding': directives.encoding} |
|
149 |
directives.register_directive('winclude', winclude_directive) |
|
150 |
||
996 | 151 |
try: |
152 |
from pygments import highlight |
|
153 |
from pygments.lexers import get_lexer_by_name, LEXERS |
|
154 |
from pygments.formatters import HtmlFormatter |
|
155 |
except ImportError: |
|
156 |
pass |
|
157 |
else: |
|
158 |
_PYGMENTS_FORMATTER = HtmlFormatter() |
|
159 |
||
160 |
def pygments_directive(name, arguments, options, content, lineno, |
|
161 |
content_offset, block_text, state, state_machine): |
|
162 |
try: |
|
163 |
lexer = get_lexer_by_name(arguments[0]) |
|
164 |
except ValueError: |
|
165 |
import traceback |
|
166 |
traceback.print_exc() |
|
167 |
print sorted(aliases for module_name, name, aliases, _, _ in LEXERS.itervalues()) |
|
168 |
# no lexer found |
|
169 |
lexer = get_lexer_by_name('text') |
|
170 |
parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER) |
|
171 |
context = state.document.settings.context |
|
172 |
context.req.add_css('pygments.css') |
|
173 |
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
|
174 |
|
996 | 175 |
pygments_directive.arguments = (1, 0, 1) |
176 |
pygments_directive.content = 1 |
|
177 |
directives.register_directive('sourcecode', pygments_directive) |
|
178 |
||
179 |
||
0 | 180 |
class CubicWebReSTParser(Parser): |
181 |
"""The (customized) reStructuredText parser.""" |
|
182 |
||
183 |
def __init__(self): |
|
184 |
self.initial_state = 'Body' |
|
185 |
self.state_classes = states.state_classes |
|
186 |
self.inliner = states.Inliner() |
|
187 |
self.statemachine = states.RSTStateMachine( |
|
188 |
state_classes=self.state_classes, |
|
189 |
initial_state=self.initial_state, |
|
190 |
debug=0) |
|
191 |
||
192 |
def parse(self, inputstring, document): |
|
193 |
"""Parse `inputstring` and populate `document`, a document tree.""" |
|
194 |
self.setup_parse(inputstring, document) |
|
195 |
inputlines = statemachine.string2lines(inputstring, |
|
196 |
convert_whitespace=1) |
|
197 |
self.statemachine.run(inputlines, document, inliner=self.inliner) |
|
198 |
self.finish_parse() |
|
199 |
||
200 |
||
201 |
def rest_publish(context, data): |
|
202 |
"""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
|
203 |
|
0 | 204 |
:type context: a cubicweb application object |
205 |
||
206 |
:type data: str |
|
207 |
:param data: some ReST text |
|
208 |
||
209 |
:rtype: unicode |
|
210 |
:return: |
|
211 |
the data formatted as HTML or the original data if an error occured |
|
212 |
""" |
|
213 |
req = context.req |
|
214 |
if isinstance(data, unicode): |
|
215 |
encoding = 'unicode' |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
216 |
# 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
|
217 |
data = data.translate(ESC_UCAR_TABLE) |
0 | 218 |
else: |
219 |
encoding = req.encoding |
|
2311
f178182b1305
actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
220 |
# 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
|
221 |
data = data.translate(ESC_CAR_TABLE) |
0 | 222 |
settings = {'input_encoding': encoding, 'output_encoding': 'unicode', |
223 |
'warning_stream': StringIO(), 'context': context, |
|
224 |
# dunno what's the max, severe is 4, and we never want a crash |
|
225 |
# (though try/except may be a better option...) |
|
1447
a1ca676294f0
don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents:
1323
diff
changeset
|
226 |
'halt_level': 10, |
0 | 227 |
} |
228 |
if context: |
|
229 |
if hasattr(req, 'url'): |
|
230 |
base_url = req.url() |
|
231 |
elif hasattr(context, 'absolute_url'): |
|
232 |
base_url = context.absolute_url() |
|
233 |
else: |
|
234 |
base_url = req.base_url() |
|
235 |
else: |
|
236 |
base_url = None |
|
237 |
try: |
|
238 |
return publish_string(writer=Writer(base_url=base_url), |
|
1447
a1ca676294f0
don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents:
1323
diff
changeset
|
239 |
parser=CubicWebReSTParser(), source=data, |
0 | 240 |
settings_overrides=settings) |
241 |
except Exception: |
|
242 |
LOGGER.exception('error while publishing ReST text') |
|
243 |
if not isinstance(data, unicode): |
|
244 |
data = unicode(data, encoding, 'replace') |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2311
diff
changeset
|
245 |
return xml_escape(req._('error while publishing ReST text') |
0 | 246 |
+ '\n\n' + data) |