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

46 statements  

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

1""" Atex heading (#, ##, ...) """ 

2from __future__ import annotations 

3 

4import logging 

5 

6from ..common.utils import isSpace 

7from .state_block import StateBlock 

8 

9LOGGER = logging.getLogger(__name__) 

10 

11 

12def heading(state: StateBlock, startLine: int, endLine: int, silent: bool): 

13 LOGGER.debug("entering heading: %s, %s, %s, %s", state, startLine, endLine, silent) 

14 

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

16 maximum = state.eMarks[startLine] 

17 

18 # if it's indented more than 3 spaces, it should be a code block 

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

20 return False 

21 

22 ch: int | None = state.srcCharCode[pos] 

23 

24 # /* # */ 

25 if ch != 0x23 or pos >= maximum: 

26 return False 

27 

28 # count heading level 

29 level = 1 

30 pos += 1 

31 try: 

32 ch = state.srcCharCode[pos] 

33 except IndexError: 

34 ch = None 

35 # /* # */ 

36 while ch == 0x23 and pos < maximum and level <= 6: 

37 level += 1 

38 pos += 1 

39 try: 

40 ch = state.srcCharCode[pos] 

41 except IndexError: 

42 ch = None 

43 

44 if level > 6 or (pos < maximum and not isSpace(ch)): 

45 return False 

46 

47 if silent: 

48 return True 

49 

50 # Let's cut tails like ' ### ' from the end of string 

51 

52 maximum = state.skipSpacesBack(maximum, pos) 

53 tmp = state.skipCharsBack(maximum, 0x23, pos) # # 

54 if tmp > pos and isSpace(state.srcCharCode[tmp - 1]): 

55 maximum = tmp 

56 

57 state.line = startLine + 1 

58 

59 token = state.push("heading_open", "h" + str(level), 1) 

60 token.markup = "########"[:level] 

61 token.map = [startLine, state.line] 

62 

63 token = state.push("inline", "", 0) 

64 token.content = state.src[pos:maximum].strip() 

65 token.map = [startLine, state.line] 

66 token.children = [] 

67 

68 token = state.push("heading_close", "h" + str(level), -1) 

69 token.markup = "########"[:level] 

70 

71 return True