1
2
3
4 from rst import *
5
6 legal_pygments_languages = [
7 'Cucumber', 'cucumber', 'Gherkin', 'gherkin',
8 'abap', 'ada', 'ada95ada2005',
9 'antlr-as', 'antlr-actionscript', 'antlr-cpp', 'antlr-csharp',
10 'antlr-c#', 'antlr-java', 'antlr-objc', 'antlr-perl',
11 'antlr-python', 'antlr-ruby', 'antlr-rb', 'antlr',
12 'apacheconf', 'aconf', 'apache', 'applescript', 'as',
13 'actionscript', 'as3', 'actionscript3', 'aspx-cs', 'aspx-vb',
14 'asy', 'asymptote', 'basemake', 'bash', 'sh', 'ksh', 'bat',
15 'bbcode', 'befunge', 'boo', 'brainfuck', 'bf', 'c-objdump',
16 'c', 'cfm', 'cfs', 'cheetah', 'spitfire', 'clojure', 'clj',
17 'cmake', 'coffee-script', 'coffeescript', 'common-lisp',
18 'cl', 'console', 'control',
19 'cpp', 'c++', 'cpp-objdump', 'c++-objdumb', 'cxx-objdump',
20 'csharp', 'c#',
21 'css+django', 'css+jinja', 'css+erb', 'css+ruby',
22 'css+genshitext', 'css+genshi', 'css+mako', 'css+myghty',
23 'css+php', 'css+smarty', 'css',
24 'cython', 'pyx', 'd-objdump', 'd', 'delphi', 'pas',
25 'pascal', 'objectpascal', 'diff', 'udiff',
26 'django', 'jinja', 'dpatch', 'dylan', 'erb',
27 'erl', 'erlang', 'evoque', 'felix', 'flx',
28 'fortran', 'gas', 'genshi', 'kid',
29 'xml+genshi', 'xml+kid', 'genshitext', 'glsl',
30 'gnuplot', 'go', 'groff', 'nroff', 'man', 'haml',
31 'HAML', 'haskell', 'hs',
32 'html+cheetah', 'html+spitfire', 'html+django', 'html+jinja',
33 'html+evoque', 'html+genshi', 'html+kid', 'html+mako',
34 'html+myghty', 'html+php', 'html+smarty', 'html',
35 'hx', 'haXe', 'ini', 'cfg', 'io', 'irc',
36 'java', 'js+cheetah', 'javascript+cheetah', 'js+spitfire',
37 'javascript+spitfire', 'js+django', 'javascript+django',
38 'js+jinja', 'javascript+jinja', 'js+erb', 'javascript+erb',
39 'js+ruby', 'javascript+ruby', 'js+genshitext', 'js+genshi',
40 'javascript+genshitext', 'javascript+genshi', 'js+mako',
41 'javascript+mako', 'js+myghty', 'javascript+myghty',
42 'js+php', 'javascript+php', 'js+smarty', 'javascript+smarty',
43 'js', 'javascript', 'jsp',
44 'lhs', 'literate-haskell', 'lighty', 'lighttpd', 'llvm',
45 'logtalk', 'lua', 'make', 'makefile', 'mf', 'bsdmake',
46 'mako', 'matlab', 'octave', 'matlabsession', 'minid',
47 'modelica', 'modula2', 'm2', 'moocode', 'mupad', 'mxml',
48 'myghty', 'mysql', 'nasm', 'newspeak', 'nginx', 'numpy',
49 'objdump', 'objective-c', 'objectivec', 'obj-c', 'objc',
50 'objective-j', 'objectivej', 'obj-j', 'objj', 'ocaml',
51 'ooc', 'perl', 'pl', 'php', 'php3', 'php4', 'php5',
52 'pot', 'po', 'pov', 'prolog', 'py3tb', 'pycon', 'pytb',
53 'python', 'py', 'python3', 'py3', 'ragel-c', 'ragel-cpp',
54 'ragel-d', 'ragel-em', 'ragel-java', 'ragel-objc',
55 'ragel-ruby', 'ragel-rb', 'ragel', 'raw', 'rb', 'ruby',
56 'rbcon', 'irb', 'rconsole', 'rout', 'rebol', 'redcode',
57 'rhtml', 'html+erb', 'html+ruby', 'rst', 'rest',
58 'restructuredtext', 'sass', 'SASS', 'scala', 'scheme',
59 'scm', 'smalltalk', 'squeak', 'smarty', 'sourceslist',
60 'sources.list', 'splus', 's', 'r', 'sql', 'sqlite3',
61 'squidconf', 'squid.conf', 'squid', 'tcl', 'tcsh',
62 'csh', 'tex', 'latex', 'text', 'trac-wiki', 'moin',
63 'vala', 'vapi', 'vb.net', 'vbnet', 'vim',
64 'xml+cheetah', 'xml+spitfire', 'xml+django', 'xml+jinja',
65 'xml+erb', 'xml+ruby', 'xml+evoque', 'xml+mako',
66 'xml+myghty', 'xml+php', 'xml+smarty', 'xml', 'xslt', 'yaml']
67
68
69
115
116 from latex import fix_latex_command_regex as fix_latex
117
119
120
121
122
123
124
125
126
127
128 m = re.search(r'#\s*[Ss]phinx\s+code-blocks?:(.+?)\n', filestr)
129 if m:
130 defs_line = m.group(1)
131
132 defs = {}
133 for definition in defs_line.split():
134 key, value = definition.split('=')
135 defs[key] = value
136 else:
137
138 defs = dict(cod='python', pycod='python', cycod='cython',
139 fcod='fortran', ccod='c', cppcod='c++',
140 mcod='matlab', plcod='perl', shcod='bash',
141 pro='python', pypro='python', cypro='cython',
142 fpro='fortran', cpro='c', cpppro='c++',
143 mpro='matlab', plpro='perl', shpro='bash',
144 sys='console', dat='python', ipy='python')
145
146
147
148
149
150 filestr, code_blocks, tex_blocks = remove_code_and_tex(filestr)
151 for i in range(len(code_blocks)):
152 code_blocks[i] = indent_lines(code_blocks[i], format)
153 for i in range(len(tex_blocks)):
154 tex_blocks[i] = indent_lines(tex_blocks[i], format)
155
156 tex_blocks[i] = re.sub(fix_latex(r'\label\{.+?\}', application='match'),
157 '', tex_blocks[i])
158
159 tex_blocks[i] = re.sub(r'label\{.+?\}', '', tex_blocks[i])
160
161
162 commands = [r'\begin{equation}',
163 r'\end{equation}',
164 r'\begin{equation*}',
165 r'\end{equation*}',
166 r'\begin{eqnarray}',
167 r'\end{eqnarray}',
168 r'\begin{eqnarray*}',
169 r'\end{eqnarray*}',
170 r'\begin{align}',
171 r'\end{align}',
172 r'\begin{align*}',
173 r'\end{align*}',
174 r'\begin{multline}',
175 r'\end{multline}',
176 r'\begin{multline*}',
177 r'\end{multline*}',
178 r'\begin{split}',
179 r'\end{split}',
180 r'\begin{gather}',
181 r'\end{gather}',
182 r'\begin{gather*}',
183 r'\end{gather*}',
184 r'\[',
185 r'\]',
186
187 r'\beqan',
188 r'\eeqan',
189 r'\beqa',
190 r'\eeqa',
191 r'\balnn',
192 r'\ealnn',
193 r'\baln',
194 r'\ealn',
195 r'\beq',
196 r'\eeq',
197 ]
198 for command in commands:
199 tex_blocks[i] = tex_blocks[i].replace(command, '')
200 tex_blocks[i] = re.sub('&\s*=\s*&', ' &= ', tex_blocks[i])
201
202 if '{alignat' in tex_blocks[i]:
203 print '\nWarning: the "alignat" environment will give errors in Sphinx:\n\n', tex_blocks[i], '\n'
204
205
206 filestr = insert_code_and_tex(filestr, code_blocks, tex_blocks, 'rst')
207
208 for key in defs:
209 language = defs[key]
210 if not language in legal_pygments_languages:
211 raise TypeError('%s is not a legal Pygments language '\
212 '(lexer) in line with:\n %s' % \
213 (language, defs_line))
214
215
216
217 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE)
218 filestr, n = cpattern.subn('\n.. code-block:: %s\n\n' % defs[key], filestr)
219 print key, n
220 if n > 0:
221 print 'sphinx: %d subst %s by %s' % (n, key, defs[key])
222
223
224
225
226 cpattern = re.compile(r'^!bc.+$', flags=re.MULTILINE)
227 filestr = cpattern.sub('\n.. code-block:: py\n\n', filestr)
228
229 filestr = re.sub(r'!ec *\n', '\n\n', filestr)
230
231
232 filestr = re.sub(r'!bt *\n', '\n.. math::\n\n', filestr)
233 filestr = re.sub(r'!et *\n', '\n\n', filestr)
234
235 return filestr
236
238
239
240
241
242
243
244
245
246
247 m = re.search(r'#\s*[Ss]phinx\s+code-blocks?:(.+?)\n', filestr)
248 if m:
249 defs_line = m.group(1)
250
251 defs = {}
252 for definition in defs_line.split():
253 key, value = definition.split('=')
254 defs[key] = value
255 else:
256
257 defs = dict(cod='python', pycod='python', cppcod='c++',
258 fcod='fortran', ccod='c',
259 pro='python', pypro='python', cpppro='c++',
260 fpro='fortran', cpro='c',
261 sys='console', dat='python')
262
263
264
265
266
267
268
269 filestr, code_blocks, tex_blocks = remove_code_and_tex(filestr)
270 for i in range(len(code_blocks)):
271 code_blocks[i] = indent_lines(code_blocks[i], format)
272
273 math_labels = []
274 for i in range(len(tex_blocks)):
275 tex_blocks[i] = indent_lines(tex_blocks[i], format)
276
277
278 label_regex1 = fix_latex(r'\label\{(.+?)\}', application='match')
279 label_regex2 = fix_latex( r'label\{(.+?)\}', application='match')
280 math_labels.extend(re.findall(label_regex1, tex_blocks[i]))
281 tex_blocks[i] = re.sub(label_regex1,
282 r' :label: \g<1> ', tex_blocks[i])
283
284 math_labels.extend(re.findall(label_regex2, tex_blocks[i]))
285 tex_blocks[i] = re.sub(label_regex2, r' :label: \g<1> ', tex_blocks[i])
286
287
288 for label in math_labels:
289 filestr = filestr.replace(':ref:`%s`' % label, ':eq:`%s`' % label)
290
291 filestr = insert_code_and_tex(filestr, code_blocks, tex_blocks, 'rst')
292
293 for key in defs:
294 language = defs[key]
295 if not language in legal_pygments_languages:
296 raise TypeError('%s is not a legal Pygments language '\
297 '(lexer) in line with:\n %s' % \
298 (language, defs_line))
299
300
301
302 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE)
303 filestr = cpattern.sub('\n.. code-block:: %s\n\n' % defs[key], filestr)
304
305
306
307
308 cpattern = re.compile(r'^!bc.+$', flags=re.MULTILINE)
309 filestr = cpattern.sub('\n.. code-block:: py\n\n', filestr)
310
311 filestr = re.sub(r'!ec *\n', '\n\n', filestr)
312
313
314 filestr = re.sub(r'!bt *\n', '\n.. math::\n :nowrap:\n\n', filestr)
315 filestr = re.sub(r'!et *\n', '\n\n', filestr)
316
317 return filestr
318
320
321
322
323
324
325
326
327 m = re.search(r'#\s*[Ss]phinx\s+code-blocks?:(.+?)\n', filestr)
328 if m:
329 defs_line = m.group(1)
330
331 envir2lang = {}
332 for definition in defs_line.split():
333 key, value = definition.split('=')
334 envir2lang[key] = value
335 else:
336
337 envir2lang = dict(cod='python', pycod='python', cycod='cython',
338 fcod='fortran', ccod='c', cppcod='c++',
339 mcod='matlab', plcod='perl', shcod='bash',
340 rst='rst', ipy='python',
341 pro='python', pypro='python', cypro='cython',
342 fpro='fortran', cpro='c', cpppro='c++',
343 mpro='matlab', plpro='perl', shpro='bash',
344 sys='console', dat='python')
345
346
347
348
349
350
351 cpattern = re.compile(r'^!bc +ipy *\n(.*?)^!ec *\n',
352 re.DOTALL|re.MULTILINE)
353 filestr = cpattern.sub('\n\g<1>\n', filestr)
354
355
356
357
358
359 filestr, code_blocks, tex_blocks = remove_code_and_tex(filestr)
360 for i in range(len(code_blocks)):
361 code_blocks[i] = indent_lines(code_blocks[i], format)
362
363
364
365
366
367
368
369 math_labels = []
370 for i in range(len(tex_blocks)):
371 tex_blocks[i] = indent_lines(tex_blocks[i], format)
372
373
374 label_regex1 = fix_latex(r'\label\{(.+?)\}', application='match')
375 label_regex2 = fix_latex( r'label\{(.+?)\}', application='match')
376 for label_regex in (label_regex1, label_regex2):
377 labels = re.findall(label_regex, tex_blocks[i])
378 if len(labels) == 1:
379 math_labels.extend(labels)
380 tex_blocks[i] = ' :label: %s\n' % labels[0] + tex_blocks[i]
381 tex_blocks[i] = re.sub(label_regex, '', tex_blocks[i])
382
383
384 commands = [r'\begin{equation}',
385 r'\end{equation}',
386 r'\begin{equation*}',
387 r'\end{equation*}',
388 r'\begin{eqnarray}',
389 r'\end{eqnarray}',
390 r'\begin{eqnarray*}',
391 r'\end{eqnarray*}',
392 r'\begin{align}',
393 r'\end{align}',
394 r'\begin{align*}',
395 r'\end{align*}',
396 r'\begin{multline}',
397 r'\end{multline}',
398 r'\begin{multline*}',
399 r'\end{multline*}',
400 r'\begin{split}',
401 r'\end{split}',
402 r'\begin{gather}',
403 r'\end{gather}',
404 r'\begin{gather*}',
405 r'\end{gather*}',
406 r'\[',
407 r'\]',
408
409 r'\beqan',
410 r'\eeqan',
411 r'\beqa',
412 r'\eeqa',
413 r'\balnn',
414 r'\ealnn',
415 r'\baln',
416 r'\ealn',
417 r'\beq',
418 r'\eeq',
419 ]
420 for command in commands:
421 tex_blocks[i] = tex_blocks[i].replace(command, '')
422 tex_blocks[i] = re.sub('&\s*=\s*&', ' &= ', tex_blocks[i])
423
424 if '{alignat' in tex_blocks[i]:
425 print '\nWarning: the "alignat" environment will give errors in Sphinx:\n\n', tex_blocks[i], '\n'
426
427
428 for label in math_labels:
429 filestr = filestr.replace('(:ref:`%s`)' % label, ':eq:`%s`' % label)
430
431 filestr = insert_code_and_tex(filestr, code_blocks, tex_blocks, 'rst')
432
433
434 for key in envir2lang:
435 language = envir2lang[key]
436 if not language in legal_pygments_languages:
437 raise TypeError('%s is not a legal Pygments language '\
438 '(lexer) in line with:\n %s' % \
439 (language, defs_line))
440
441
442
443 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE)
444 filestr = cpattern.sub('\n.. code-block:: %s\n\n' % \
445 envir2lang[key], filestr)
446
447
448
449
450 cpattern = re.compile(r'^!bc.+$', flags=re.MULTILINE)
451 filestr = cpattern.sub('\n.. code-block:: py\n\n', filestr)
452
453 filestr = re.sub(r'!ec *\n', '\n\n', filestr)
454
455
456 filestr = re.sub(r'!bt *\n', '\n.. math::\n', filestr)
457 filestr = re.sub(r'!et *\n', '\n\n', filestr)
458
459 return filestr
460
462 filestr = ref_and_label_commoncode(section_label2title, format, filestr)
463
464
465 for label in section_label2title:
466 filestr = filestr.replace('ref{%s}' % label, ':ref:`%s`' % label)
467
468
469
470
471
472
473 filestr = re.sub(r'ref\{(.+?)\}', ':ref:`\g<1>`', filestr)
474
475 return filestr
476
478 filestr = rst_bib(filestr, citations, bibfile)
479
480 for word in index:
481 word2 = word.replace('`', '')
482 if not '!' in word:
483 filestr = filestr.replace('idx{%s}' % word,
484 '\n.. index:: ' + word2 + '\n')
485 else:
486 word3 = word2.replace('!', '; ')
487 filestr = filestr.replace('idx{%s}' % word,
488 '\n.. index::\n pair: ' + word3 + '\n')
489 return filestr
490
491
492 -def define(FILENAME_EXTENSION,
493 BLANKLINE,
494 INLINE_TAGS_SUBST,
495 CODE,
496 LIST,
497 ARGLIST,
498 TABLE,
499 EXERCISE,
500 FIGURE_EXT,
501 CROSS_REFS,
502 INDEX_BIB,
503 INTRO,
504 OUTRO):
505 if not 'rst' in BLANKLINE:
506
507 import rst
508 rst.define(FILENAME_EXTENSION,
509 BLANKLINE,
510 INLINE_TAGS_SUBST,
511 CODE,
512 LIST,
513 ARGLIST,
514 TABLE,
515 FIGURE_EXT,
516 INTRO,
517 OUTRO)
518
519 FILENAME_EXTENSION['sphinx'] = FILENAME_EXTENSION['rst']
520 BLANKLINE['sphinx'] = BLANKLINE['rst']
521 CODE['sphinx'] = CODE['rst']
522 LIST['sphinx'] = LIST['rst']
523 FIGURE_EXT['sphinx'] = ('.png', '.gif', '.jpg', '.jpeg')
524 CROSS_REFS['sphinx'] = sphinx_ref_and_label
525 INDEX_BIB['sphinx'] = sphinx_index_bib
526 TABLE['sphinx'] = TABLE['rst']
527 EXERCISE['sphinx'] = EXERCISE['rst']
528 INTRO['sphinx'] = INTRO['rst']
529
530
531 INLINE_TAGS_SUBST['sphinx'] = {}
532 for tag in INLINE_TAGS_SUBST['rst']:
533 INLINE_TAGS_SUBST['sphinx'][tag] = INLINE_TAGS_SUBST['rst'][tag]
534
535
536 INLINE_TAGS_SUBST['sphinx']['math'] = r'\g<begin>:math:`\g<subst>`\g<end>'
537 INLINE_TAGS_SUBST['sphinx']['math2'] = r'\g<begin>:math:`\g<latexmath>`\g<end>'
538 INLINE_TAGS_SUBST['sphinx']['figure'] = sphinx_figure
539 CODE['sphinx'] = sphinx_code
540
541 ARGLIST['sphinx'] = {
542 'parameter': ':param',
543 'keyword': ':keyword',
544 'return': ':return',
545 'instance variable': ':ivar',
546 'class variable': ':cvar',
547 'module variable': ':var',
548 }
549