/
doxyrest.py
390 lines (303 loc) · 11.6 KB
/
doxyrest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#...............................................................................
#
# This file is part of the Doxyrest toolkit.
#
# Doxyrest is distributed under the MIT license.
# For details see accompanying license.txt file,
# the public copy of which is also available at:
# http://tibbo.com/downloads/archive/doxyrest/license.txt
#
#...............................................................................
import os
import re
import warnings
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from docutils.transforms import Transform
from docutils.statemachine import StringList, string2lines
from sphinx import roles, addnodes, config
from sphinx.io import SphinxBaseFileInput, SphinxRSTFileInput
from sphinx.directives.other import Include
#...............................................................................
#
# utils
#
this_dir = os.path.dirname(os.path.realpath(__file__))
crefdb = None
def get_cref_target(text):
if crefdb and text in crefdb:
return crefdb[text]
warnings.warn('target not found for cref: ' + text, Warning, 2)
return None
#...............................................................................
#
# Sphinx nodes
#
class HighlightedText(nodes.General, nodes.TextElement):
def __init__(self, *args, **kwargs):
nodes.TextElement.__init__(self, *args, **kwargs)
def visit_highlighted_text_node(self, node):
text_node = node.children[0]
language = node['language']
if language == 'none':
self.body.append(text_node)
else:
options = {}
options['stripnl'] = False
options['ensurenl'] = False
text = text_node
# pygments always ensures a newline at the end and there is no way to tell
# if the newline is originating from source or was it inserted by pygments
# as a workaround, duplicate trailing newline and then strip it
if text.endswith('\n'):
text = text + '\n'
highlighted = self.highlighter.highlight_block(text, language, options)
match = re.search('<pre>(.*)\n</pre>', highlighted, re.DOTALL)
if not match:
self.body.append(text_node)
else:
self.body.append(match.group(1))
raise nodes.SkipNode
def create_xref_node(raw_text, text, target):
if not target:
return nodes.Text(text, text)
node = addnodes.pending_xref(raw_text)
node['reftype'] = 'ref'
node['refdomain'] = 'std'
node['reftarget'] = target
node['refwarn'] = True
node['refexplicit'] = True
node += nodes.Text(text, text)
return node
def create_target_node(raw_text, text, target, lineno, document, extra_classes=[]):
node = nodes.target(raw_text, '')
node['names'] += [target]
node['classes'] += extra_classes
node.line = lineno
if text:
node += nodes.Text(text, text)
document.note_explicit_target(node)
return node
#...............................................................................
#
# Sphinx directives
#
class RefCodeBlock(Directive):
has_content = True
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = False
option_spec = {
'linenos': directives.flag,
'dedent': int,
'lineno-start': int,
'emphasize-lines': directives.unchanged_required,
'caption': directives.unchanged_required,
'class': directives.class_option,
'name': directives.unchanged,
}
def __init__(self, *args, **kwargs):
Directive.__init__(self, *args, **kwargs)
role_re_src = '(:ref:|:cref:|:target:)'
if self.state.document.settings.env.config.default_role == 'cref':
role_re_src += '?' # explicit role is optional
role_re_src += '`(.+?)(\s*<([^<>]*)>)?`'
self.role_re_prog = re.compile(role_re_src)
self.ws_re_prog = re.compile('\s+')
self.url_re_prog = re.compile('(ftp|https?)://')
def run(self):
config = self.state.document.settings.env.config
code = u'\n'.join(self.content)
pos = 0
node = nodes.literal_block('.', '') # single char to prevent sphinx from trying to highlight it
node['classes'] += ['highlight'] # we are stripping pyments-generated <div>
node['classes'] += self.options.get('class', [])
if len(self.arguments) >= 1:
language = self.arguments[0]
else:
language = config.highlight_language
while True:
match = self.role_re_prog.search(code, pos)
if match is None:
plain_text = code[pos:]
if plain_text != "":
node += HighlightedText(plain_text, plain_text, language=language)
break
plain_text = code[pos:match.start()]
if plain_text != "":
node += HighlightedText(plain_text, plain_text, language=language)
raw_text = match.group(0)
role = match.group(1)
text = match.group(2)
target = match.group(4)
pos = match.end()
if text:
text = text.replace('\\<', '<') # restore escaped left-chevron
if role == ':target:':
if not target:
target = text
text = None
ws_match = self.ws_re_prog.match(code, pos)
if ws_match: # drop whitespace right after empty target
pos = ws_match.end()
new_node = create_target_node(
raw_text,
text,
target,
None,
self.state.document,
['doxyrest-code-target']
)
elif not role or role == ':cref:':
target = get_cref_target(target if target else text)
new_node = create_xref_node(raw_text, text, target)
else: # :ref:
if not target:
target = text
if self.url_re_prog.match(target):
new_node = nodes.reference(raw_text, text, refuri=target)
else:
new_node = create_xref_node(raw_text, text, target)
node += new_node
self.add_name(node)
return [node]
class TabAwareInclude(Include):
def run(self):
# update tab_width setting
self.state.document.settings.tab_width = \
self.state.document.settings.env.config.doxyrest_tab_width
return Include.run(self)
#...............................................................................
#
# Sphinx transforms
#
class RefTransform(Transform):
default_priority = 100
node_classes = {
nodes.literal,
nodes.strong,
nodes.emphasis
}
def __init__(self, document, startnode=None):
Transform.__init__(self, document, startnode)
re_src = '(:c?ref:)'
if document.settings.env.config.default_role == 'cref':
re_src += '?' # explicit role is optional
re_src += '`(.+?)(\s*<([^<>]*)>)?`'
self.re_prog = re.compile(re_src)
@staticmethod
def node_filter(node):
for node_class in RefTransform.node_classes:
if isinstance (node, node_class):
return node['classes'] == []
return False
def apply(self):
for node in self.document.traverse(RefTransform.node_filter):
code = node.astext()
node.children = []
pos = 0
while True:
match = self.re_prog.search(code, pos)
if match is None:
plain_text = code[pos:]
if plain_text != "":
node += nodes.Text(plain_text, plain_text)
break
plain_text = code[pos:match.start()]
if plain_text != "":
node += nodes.Text(plain_text, plain_text)
raw_text = match.group(0)
role = match.group(1)
text = match.group(2)
target = match.group(4)
if not role or role == ':cref:':
target = get_cref_target(text)
node += create_xref_node(raw_text, text, target)
pos = match.end()
#...............................................................................
#
# Sphinx roles
#
def cref_role(typ, raw_text, text, lineno, inliner, options={}, content=[]):
target = get_cref_target(text)
if text.find(' ') == -1:
node = nodes.literal(raw_text, '')
else:
node = nodes.inline(raw_text, '')
node['classes'] += ['doxyrest-cref']
node += create_xref_node(raw_text, text, target)
return [node], []
def target_role(typ, raw_text, text, lineno, inliner, options={}, content=[]):
node = create_target_node(raw_text, None, text, lineno, inliner.document)
return [node], []
#...............................................................................
#
# Sphinx source inputs
#
class TabAwareSphinxRSTFileInput(SphinxRSTFileInput):
def read(self):
# type: () -> StringList
inputstring = SphinxBaseFileInput.read(self)
tab_width = self.env.config.doxyrest_tab_width
lines = string2lines(inputstring, convert_whitespace=True, tab_width=tab_width)
content = StringList()
for lineno, line in enumerate(lines):
content.append(line, self.source_path, lineno)
if self.env.config.rst_prolog:
self.prepend_prolog(content, self.env.config.rst_prolog)
if self.env.config.rst_epilog:
self.append_epilog(content, self.env.config.rst_epilog)
return content
#...............................................................................
#
# Sphinx events
#
def on_builder_inited(app):
app.config.html_static_path += [
this_dir + '/css/doxyrest-pygments.css',
this_dir + '/js/target-highlight.js'
]
app.add_stylesheet('doxyrest-pygments.css')
app.add_javascript('target-highlight.js')
supported_themes = {
'sphinx_rtd_theme',
'sphinxdoc'
}
if app.config.html_theme in supported_themes:
css_file = 'doxyrest-' + app.config.html_theme + '.css'
app.config.html_static_path += [this_dir + '/css/' + css_file];
app.add_stylesheet(css_file);
crefdb_path = app.srcdir + '/crefdb.py'
if os.path.isfile(crefdb_path):
ns = {}
src = open(crefdb_path).read()
exec(src, ns)
global crefdb
crefdb = ns['crefdb']
#...............................................................................
#
# Doxyrest extenstion setup
#
def setup(app):
app.add_node(
HighlightedText,
html=(visit_highlighted_text_node, None),
latex=(visit_highlighted_text_node, None)
)
app.add_role('cref', cref_role)
app.add_role('target', target_role)
app.add_config_value('doxyrest_tab_width', default=4, rebuild=True)
app.add_config_value('doxyrest_cref_file', default=None, rebuild=True)
app.registry.source_inputs['restructuredtext'] = TabAwareSphinxRSTFileInput
directives.register_directive('ref-code-block', RefCodeBlock)
directives.register_directive('include', TabAwareInclude)
app.add_transform(RefTransform)
app.connect('builder-inited', on_builder_inited)
# register our docutils.conf
prevConfig = None
if 'DOCUTILSCONFIG' in os.environ:
prevConfig = os.environ['DOCUTILSCONFIG']
os.environ['DOCUTILSCONFIG'] = this_dir + '/conf/docutils.conf'
if prevConfig:
os.environ['DOCUTILSCONFIG'] += os.pathsep
os.environ['DOCUTILSCONFIG'] += prevConfig