-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatches.py
52 lines (43 loc) · 1.74 KB
/
patches.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
# -*- coding: utf-8 -*-
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.body import ParsedLiteral
from docutils import nodes
from sphinx import environment as env
from sphinx.writers import html
from sphinx.highlighting import lexers
from pygments.lexers.web import PhpLexer
# Add "parsed-code" directive that combines both the features
# of "parsed-literal" (support for inline markup) and "sourcecode"
# (syntax highlighting).
class ParsedCodeBlock(ParsedLiteral):
"""
Directive for a code block with special highlighting or line numbering
settings that parses its content first (like "parsed-literal").
"""
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
'linenos': directives.flag,
'class': directives.class_option,
}
def run(self):
res = ParsedLiteral.run(self)
res[0]['language'] = self.arguments[0]
res[0]['linenos'] = 'linenos' in self.options
res[0]['highlight'] = True
return res
def setup(app):
# Monkey-patch html.HTMLTranslator.visit_literal_block so that
# a node with the "highlight" attribute is always highlighted.
old_visit = html.HTMLTranslator.visit_literal_block
def new_visit(self, node):
if node.get('highlight'):
node.rawsource = node.astext()
return old_visit(self, node)
html.HTMLTranslator.visit_literal_block = new_visit
directives.register_directive('parsed-code', ParsedCodeBlock)
# Add "project" to the default substitutions.
env.default_substitutions = set(['version', 'release', 'today', 'project'])
# Additionnal lexer for inline PHP code blocks.
lexers['inline-php'] = PhpLexer(startinline=True)