codekingpro/portable-devtools
114k
1# Parse backticks2import re3 4from .state_inline import StateInline5 6regex = re.compile("^ (.+) $")7 8 9def backtick(state: StateInline, silent: bool) -> bool:10 pos = state.pos11 12 if state.src[pos] != "`":13 return False14 15 start = pos16 pos += 117 maximum = state.posMax18 19 # scan marker length20 while pos < maximum and (state.src[pos] == "`"):21 pos += 122 23 marker = state.src[start:pos]24 openerLength = len(marker)25 26 if state.backticksScanned and state.backticks.get(openerLength, 0) <= start:27 if not silent:28 state.pending += marker29 state.pos += openerLength30 return True31 32 matchStart = matchEnd = pos33 34 # Nothing found in the cache, scan until the end of the line (or until marker is found)35 while True:36 try:37 matchStart = state.src.index("`", matchEnd)38 except ValueError:39 break40 matchEnd = matchStart + 141 42 # scan marker length43 while matchEnd < maximum and (state.src[matchEnd] == "`"):44 matchEnd += 145 46 closerLength = matchEnd - matchStart47 48 if closerLength == openerLength:49 # Found matching closer length.50 if not silent:51 token = state.push("code_inline", "code", 0)52 token.markup = marker53 token.content = state.src[pos:matchStart].replace("\n", " ")54 if (55 token.content.startswith(" ")56 and token.content.endswith(" ")57 and len(token.content.strip()) > 058 ):59 token.content = token.content[1:-1]60 state.pos = matchEnd61 return True62 63 # Some different length found, put it in cache as upper limit of where closer can be found64 state.backticks[closerLength] = matchStart65 66 # Scanned through the end, didn't find anything67 state.backticksScanned = True68 69 if not silent:70 state.pending += marker71 state.pos += openerLength72 return True73 