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
|
from typing import Optional
from sybil.parsers.abstract import AbstractCodeBlockParser
from sybil.typing import Evaluator
from ..abstract.codeblock import PythonDocTestOrCodeBlockParser
from ..markdown.lexers import FencedCodeBlockLexer, DirectiveInHTMLCommentLexer
class CodeBlockParser(AbstractCodeBlockParser):
"""
A :any:`Parser` for :ref:`markdown-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'},
),
DirectiveInHTMLCommentLexer(
directive=r'(invisible-)?code(-block)?',
arguments='.+',
),
],
language, evaluator
)
class PythonCodeBlockParser(PythonDocTestOrCodeBlockParser):
"""
A :any:`Parser` for Python :ref:`markdown-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
|