codekingpro/portable-devtools
114k
1"""Atex heading (#, ##, ...)"""2 3from __future__ import annotations4 5import logging6 7from ..common.utils import isStrSpace8from .state_block import StateBlock9 10LOGGER = logging.getLogger(__name__)11 12 13def heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:14 LOGGER.debug("entering heading: %s, %s, %s, %s", state, startLine, endLine, silent)15 16 pos = state.bMarks[startLine] + state.tShift[startLine]17 maximum = state.eMarks[startLine]18 19 if state.is_code_block(startLine):20 return False21 22 ch: str | None = state.src[pos]23 24 if ch != "#" or pos >= maximum:25 return False26 27 # count heading level28 level = 129 pos += 130 try:31 ch = state.src[pos]32 except IndexError:33 ch = None34 while ch == "#" and pos < maximum and level <= 6:35 level += 136 pos += 137 try:38 ch = state.src[pos]39 except IndexError:40 ch = None41 42 if level > 6 or (pos < maximum and not isStrSpace(ch)):43 return False44 45 if silent:46 return True47 48 # Let's cut tails like ' ### ' from the end of string49 50 maximum = state.skipSpacesBack(maximum, pos)51 tmp = state.skipCharsStrBack(maximum, "#", pos)52 if tmp > pos and isStrSpace(state.src[tmp - 1]):53 maximum = tmp54 55 state.line = startLine + 156 57 token = state.push("heading_open", "h" + str(level), 1)58 token.markup = "########"[:level]59 token.map = [startLine, state.line]60 61 token = state.push("inline", "", 0)62 token.content = state.src[pos:maximum].strip()63 token.map = [startLine, state.line]64 token.children = []65 66 token = state.push("heading_close", "h" + str(level), -1)67 token.markup = "########"[:level]68 69 return True70 