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

46 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:07 +0000

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, re.Pattern, 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): 

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 it's indented more than 3 spaces, it should be a code block 

42 if state.sCount[startLine] - state.blkIndent >= 4: 

43 return False 

44 

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

46 return False 

47 

48 if state.srcCharCode[pos] != 0x3C: # /* < */ 

49 return False 

50 

51 lineText = state.src[pos:maximum] 

52 

53 html_seq = None 

54 for HTML_SEQUENCE in HTML_SEQUENCES: 

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

56 html_seq = HTML_SEQUENCE 

57 break 

58 

59 if not html_seq: 

60 return False 

61 

62 if silent: 

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

64 return html_seq[2] 

65 

66 nextLine = startLine + 1 

67 

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

69 # Let's roll down till block end. 

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

71 while nextLine < endLine: 

72 if state.sCount[nextLine] < state.blkIndent: 

73 break 

74 

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

76 maximum = state.eMarks[nextLine] 

77 lineText = state.src[pos:maximum] 

78 

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

80 if len(lineText) != 0: 

81 nextLine += 1 

82 break 

83 nextLine += 1 

84 

85 state.line = nextLine 

86 

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

88 token.map = [startLine, nextLine] 

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

90 

91 return True