forked from 7enderhead/antiweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathantisphinx.py
More file actions
152 lines (120 loc) · 3.52 KB
/
antisphinx.py
File metadata and controls
152 lines (120 loc) · 3.52 KB
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
#!python
__author__ = "Michael Reithinger, Philipp Rathmanner, Lukas Tanner, Philipp Grandits, and Christian Eitner"
__copyright__ = "Copyright 2017, antiweb team"
__license__ = "GPL"
__version__ = "0.9.1"
__maintainer__ = "antiweb team"
__email__ = "antiweb@freelists.org"
#@start()
"""
.. default-domain:: python
.. highlight:: python
:linenothreshold: 6
##########
antisphinx
##########
This sphinx extension modifies the syntax highlight mechanism to handle
<<textblock>> abbreviations in source code.
Additionally it makes them linking to the referring source block.
The primary technique is:
1. Extend the basic pygments language lexer with a new *Heading* token.
2. Filter the html output of pygment lexing process: Replacing the
heading's ``<span>`` tag by an ``<a>`` tag, referencing the
a block.
***********
File Layout
***********
@include(file layout)
@include(imports)
@include(export)
@include(Lexers)
@include(Filter Output)
"""
#@
#@start(file layout)
#@code
#@rstart(imports)
'''
<<imports>>
===========
'''
#@code
import sphinx.highlighting as shighlighting
import pygments.lexers as plexers
import pygments
import re
from pygments.token import Token
#@(imports)
#@rstart(export)
'''
***********
<<exports>>
***********
'''
#@indent 5
#@code
priority = 5
#@(export)
#@rstart(Lexers)
'''
<<Lexers>>
==========
'''
#@code
class CHeaderLexer(plexers.CLexer):
tokens = plexers.CLexer.tokens.copy()
tokens["whitespace"] = [ (r'(?m)^\s*<<.+>>\s*$', Token.Generic.Heading), ]\
+ plexers.CLexer.tokens["whitespace"]
CHeaderLexer._tokens = CHeaderLexer.process_tokendef('', CHeaderLexer.tokens)
class PythonHeaderLexer(plexers.PythonLexer):
tokens = plexers.PythonLexer.tokens.copy()
tokens["root"] = [ (r'^\s*<<.+>>\s*$', Token.Generic.Heading), ]\
+ plexers.PythonLexer.tokens["root"]
PythonHeaderLexer._tokens = PythonHeaderLexer.process_tokendef('', PythonHeaderLexer.tokens)
#replace the sphinx lexers by the new Lexers
shighlighting.lexers["c"] = CHeaderLexer()
shighlighting.lexers["python"] = PythonHeaderLexer()
#@(lexers)
#@rstart(Filter Output)
'''
<<Filter Output>>
=================
'''
#@code
re_html_heading = re.compile('<span class="gh">(.*?)</span>')
def highlight(code, lexer, formatter, outfile=None):
#@cstart(make anchor)
def make_anchor(mo):
indented_name = mo.group(1)
indent = len(indented_name)-len(indented_name.lstrip())
name = indented_name.strip()
#mangle the textblock name to satisfy the sphinx anchor names.
href = name.replace("<", "").replace(">", "")\
.replace(" ", "-").replace(":", "-").replace("+", "-")
if "." in href:
path = href.split(".")
href = path[0] + "." + ".".join(path[1:]).lower()
else:
href = href.replace("_", "-").lower()
if href.startswith("-"):
href = href[1:]
phref = None
while phref != href:
phref = href
href = href.replace("--", "-")
return '<span class="gh">%s<a href="#%s">%s</a></span>' \
% (indented_name[:indent], href, name)
#@
output = pygments.highlight(code, lexer, formatter, outfile)
output, noc = re_html_heading.subn(make_anchor, output)
return output
#monkey path the original sphinx highlighting
shighlighting.highlight = highlight
shighlighting.parser = None
def setup(app):
#is needed for sphinx extension mechanism
pass
#@edoc
#@rinclude(make anchor)
#@(Filter Output)
#@(file layout)