quizcomp.parser.renderer.markdown

 1import argparse
 2import typing
 3
 4import markdown_it
 5import mdformat.plugins
 6import mdformat.renderer
 7
 8import quizcomp.parser.common
 9import quizcomp.parser.math
10
11def math_inline(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
12    """ Render inline math. """
13
14    context = typing.cast(quizcomp.parser.common.RenderContext, md_context.env[quizcomp.parser.common.ENV_KEY_CONTEXT])
15    return quizcomp.parser.math._render_md(node.content, True, context)
16
17def math_block(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
18    """ Render a math block. """
19
20    context = typing.cast(quizcomp.parser.common.RenderContext, md_context.env[quizcomp.parser.common.ENV_KEY_CONTEXT])
21    return quizcomp.parser.math._render_md(node.content, False, context)
22
23def placeholder(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
24    """ Render an answer placeholder. """
25
26    return f"<placeholder>{node.content}</placeholder>"
27
28def container_block(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
29    """ Render a container block. """
30
31    # We can ignore blocks when outputting markdown (since it is non-standard).
32    # Just render the child node (there should only be one).
33    if ((node.children is None) or (len(node.children) == 0)):
34        return ''
35
36    parts = []
37    for child in node.children:
38        parts.append(child.render(md_context))
39
40    return "\n\n".join(parts)
41
42class QuizComposerMDformatExtension(mdformat.plugins.ParserExtensionInterface):
43    """ A plugin for out Markdown extensions. """
44
45    CHANGES_AST = False
46    POSTPROCESSORS = {}
47    RENDERERS = {
48        'container_block': container_block,
49        'math_block': math_block,
50        'math_inline': math_inline,
51        'placeholder': placeholder,
52    }
53
54    @staticmethod
55    def add_cli_options(parser: argparse.ArgumentParser) -> None:
56        return
57
58    @staticmethod
59    def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
60        return
61
62    @staticmethod
63    def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
64        return
65
66def get_renderer(options: markdown_it.utils.OptionsDict) -> mdformat.renderer.MDRenderer:
67    """ Get this renderer and options. """
68
69    extensions = options.get('parser_extension', [])
70    extensions += [
71        mdformat.plugins.PARSER_EXTENSIONS['tables'],
72        QuizComposerMDformatExtension(),
73    ]
74    options['parser_extension'] = extensions
75
76    return mdformat.renderer.MDRenderer()
def math_inline( node: mdformat.renderer._tree.RenderTreeNode, md_context: mdformat.renderer._context.RenderContext) -> str:
12def math_inline(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
13    """ Render inline math. """
14
15    context = typing.cast(quizcomp.parser.common.RenderContext, md_context.env[quizcomp.parser.common.ENV_KEY_CONTEXT])
16    return quizcomp.parser.math._render_md(node.content, True, context)

Render inline math.

def math_block( node: mdformat.renderer._tree.RenderTreeNode, md_context: mdformat.renderer._context.RenderContext) -> str:
18def math_block(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
19    """ Render a math block. """
20
21    context = typing.cast(quizcomp.parser.common.RenderContext, md_context.env[quizcomp.parser.common.ENV_KEY_CONTEXT])
22    return quizcomp.parser.math._render_md(node.content, False, context)

Render a math block.

def placeholder( node: mdformat.renderer._tree.RenderTreeNode, md_context: mdformat.renderer._context.RenderContext) -> str:
24def placeholder(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
25    """ Render an answer placeholder. """
26
27    return f"<placeholder>{node.content}</placeholder>"

Render an answer placeholder.

def container_block( node: mdformat.renderer._tree.RenderTreeNode, md_context: mdformat.renderer._context.RenderContext) -> str:
29def container_block(node: mdformat.renderer.RenderTreeNode, md_context: mdformat.renderer.RenderContext) -> str:
30    """ Render a container block. """
31
32    # We can ignore blocks when outputting markdown (since it is non-standard).
33    # Just render the child node (there should only be one).
34    if ((node.children is None) or (len(node.children) == 0)):
35        return ''
36
37    parts = []
38    for child in node.children:
39        parts.append(child.render(md_context))
40
41    return "\n\n".join(parts)

Render a container block.

class QuizComposerMDformatExtension(mdformat.plugins.ParserExtensionInterface):
43class QuizComposerMDformatExtension(mdformat.plugins.ParserExtensionInterface):
44    """ A plugin for out Markdown extensions. """
45
46    CHANGES_AST = False
47    POSTPROCESSORS = {}
48    RENDERERS = {
49        'container_block': container_block,
50        'math_block': math_block,
51        'math_inline': math_inline,
52        'placeholder': placeholder,
53    }
54
55    @staticmethod
56    def add_cli_options(parser: argparse.ArgumentParser) -> None:
57        return
58
59    @staticmethod
60    def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
61        return
62
63    @staticmethod
64    def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
65        return

A plugin for out Markdown extensions.

CHANGES_AST = False
POSTPROCESSORS = {}
RENDERERS = {'container_block': <function container_block>, 'math_block': <function math_block>, 'math_inline': <function math_inline>, 'placeholder': <function placeholder>}
@staticmethod
def add_cli_options(parser: argparse.ArgumentParser) -> None:
55    @staticmethod
56    def add_cli_options(parser: argparse.ArgumentParser) -> None:
57        return

DEPRECATED - use add_cli_argument_group instead.

Add options to the mdformat CLI, to be stored in mdit.options["mdformat"] (optional)

@staticmethod
def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
59    @staticmethod
60    def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
61        return

Add an argument group to mdformat CLI and add arguments to it.

Call group.add_argument() to add CLI arguments (signature is the same as argparse.ArgumentParser.add_argument). Values will be stored in a mapping under mdit.options["mdformat"]["plugin"][] where equals entry point name of the plugin.

The mapping will be merged with values read from TOML config file section [plugin.].

(optional)

@staticmethod
def update_mdit(mdit: markdown_it.main.MarkdownIt) -> None:
63    @staticmethod
64    def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
65        return

Update the parser, e.g. by adding a plugin: mdit.use(myplugin)

def get_renderer(options: markdown_it.utils.OptionsDict) -> mdformat.renderer.MDRenderer:
67def get_renderer(options: markdown_it.utils.OptionsDict) -> mdformat.renderer.MDRenderer:
68    """ Get this renderer and options. """
69
70    extensions = options.get('parser_extension', [])
71    extensions += [
72        mdformat.plugins.PARSER_EXTENSIONS['tables'],
73        QuizComposerMDformatExtension(),
74    ]
75    options['parser_extension'] = extensions
76
77    return mdformat.renderer.MDRenderer()

Get this renderer and options.