split a row of text with some code into a list of cells.
(self, row: str)
| 153 | return self._split(row) |
| 154 | |
| 155 | def _split(self, row: str) -> list[str]: |
| 156 | """ split a row of text with some code into a list of cells. """ |
| 157 | elements = [] |
| 158 | pipes = [] |
| 159 | tics = [] |
| 160 | tic_points = [] |
| 161 | tic_region = [] |
| 162 | good_pipes = [] |
| 163 | |
| 164 | # Parse row |
| 165 | # Throw out \\, and \| |
| 166 | for m in self.RE_CODE_PIPES.finditer(row): |
| 167 | # Store ` data (len, start_pos, end_pos) |
| 168 | if m.group(2): |
| 169 | # \`+ |
| 170 | # Store length of each tic group: subtract \ |
| 171 | tics.append(len(m.group(2)) - 1) |
| 172 | # Store start of group, end of group, and escape length |
| 173 | tic_points.append((m.start(2), m.end(2) - 1, 1)) |
| 174 | elif m.group(3): |
| 175 | # `+ |
| 176 | # Store length of each tic group |
| 177 | tics.append(len(m.group(3))) |
| 178 | # Store start of group, end of group, and escape length |
| 179 | tic_points.append((m.start(3), m.end(3) - 1, 0)) |
| 180 | # Store pipe location |
| 181 | elif m.group(5): |
| 182 | pipes.append(m.start(5)) |
| 183 | |
| 184 | # Pair up tics according to size if possible |
| 185 | # Subtract the escape length *only* from the opening. |
| 186 | # Walk through tic list and see if tic has a close. |
| 187 | # Store the tic region (start of region, end of region). |
| 188 | pos = 0 |
| 189 | tic_len = len(tics) |
| 190 | while pos < tic_len: |
| 191 | try: |
| 192 | tic_size = tics[pos] - tic_points[pos][2] |
| 193 | if tic_size == 0: |
| 194 | raise ValueError |
| 195 | index = tics[pos + 1:].index(tic_size) + 1 |
| 196 | tic_region.append((tic_points[pos][0], tic_points[pos + index][1])) |
| 197 | pos += index + 1 |
| 198 | except ValueError: |
| 199 | pos += 1 |
| 200 | |
| 201 | # Resolve pipes. Check if they are within a tic pair region. |
| 202 | # Walk through pipes comparing them to each region. |
| 203 | # - If pipe position is less that a region, it isn't in a region |
| 204 | # - If it is within a region, we don't want it, so throw it out |
| 205 | # - If we didn't throw it out, it must be a table pipe |
| 206 | for pipe in pipes: |
| 207 | throw_out = False |
| 208 | for region in tic_region: |
| 209 | if pipe < region[0]: |
| 210 | # Pipe is not in a region |
| 211 | break |
| 212 | elif region[0] <= pipe <= region[1]: |