Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Helpers for markdown functionalities, it requires dependencies on :epkg:`mistune`.
4"""
5import re
8def parse_markdown(text):
9 """
10 parse markdown text and return the markdown object
12 @param text markdown text
13 @return
14 """
15 import mistune
16 markdown = mistune.Markdown()
17 r = markdown(text)
18 return r
21def yield_sphinx_only_markup_for_pipy(lines):
22 """
23 Code from `My rst README is not formatted on pypi.python.org
24 <http://stackoverflow.com/questions/16367770/my-rst-readme-is-not-formatted-on-pypi-python-org>`_.
26 :param lines: lines to process
27 """
28 substs = [
29 # Selected Sphinx-only Roles.
30 #
31 (':abbr:`([^`]+)`', '\\1'),
32 (':ref:`([^`]+)`', '`\\1`_'),
33 (':term:`([^`]+)`', '**\\1**'),
34 (':dfn:`([^`]+)`', '**\\1**'),
35 (':(samp|guilabel|menuselection):`([^`]+)`', '``\\2``'),
37 # Sphinx-only roles:
38 # :foo:`bar` --> foo(``bar``)
39 # :a:foo:`bar` XXX afoo(``bar``)
40 #
41 #(r'(:(\w+))?:(\w+):`([^`]*)`', r'\2\3(``\4``)'),
42 (':(\\w+):`([^`]*)`', '\\1(``\\2``)'),
44 # Sphinx-only Directives.
45 #
46 ('\\.\\. doctest', 'code-block'),
47 ('\\.\\. plot::', '.. '),
48 ('\\.\\. seealso', 'info'),
49 ('\\.\\. glossary', 'rubric'),
50 ('\\.\\. figure::', '.. '),
52 # Other
53 #
54 ('\\|version\\|', 'x.x.x'),
55 ]
57 regex_subs = [(re.compile(regex, re.IGNORECASE), sub)
58 for (regex, sub) in substs]
60 def clean_line(line):
61 try:
62 for (regex, sub) in regex_subs:
63 line = regex.sub(sub, line)
64 except Exception as ex: # pragma: no cover
65 raise RuntimeError(
66 "[sphinxerror]-A %s, (line(%s)" % (
67 regex, sub)) from ex
69 return line
71 for line in lines:
72 yield clean_line(line)