Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/filters/markdown.py: 40%

25 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1"""Markdown filters 

2 

3This file contains a collection of utility filters for dealing with 

4markdown within Jinja templates. 

5""" 

6# Copyright (c) IPython Development Team. 

7# Distributed under the terms of the Modified BSD License. 

8 

9 

10import re 

11 

12try: 

13 from .markdown_mistune import markdown2html_mistune 

14 

15except ImportError as e: 

16 _mistune_import_error = e 

17 

18 def markdown2html_mistune(source: str) -> str: 

19 """mistune is unavailable, raise ImportError""" 

20 msg = f"markdown2html requires mistune: {_mistune_import_error}" 

21 raise ImportError(msg) 

22 

23 

24from .pandoc import convert_pandoc 

25 

26__all__ = [ 

27 "markdown2html", 

28 "markdown2html_pandoc", 

29 "markdown2html_mistune", 

30 "markdown2latex", 

31 "markdown2rst", 

32 "markdown2asciidoc", 

33] 

34 

35 

36def markdown2latex(source, markup="markdown", extra_args=None): 

37 """ 

38 Convert a markdown string to LaTeX via pandoc. 

39 

40 This function will raise an error if pandoc is not installed. 

41 Any error messages generated by pandoc are printed to stderr. 

42 

43 Parameters 

44 ---------- 

45 source : string 

46 Input string, assumed to be valid markdown. 

47 markup : string 

48 Markup used by pandoc's reader 

49 default : pandoc extended markdown 

50 (see https://pandoc.org/README.html#pandocs-markdown) 

51 

52 Returns 

53 ------- 

54 out : string 

55 Output as returned by pandoc. 

56 """ 

57 return convert_pandoc(source, markup, "latex", extra_args=extra_args) 

58 

59 

60def markdown2html_pandoc(source, extra_args=None): 

61 """ 

62 Convert a markdown string to HTML via pandoc. 

63 """ 

64 extra_args = extra_args or ["--mathjax"] 

65 return convert_pandoc(source, "markdown", "html", extra_args=extra_args) 

66 

67 

68def markdown2asciidoc(source, extra_args=None): 

69 """Convert a markdown string to asciidoc via pandoc""" 

70 extra_args = extra_args or ["--atx-headers"] 

71 asciidoc = convert_pandoc(source, "markdown", "asciidoc", extra_args=extra_args) 

72 # workaround for https://github.com/jgm/pandoc/issues/3068 

73 if "__" in asciidoc: 

74 asciidoc = re.sub(r"\b__([\w \n-]+)__([:,.\n\)])", r"_\1_\2", asciidoc) 

75 # urls / links: 

76 asciidoc = re.sub(r"\(__([\w\/-:\.]+)__\)", r"(_\1_)", asciidoc) 

77 

78 return asciidoc 

79 

80 

81# The mistune renderer is the default, because it's simple to depend on it 

82markdown2html = markdown2html_mistune 

83 

84 

85def markdown2rst(source, extra_args=None): 

86 """ 

87 Convert a markdown string to ReST via pandoc. 

88 

89 This function will raise an error if pandoc is not installed. 

90 Any error messages generated by pandoc are printed to stderr. 

91 

92 Parameters 

93 ---------- 

94 source : string 

95 Input string, assumed to be valid markdown. 

96 

97 Returns 

98 ------- 

99 out : string 

100 Output as returned by pandoc. 

101 """ 

102 return convert_pandoc(source, "markdown", "rst", extra_args=extra_args)