Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/filters/markdown.py: 42%
24 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +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
14except ImportError as e:
15 # store in variable for Python 3
16 _mistune_import_error = e
18 def markdown2html_mistune(source):
19 """mistune is unavailable, raise ImportError"""
20 raise ImportError("markdown2html requires mistune: %s" % _mistune_import_error)
23from .pandoc import convert_pandoc
25__all__ = [
26 "markdown2html",
27 "markdown2html_pandoc",
28 "markdown2html_mistune",
29 "markdown2latex",
30 "markdown2rst",
31 "markdown2asciidoc",
32]
35def markdown2latex(source, markup="markdown", extra_args=None):
36 """
37 Convert a markdown string to LaTeX via pandoc.
39 This function will raise an error if pandoc is not installed.
40 Any error messages generated by pandoc are printed to stderr.
42 Parameters
43 ----------
44 source : string
45 Input string, assumed to be valid markdown.
46 markup : string
47 Markup used by pandoc's reader
48 default : pandoc extended markdown
49 (see https://pandoc.org/README.html#pandocs-markdown)
51 Returns
52 -------
53 out : string
54 Output as returned by pandoc.
55 """
56 return convert_pandoc(source, markup, "latex", extra_args=extra_args)
59def markdown2html_pandoc(source, extra_args=None):
60 """
61 Convert a markdown string to HTML via pandoc.
62 """
63 extra_args = extra_args or ["--mathjax"]
64 return convert_pandoc(source, "markdown", "html", extra_args=extra_args)
67def markdown2asciidoc(source, extra_args=None):
68 """Convert a markdown string to asciidoc via pandoc"""
69 extra_args = extra_args or ["--atx-headers"]
70 asciidoc = convert_pandoc(source, "markdown", "asciidoc", extra_args=extra_args)
71 # workaround for https://github.com/jgm/pandoc/issues/3068
72 if "__" in asciidoc:
73 asciidoc = re.sub(r"\b__([\w \n-]+)__([:,.\n\)])", r"_\1_\2", asciidoc)
74 # urls / links:
75 asciidoc = re.sub(r"\(__([\w\/-:\.]+)__\)", r"(_\1_)", asciidoc)
77 return asciidoc
80# The mistune renderer is the default, because it's simple to depend on it
81markdown2html = markdown2html_mistune
84def markdown2rst(source, extra_args=None):
85 """
86 Convert a markdown string to ReST via pandoc.
88 This function will raise an error if pandoc is not installed.
89 Any error messages generated by pandoc are printed to stderr.
91 Parameters
92 ----------
93 source : string
94 Input string, assumed to be valid markdown.
96 Returns
97 -------
98 out : string
99 Output as returned by pandoc.
100 """
101 return convert_pandoc(source, "markdown", "rst", extra_args=extra_args)