1# Parse backticks 
    2import re 
    3 
    4from .state_inline import StateInline 
    5 
    6regex = re.compile("^ (.+) $") 
    7 
    8 
    9def backtick(state: StateInline, silent: bool) -> bool: 
    10    pos = state.pos 
    11 
    12    if state.src[pos] != "`": 
    13        return False 
    14 
    15    start = pos 
    16    pos += 1 
    17    maximum = state.posMax 
    18 
    19    # scan marker length 
    20    while pos < maximum and (state.src[pos] == "`"): 
    21        pos += 1 
    22 
    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 += marker 
    29        state.pos += openerLength 
    30        return True 
    31 
    32    matchStart = matchEnd = pos 
    33 
    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            break 
    40        matchEnd = matchStart + 1 
    41 
    42        # scan marker length 
    43        while matchEnd < maximum and (state.src[matchEnd] == "`"): 
    44            matchEnd += 1 
    45 
    46        closerLength = matchEnd - matchStart 
    47 
    48        if closerLength == openerLength: 
    49            # Found matching closer length. 
    50            if not silent: 
    51                token = state.push("code_inline", "code", 0) 
    52                token.markup = marker 
    53                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()) > 0 
    58                ): 
    59                    token.content = token.content[1:-1] 
    60            state.pos = matchEnd 
    61            return True 
    62 
    63        # Some different length found, put it in cache as upper limit of where closer can be found 
    64        state.backticks[closerLength] = matchStart 
    65 
    66    # Scanned through the end, didn't find anything 
    67    state.backticksScanned = True 
    68 
    69    if not silent: 
    70        state.pending += marker 
    71    state.pos += openerLength 
    72    return True