codekingpro/portable-devtools
114k
1# lheading (---, ==)2import logging3 4from .state_block import StateBlock5 6LOGGER = logging.getLogger(__name__)7 8 9def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:10 LOGGER.debug("entering lheading: %s, %s, %s, %s", state, startLine, endLine, silent)11 12 level = None13 nextLine = startLine + 114 ruler = state.md.block.ruler15 terminatorRules = ruler.getRules("paragraph")16 17 if state.is_code_block(startLine):18 return False19 20 oldParentType = state.parentType21 state.parentType = "paragraph" # use paragraph to match terminatorRules22 23 # jump line-by-line until empty one or EOF24 while nextLine < endLine and not state.isEmpty(nextLine):25 # this would be a code block normally, but after paragraph26 # it's considered a lazy continuation regardless of what's there27 if state.sCount[nextLine] - state.blkIndent > 3:28 nextLine += 129 continue30 31 # Check for underline in setext header32 if state.sCount[nextLine] >= state.blkIndent:33 pos = state.bMarks[nextLine] + state.tShift[nextLine]34 maximum = state.eMarks[nextLine]35 36 if pos < maximum:37 marker = state.src[pos]38 39 if marker in ("-", "="):40 pos = state.skipCharsStr(pos, marker)41 pos = state.skipSpaces(pos)42 43 # /* = */44 if pos >= maximum:45 level = 1 if marker == "=" else 246 break47 48 # quirk for blockquotes, this line should already be checked by that rule49 if state.sCount[nextLine] < 0:50 nextLine += 151 continue52 53 # Some tags can terminate paragraph without empty line.54 terminate = False55 for terminatorRule in terminatorRules:56 if terminatorRule(state, nextLine, endLine, True):57 terminate = True58 break59 if terminate:60 break61 62 nextLine += 163 64 if not level:65 # Didn't find valid underline66 return False67 68 content = state.getLines(startLine, nextLine, state.blkIndent, False).strip()69 70 state.line = nextLine + 171 72 token = state.push("heading_open", "h" + str(level), 1)73 token.markup = marker74 token.map = [startLine, state.line]75 76 token = state.push("inline", "", 0)77 token.content = content78 token.map = [startLine, state.line - 1]79 token.children = []80 81 token = state.push("heading_close", "h" + str(level), -1)82 token.markup = marker83 84 state.parentType = oldParentType85 86 return True87 