|
| 1 | +# ~~strike through~~ |
| 2 | +from typing import List |
| 3 | +from .state_inline import StateInline, Delimiter |
| 4 | +from ..common.utils import charCodeAt |
| 5 | + |
| 6 | + |
| 7 | +def tokenize(state: StateInline, silent: bool): |
| 8 | + """Insert each marker as a separate text token, and add it to delimiter list""" |
| 9 | + start = state.pos |
| 10 | + marker = charCodeAt(state.src, start) |
| 11 | + |
| 12 | + if silent: |
| 13 | + return False |
| 14 | + |
| 15 | + if marker != 0x7E: # /* ~ */ |
| 16 | + return False |
| 17 | + |
| 18 | + scanned = state.scanDelims(state.pos, True) |
| 19 | + length = scanned.length |
| 20 | + ch = chr(marker) |
| 21 | + |
| 22 | + if length < 2: |
| 23 | + return False |
| 24 | + |
| 25 | + if length % 2: |
| 26 | + token = state.push("text", "", 0) |
| 27 | + token.content = ch |
| 28 | + length -= 1 |
| 29 | + |
| 30 | + i = 0 |
| 31 | + while i < length: |
| 32 | + token = state.push("text", "", 0) |
| 33 | + token.content = ch + ch |
| 34 | + state.delimiters.append( |
| 35 | + Delimiter( |
| 36 | + **{ |
| 37 | + "marker": marker, |
| 38 | + "length": 0, # disable "rule of 3" length checks meant for emphasis |
| 39 | + "jump": i, |
| 40 | + "token": len(state.tokens) - 1, |
| 41 | + "end": -1, |
| 42 | + "open": scanned.can_open, |
| 43 | + "close": scanned.can_close, |
| 44 | + } |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + i += 2 |
| 49 | + |
| 50 | + state.pos += scanned.length |
| 51 | + |
| 52 | + return True |
| 53 | + |
| 54 | + |
| 55 | +def _postProcess(state: StateInline, delimiters: List[Delimiter]): |
| 56 | + |
| 57 | + loneMarkers = [] |
| 58 | + maximum = len(delimiters) |
| 59 | + |
| 60 | + i = 0 |
| 61 | + while i < maximum: |
| 62 | + startDelim = delimiters[i] |
| 63 | + |
| 64 | + if startDelim.marker != 0x7E: # /* ~ */ |
| 65 | + i += 1 |
| 66 | + continue |
| 67 | + |
| 68 | + if startDelim.end == -1: |
| 69 | + i += 1 |
| 70 | + continue |
| 71 | + |
| 72 | + endDelim = delimiters[startDelim.end] |
| 73 | + |
| 74 | + token = state.tokens[startDelim.token] |
| 75 | + token.type = "s_open" |
| 76 | + token.tag = "s" |
| 77 | + token.nesting = 1 |
| 78 | + token.markup = "~~" |
| 79 | + token.content = "" |
| 80 | + |
| 81 | + token = state.tokens[endDelim.token] |
| 82 | + token.type = "s_close" |
| 83 | + token.tag = "s" |
| 84 | + token.nesting = -1 |
| 85 | + token.markup = "~~" |
| 86 | + token.content = "" |
| 87 | + |
| 88 | + if ( |
| 89 | + state.tokens[endDelim.token - 1].type == "text" |
| 90 | + and state.tokens[endDelim.token - 1].content == "~" |
| 91 | + ): |
| 92 | + |
| 93 | + loneMarkers.append(endDelim.token - 1) |
| 94 | + |
| 95 | + i += 1 |
| 96 | + |
| 97 | + # If a marker sequence has an odd number of characters, it's splitted |
| 98 | + # like this: `~~~~~` -> `~` + `~~` + `~~`, leaving one marker at the |
| 99 | + # start of the sequence. |
| 100 | + # |
| 101 | + # So, we have to move all those markers after subsequent s_close tags. |
| 102 | + # |
| 103 | + while loneMarkers: |
| 104 | + i = loneMarkers.pop() |
| 105 | + j = i + 1 |
| 106 | + |
| 107 | + while (j < len(state.tokens)) and (state.tokens[j].type == "s_close"): |
| 108 | + j += 1 |
| 109 | + |
| 110 | + j -= 1 |
| 111 | + |
| 112 | + if i != j: |
| 113 | + token = state.tokens[j] |
| 114 | + state.tokens[j] = state.tokens[i] |
| 115 | + state.tokens[i] = token |
| 116 | + |
| 117 | + |
| 118 | +def postProcess(state: StateInline): |
| 119 | + """Walk through delimiter list and replace text tokens with tags.""" |
| 120 | + tokens_meta = state.tokens_meta |
| 121 | + maximum = len(state.tokens_meta) |
| 122 | + _postProcess(state, state.delimiters) |
| 123 | + |
| 124 | + curr = 0 |
| 125 | + while curr < maximum: |
| 126 | + try: |
| 127 | + tokens_meta[curr] |
| 128 | + except IndexError: |
| 129 | + pass |
| 130 | + else: |
| 131 | + if tokens_meta[curr] and "delimiters" in tokens_meta[curr]: |
| 132 | + _postProcess(state, tokens_meta[curr]["delimiters"]) |
| 133 | + curr += 1 |
0 commit comments