Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
parser_block.py112 linesDownload Raw Back to markdown_it
1"""Block-level tokenizer."""2from __future__ import annotations3 4import logging5from typing import TYPE_CHECKING, Callable6 7from . import rules_block8from .ruler import Ruler9from .rules_block.state_block import StateBlock10from .token import Token11from .utils import EnvType12 13if TYPE_CHECKING:14    from markdown_it import MarkdownIt15 16LOGGER = logging.getLogger(__name__)17 18 19RuleFuncBlockType = Callable[[StateBlock, int, int, bool], bool]20"""(state: StateBlock, startLine: int, endLine: int, silent: bool) -> matched: bool)21 22`silent` disables token generation, useful for lookahead.23"""24 25_rules: list[tuple[str, RuleFuncBlockType, list[str]]] = [26    # First 2 params - rule name & source. Secondary array - list of rules,27    # which can be terminated by this one.28    ("table", rules_block.table, ["paragraph", "reference"]),29    ("code", rules_block.code, []),30    ("fence", rules_block.fence, ["paragraph", "reference", "blockquote", "list"]),31    (32        "blockquote",33        rules_block.blockquote,34        ["paragraph", "reference", "blockquote", "list"],35    ),36    ("hr", rules_block.hr, ["paragraph", "reference", "blockquote", "list"]),37    ("list", rules_block.list_block, ["paragraph", "reference", "blockquote"]),38    ("reference", rules_block.reference, []),39    ("html_block", rules_block.html_block, ["paragraph", "reference", "blockquote"]),40    ("heading", rules_block.heading, ["paragraph", "reference", "blockquote"]),41    ("lheading", rules_block.lheading, []),42    ("paragraph", rules_block.paragraph, []),43]44 45 46class ParserBlock:47    """48    ParserBlock#ruler -> Ruler49 50    [[Ruler]] instance. Keep configuration of block rules.51    """52 53    def __init__(self) -> None:54        self.ruler = Ruler[RuleFuncBlockType]()55        for name, rule, alt in _rules:56            self.ruler.push(name, rule, {"alt": alt})57 58    def tokenize(self, state: StateBlock, startLine: int, endLine: int) -> None:59        """Generate tokens for input range."""60        rules = self.ruler.getRules("")61        line = startLine62        maxNesting = state.md.options.maxNesting63        hasEmptyLines = False64 65        while line < endLine:66            state.line = line = state.skipEmptyLines(line)67            if line >= endLine:68                break69            if state.sCount[line] < state.blkIndent:70                # Termination condition for nested calls.71                # Nested calls currently used for blockquotes & lists72                break73            if state.level >= maxNesting:74                # If nesting level exceeded - skip tail to the end.75                # That's not ordinary situation and we should not care about content.76                state.line = endLine77                break78 79            # Try all possible rules.80            # On success, rule should:81            # - update `state.line`82            # - update `state.tokens`83            # - return True84            for rule in rules:85                if rule(state, line, endLine, False):86                    break87 88            # set state.tight if we had an empty line before current tag89            # i.e. latest empty line should not count90            state.tight = not hasEmptyLines91 92            line = state.line93 94            # paragraph might "eat" one newline after it in nested lists95            if (line - 1) < endLine and state.isEmpty(line - 1):96                hasEmptyLines = True97 98            if line < endLine and state.isEmpty(line):99                hasEmptyLines = True100                line += 1101                state.line = line102 103    def parse(104        self, src: str, md: MarkdownIt, env: EnvType, outTokens: list[Token]105    ) -> list[Token] | None:106        """Process input string and push block tokens into `outTokens`."""107        if not src:108            return None109        state = StateBlock(src, md, env, outTokens)110        self.tokenize(state, state.line, state.lineMax)111        return state.tokens112 
codekingpro/portable-devtools · Team Ai