/usr/lib/python2.7/dist-packages/recommonmark/transform.py is in python-recommonmark 0.4.0+ds-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """Implement some common transforms on parsed AST."""
import os
import sys
from .states import DummyStateMachine
from docutils import nodes, transforms
from docutils.statemachine import StringList
class AutoStructify(transforms.Transform):
"""Automatically try to transform blocks to sphinx directives.
This class is designed to handle AST generated by CommonMarkParser.
"""
# set to a high priority so it can be applied first for markdown docs
default_priority = 1000
suffix_set = set(['md', 'rst'])
default_config = {
'enable_auto_doc_ref': True,
'auto_toc_tree_section': None,
'enable_auto_toc_tree': True,
'enable_eval_rst': True,
'enable_math': True,
'enable_inline_math': True,
'url_resolver': lambda x: x,
}
def parse_ref(self, ref):
"""Analyze the ref block, and return the information needed.
Parameters
----------
ref : nodes.reference
Returns
-------
result : tuple of (str, str, str)
The returned result is tuple of (title, uri, docpath).
title is the display title of the ref.
uri is the html uri of to the ref after resolve.
docpath is the absolute document path to the document, if
the target corresponds to an internal document, this can bex None
"""
assert isinstance(ref, nodes.reference)
title = None
if len(ref.children) == 0:
title = ref['name']
elif isinstance(ref.children[0], nodes.Text):
title = ref.children[0].astext()
uri = ref['refuri']
if uri.find('://') != -1:
return (title, uri, None)
anchor = None
arr = uri.split('#')
if len(arr) == 2:
anchor = arr[1]
if len(arr) > 2 or len(arr[0]) == 0:
return (title, uri, None)
uri = arr[0]
abspath = os.path.abspath(os.path.join(self.file_dir, uri))
relpath = os.path.relpath(abspath, self.root_dir)
suffix = abspath.rsplit('.', 1)
if len(suffix) == 2 and suffix[1] in AutoStructify.suffix_set and (
os.path.exists(abspath) and abspath.startswith(self.root_dir)):
docpath = '/' + relpath.rsplit('.', 1)[0]
# rewrite suffix to html, this is suboptimal
uri = docpath + '.html'
if anchor is None:
return (title, uri, docpath)
else:
return (title, uri + '#' + anchor, None)
else:
# use url resolver
if self.url_resolver:
uri = self.url_resolver(relpath)
if anchor:
uri += '#' + anchor
return (title, uri, None)
def auto_toc_tree(self, node):
"""Try to convert a list block to toctree in rst.
This function detects if the matches the condition and return
a converted toc tree node. The matching condition:
The list only contains one level, and only contains references
Parameters
----------
node: nodes.Sequential
A list node in the doctree
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
if not self.config['enable_auto_toc_tree']:
return None
# when auto_toc_tree_section is set
# only auto generate toctree under the specified section title
sec = self.config['auto_toc_tree_section']
if sec is not None:
if node.parent is None:
return None
if not isinstance(node.parent, nodes.section):
return None
title = node.parent.children[0]
if not isinstance(title, nodes.title):
return None
if title.astext().strip() != sec:
return None
numbered = None
if isinstance(node, nodes.bullet_list):
numbered = 0
elif isinstance(node, nodes.enumerated_list):
numbered = 1
if numbered is None:
return None
refs = []
for nd in node.children[:]:
assert isinstance(nd, nodes.list_item)
if len(nd.children) != 1:
return None
par = nd.children[0]
if not isinstance(par, nodes.paragraph):
return None
if len(par.children) != 1:
return None
ref = par.children[0]
if not isinstance(ref, nodes.reference):
return None
title, uri, docpath = self.parse_ref(ref)
if title is None or uri.startswith('#'):
return None
if docpath:
refs.append((title, docpath))
else:
refs.append((title, uri))
self.state_machine.reset(self.document,
node.parent,
self.current_level)
return self.state_machine.run_directive(
'toctree',
options={'maxdepth': 1, 'numbered': numbered},
content=['%s <%s>' % (k, v) for k, v in refs])
def auto_doc_ref(self, node):
"""Try to convert a reference to docref in rst.
Parameters
----------
node : nodes.reference
A reference node in doctree.
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
if not self.config['enable_auto_doc_ref']:
return None
assert isinstance(node, nodes.reference)
title, uri, docpath = self.parse_ref(node)
if title is None:
return None
if docpath:
content = u'%s <%s>' % (title, docpath)
self.state_machine.reset(self.document,
node.parent,
self.current_level)
return self.state_machine.run_role('doc', content=content)
else:
# inplace modify uri
node['refuri'] = uri
return None
def auto_inline_code(self, node):
"""Try to automatically generate nodes for inline literals.
Parameters
----------
node : nodes.literal
Original codeblock node
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
assert isinstance(node, nodes.literal)
if len(node.children) != 1:
return None
content = node.children[0]
if not isinstance(content, nodes.Text):
return None
content = content.astext().strip()
if content.startswith('$') and content.endswith('$'):
if not self.config['enable_inline_math']:
return None
content = content[1:-1]
self.state_machine.reset(self.document,
node.parent,
self.current_level)
return self.state_machine.run_role('math', content=content)
else:
return None
def auto_code_block(self, node):
"""Try to automatically generate nodes for codeblock syntax.
Parameters
----------
node : nodes.literal_block
Original codeblock node
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
assert isinstance(node, nodes.literal_block)
if 'language' not in node:
return None
self.state_machine.reset(self.document,
node.parent,
self.current_level)
content = node.rawsource.split('\n')
language = node['language']
if language == 'math':
if self.config['enable_math']:
return self.state_machine.run_directive(
'math', content=content)
elif language == 'eval_rst':
if self.config['enable_eval_rst']:
# allow embed non section level rst
node = nodes.section()
self.state_machine.state.nested_parse(
StringList(content, source=node.rawsource),
0, node=node, match_titles=False)
return node.children[:]
else:
return self.state_machine.run_directive(
'code-block', arguments=[language],
content=content)
return None
def find_replace(self, node):
"""Try to find replace node for current node.
Parameters
----------
node : docutil node
Node to find replacement for.
Returns
-------
nodes : node or list of node
The replacement nodes of current node.
Returns None if no replacement can be found.
"""
newnode = None
if isinstance(node, nodes.Sequential):
newnode = self.auto_toc_tree(node)
elif isinstance(node, nodes.reference):
newnode = self.auto_doc_ref(node)
elif isinstance(node, nodes.literal_block):
newnode = self.auto_code_block(node)
elif isinstance(node, nodes.literal):
newnode = self.auto_inline_code(node)
return newnode
def traverse(self, node):
"""Traverse the document tree rooted at node.
node : docutil node
current root node to traverse
"""
old_level = self.current_level
if isinstance(node, nodes.section):
if 'level' in node:
self.current_level = node['level']
to_visit = []
to_replace = []
for c in node.children[:]:
newnode = self.find_replace(c)
if newnode is not None:
to_replace.append((c, newnode))
else:
to_visit.append(c)
for oldnode, newnodes in to_replace:
node.replace(oldnode, newnodes)
for child in to_visit:
self.traverse(child)
self.current_level = old_level
def apply(self):
"""Apply the transformation by configuration."""
# only transform markdowns
source = self.document['source']
if not source.endswith('.md'):
return
self.reporter = self.document.reporter
self.reporter.info('AutoStructify: %s' % source)
config = self.default_config.copy()
try:
new_cfg = self.document.settings.env.config.recommonmark_config
config.update(new_cfg)
except:
self.reporter.warning('recommonmark_config not setted,'
' proceed default setting')
self.url_resolver = config['url_resolver']
assert callable(self.url_resolver)
self.config = config
self.state_machine = DummyStateMachine()
self.current_level = 0
self.file_dir = os.path.abspath(os.path.dirname(self.document['source']))
self.root_dir = os.path.abspath(self.document.settings.env.srcdir)
self.traverse(self.document)
|