File: codeblock.py

package info (click to toggle)
python-sybil 9.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,164 kB
  • sloc: python: 4,545; makefile: 90
file content (64 lines) | stat: -rw-r--r-- 2,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Optional

from sybil.parsers.abstract import AbstractCodeBlockParser
from sybil.typing import Evaluator
from .lexers import (
    DirectiveLexer, DirectiveInPercentCommentLexer
)
from ..markdown.lexers import FencedCodeBlockLexer, DirectiveInHTMLCommentLexer
from ..abstract.codeblock import PythonDocTestOrCodeBlockParser


class CodeBlockParser(AbstractCodeBlockParser):
    """
    A :any:`Parser` for :ref:`myst-codeblock-parser` examples.

    :param language:
        The language that this parser should look for.

    :param evaluator:
        The evaluator to use for evaluating code blocks in the specified language.
        You can also override the :meth:`evaluate` method below.
    """

    def __init__(
            self, language: Optional[str] = None, evaluator: Optional[Evaluator] = None
    ) -> None:
        super().__init__(
            [
                FencedCodeBlockLexer(
                    language=r'.+',
                    mapping={'language': 'arguments', 'source': 'source'},
                ),
                DirectiveLexer(
                    directive=r'(sourcecode|code-block|code-cell|code)',
                    arguments='.+',
                ),
                DirectiveInPercentCommentLexer(
                    directive=r'(invisible-)?code(-block)?',
                    arguments='.+',
                ),
                DirectiveInHTMLCommentLexer(
                    directive=r'(invisible-)?code(-block)?',
                    arguments='.+',
                ),
            ],
            language, evaluator
        )


class PythonCodeBlockParser(PythonDocTestOrCodeBlockParser):
    """
    A :any:`Parser` for Python :ref:`myst-codeblock-parser` examples.

    :param future_imports:
        An optional list of strings that will be turned into
        ``from __future__ import ...`` statements and prepended to the code
        in each of the examples found by this parser.

    :param doctest_optionflags:
        :ref:`doctest option flags<option-flags-and-directives>` to use
        when evaluating the doctest examples found by this parser.
    """

    codeblock_parser_class = CodeBlockParser