Coverage for pyquickhelper/helpgen/markdown_helper.py: 89%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1""" 

2@file 

3@brief Helpers for markdown functionalities, it requires dependencies on :epkg:`mistune`. 

4""" 

5import re 

6 

7 

8def parse_markdown(text): 

9 """ 

10 parse markdown text and return the markdown object 

11 

12 @param text markdown text 

13 @return 

14 """ 

15 import mistune 

16 try: 

17 markdown = mistune.create_markdown(escape=False) 

18 except TypeError: 

19 # older version of mistune 

20 markdown = mistune.Markdown() # pylint: disable=E1120 

21 r = markdown(text) 

22 return r 

23 

24 

25def yield_sphinx_only_markup_for_pipy(lines): 

26 """ 

27 Code from `My rst README is not formatted on pypi.python.org 

28 <http://stackoverflow.com/questions/16367770/my-rst-readme-is-not-formatted-on-pypi-python-org>`_. 

29 

30 :param lines: lines to process 

31 """ 

32 substs = [ 

33 # Selected Sphinx-only Roles. 

34 # 

35 (':abbr:`([^`]+)`', '\\1'), 

36 (':ref:`([^`]+)`', '`\\1`_'), 

37 (':term:`([^`]+)`', '**\\1**'), 

38 (':dfn:`([^`]+)`', '**\\1**'), 

39 (':(samp|guilabel|menuselection):`([^`]+)`', '``\\2``'), 

40 

41 # Sphinx-only roles: 

42 # :foo:`bar` --> foo(``bar``) 

43 # :a:foo:`bar` XXX afoo(``bar``) 

44 # 

45 # (r'(:(\w+))?:(\w+):`([^`]*)`', r'\2\3(``\4``)'), 

46 (':(\\w+):`([^`]*)`', '\\1(``\\2``)'), 

47 

48 # Sphinx-only Directives. 

49 # 

50 ('\\.\\. doctest', 'code-block'), 

51 ('\\.\\. plot::', '.. '), 

52 ('\\.\\. seealso', 'info'), 

53 ('\\.\\. glossary', 'rubric'), 

54 ('\\.\\. figure::', '.. '), 

55 

56 # Other 

57 # 

58 ('\\|version\\|', 'x.x.x'), 

59 ] 

60 

61 regex_subs = [(re.compile(regex, re.IGNORECASE), sub) 

62 for (regex, sub) in substs] 

63 

64 def clean_line(line): 

65 try: 

66 for (regex, sub) in regex_subs: 

67 line = regex.sub(sub, line) 

68 except Exception as ex: # pragma: no cover 

69 raise RuntimeError( 

70 f"[sphinxerror]-A {regex}, (line({sub})") from ex 

71 

72 return line 

73 

74 for line in lines: 

75 yield clean_line(line)