1# HTML block
2from __future__ import annotations
3
4import logging
5import re
6
7from ..common.html_blocks import block_names
8from ..common.html_re import HTML_OPEN_CLOSE_TAG_STR
9from .state_block import StateBlock
10
11LOGGER = logging.getLogger(__name__)
12
13# An array of opening and corresponding closing sequences for html tags,
14# last argument defines whether it can terminate a paragraph or not
15HTML_SEQUENCES: list[tuple[re.Pattern[str], re.Pattern[str], bool]] = [
16 (
17 re.compile(r"^<(script|pre|style|textarea)(?=(\s|>|$))", re.IGNORECASE),
18 re.compile(r"<\/(script|pre|style|textarea)>", re.IGNORECASE),
19 True,
20 ),
21 (re.compile(r"^<!--"), re.compile(r"-->"), True),
22 (re.compile(r"^<\?"), re.compile(r"\?>"), True),
23 (re.compile(r"^<![A-Z]"), re.compile(r">"), True),
24 (re.compile(r"^<!\[CDATA\["), re.compile(r"\]\]>"), True),
25 (
26 re.compile("^</?(" + "|".join(block_names) + ")(?=(\\s|/?>|$))", re.IGNORECASE),
27 re.compile(r"^$"),
28 True,
29 ),
30 (re.compile(HTML_OPEN_CLOSE_TAG_STR + "\\s*$"), re.compile(r"^$"), False),
31]
32
33
34def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
35 LOGGER.debug(
36 "entering html_block: %s, %s, %s, %s", state, startLine, endLine, silent
37 )
38 pos = state.bMarks[startLine] + state.tShift[startLine]
39 maximum = state.eMarks[startLine]
40
41 if state.is_code_block(startLine):
42 return False
43
44 if not state.md.options.get("html", None):
45 return False
46
47 try:
48 if state.src[pos] != "<":
49 return False
50 except IndexError:
51 return False
52
53 lineText = state.src[pos:maximum]
54
55 html_seq = None
56 for HTML_SEQUENCE in HTML_SEQUENCES:
57 if HTML_SEQUENCE[0].search(lineText):
58 html_seq = HTML_SEQUENCE
59 break
60
61 if not html_seq:
62 return False
63
64 if silent:
65 # true if this sequence can be a terminator, false otherwise
66 return html_seq[2]
67
68 nextLine = startLine + 1
69
70 # If we are here - we detected HTML block.
71 # Let's roll down till block end.
72 if not html_seq[1].search(lineText):
73 while nextLine < endLine:
74 if state.sCount[nextLine] < state.blkIndent:
75 break
76
77 pos = state.bMarks[nextLine] + state.tShift[nextLine]
78 maximum = state.eMarks[nextLine]
79 lineText = state.src[pos:maximum]
80
81 if html_seq[1].search(lineText):
82 if len(lineText) != 0:
83 nextLine += 1
84 break
85 nextLine += 1
86
87 state.line = nextLine
88
89 token = state.push("html_block", "", 0)
90 token.map = [startLine, nextLine]
91 token.content = state.getLines(startLine, nextLine, state.blkIndent, True)
92
93 return True