Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/markdown_it/rules_block/html_block.py: 92%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

51 statements  

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"^<(?a:(script|pre|style|textarea))(?=(\s|>|$))", re.IGNORECASE), 

18 re.compile(r"<\/(?a:(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-Za-z]"), re.compile(r">"), True), 

24 (re.compile(r"^<!\[CDATA\["), re.compile(r"\]\]>"), True), 

25 ( 

26 re.compile( 

27 "^</?(?a:(" + "|".join(block_names) + "))(?=(\\s|/?>|$))", re.IGNORECASE 

28 ), 

29 re.compile(r"^$"), 

30 True, 

31 ), 

32 (re.compile(HTML_OPEN_CLOSE_TAG_STR + "\\s*$"), re.compile(r"^$"), False), 

33] 

34 

35 

36def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool: 

37 LOGGER.debug( 

38 "entering html_block: %s, %s, %s, %s", state, startLine, endLine, silent 

39 ) 

40 pos = state.bMarks[startLine] + state.tShift[startLine] 

41 maximum = state.eMarks[startLine] 

42 

43 if state.is_code_block(startLine): 

44 return False 

45 

46 if not state.md.options.get("html", None): 

47 return False 

48 

49 try: 

50 if state.src[pos] != "<": 

51 return False 

52 except IndexError: 

53 return False 

54 

55 lineText = state.src[pos:maximum] 

56 

57 html_seq = None 

58 for HTML_SEQUENCE in HTML_SEQUENCES: 

59 if HTML_SEQUENCE[0].search(lineText): 

60 html_seq = HTML_SEQUENCE 

61 break 

62 

63 if not html_seq: 

64 return False 

65 

66 if silent: 

67 # true if this sequence can be a terminator, false otherwise 

68 return html_seq[2] 

69 

70 nextLine = startLine + 1 

71 

72 # Block types 6 and 7 (the only ones whose end condition is a blank line) 

73 # have `^$` as their closing regexp. For all other types (1-5, e.g. 

74 # `<!--` comments), a blank line is regular content and must not terminate 

75 # the block - it ends only when its closing sequence is found. 

76 endsOnBlankLine = bool(html_seq[1].search("")) 

77 

78 # If we are here - we detected HTML block. 

79 # Let's roll down till block end. 

80 if not html_seq[1].search(lineText): 

81 while nextLine < endLine: 

82 # An outdented blank line shouldn't end a block that doesn't end on a 

83 # blank line (e.g. a `<!--` comment inside a list item). Such blocks 

84 # must continue until their closing sequence regardless of indent. 

85 if state.sCount[nextLine] < state.blkIndent and ( 

86 endsOnBlankLine or not state.isEmpty(nextLine) 

87 ): 

88 break 

89 

90 pos = state.bMarks[nextLine] + state.tShift[nextLine] 

91 maximum = state.eMarks[nextLine] 

92 lineText = state.src[pos:maximum] 

93 

94 if html_seq[1].search(lineText): 

95 if len(lineText) != 0: 

96 nextLine += 1 

97 break 

98 nextLine += 1 

99 

100 state.line = nextLine 

101 

102 token = state.push("html_block", "", 0) 

103 token.map = [startLine, nextLine] 

104 token.content = state.getLines(startLine, nextLine, state.blkIndent, True) 

105 

106 return True