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
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Markdown filters
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.
10import re
12try:
13 from .markdown_mistune import markdown2html_mistune
15except ImportError as e:
16 _mistune_import_error = e
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)
24from .pandoc import convert_pandoc
26__all__ = [
27 "markdown2html",
28 "markdown2html_pandoc",
29 "markdown2html_mistune",
30 "markdown2latex",
31 "markdown2rst",
32 "markdown2asciidoc",
33]
36def markdown2latex(source, markup="markdown", extra_args=None):
37 """
38 Convert a markdown string to LaTeX via pandoc.
40 This function will raise an error if pandoc is not installed.
41 Any error messages generated by pandoc are printed to stderr.
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)
52 Returns
53 -------
54 out : string
55 Output as returned by pandoc.
56 """
57 return convert_pandoc(source, markup, "latex", extra_args=extra_args)
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)
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)
78 return asciidoc
81# The mistune renderer is the default, because it's simple to depend on it
82markdown2html = markdown2html_mistune
85def markdown2rst(source, extra_args=None):
86 """
87 Convert a markdown string to ReST via pandoc.
89 This function will raise an error if pandoc is not installed.
90 Any error messages generated by pandoc are printed to stderr.
92 Parameters
93 ----------
94 source : string
95 Input string, assumed to be valid markdown.
97 Returns
98 -------
99 out : string
100 Output as returned by pandoc.
101 """
102 return convert_pandoc(source, "markdown", "rst", extra_args=extra_args)