def _ConvertOldTable(self, i): """Detects and converts a sequence of markdown table lines. This method will consume multiple input lines if the current line is a table heading. The table markdown sequence is: [...format="csv"...] |====* col-1-data-item,col-2-data-item... ... <blank line ends table> Args: i: The current character index in self._line. Returns: -1 if the input lines are table markdown, i otherwise. """ if (self._line[0] != '[' or self._line[-1] != ']' or 'format="csv"' not in self._line): return i line = self._ReadLine() if not line: return i if not line.startswith('|===='): self._PushBackLine(line) return i rows = [] while True: self._buf = self._ReadLine() if not self._buf: break self._buf = self._buf.rstrip() if self._buf.startswith('|===='): break rows.append(self._Attributes().split(',')) self._buf = '' table = renderer.TableAttributes() if len(rows) > 1: for label in rows[0]: table.AddColumn(label=label) rows = rows[1:] if table.columns and rows: self._renderer.Table(table, rows) return -1
def _ConvertTable(self, i): """Detects and converts a sequence of markdown table lines. Markdown attributes are not supported in headings or column data. This method will consume multiple input lines if the current line is a table heading or separator line. The table markdown sequence is: heading line heading-1 | ... | heading-n OR for boxed table | heading-1 | ... | heading-n | separator line --- | ... | --- OR for boxed table | --- | ... | --- | WHERE :--- align left :---: align center ---: align right ----* length >= fixed_width_length sets column fixed width row data lines col-1-data-item | ... | col-n-data-item ... blank line ends table Args: i: The current character index in self._line. Returns: -1 if the input lines are table markdown, i otherwise. """ fixed_width_length = 8 if ' | ' not in self._line: return self._ConvertOldTable(i) if '---' in self._line: head = False line = self._line else: head = True line = self._ReadLine() if not line or '---' not in line: if line is not self._line: self._PushBackLine(line) return self._ConvertOldTable(i) # Parse the heading and separator lines. box = False if head: heading = re.split(r' *\| *', self._line.strip()) if not heading[0] and not heading[-1]: heading = heading[1:-1] box = True else: heading = [] sep = re.split(r' *\| *', line.strip()) if not sep[0] and not sep[-1]: sep = sep[1:-1] box = True if heading and len(heading) != len(sep): if line is not self._line: self._PushBackLine(line) return self._ConvertOldTable(i) # Committed to table markdown now. table = renderer.TableAttributes(box=box) # Determine the column attributes. for index in range(len(sep)): align = 'left' s = sep[index] if s.startswith(':'): if s.endswith(':'): align = 'center' elif s.endswith(':'): align = 'right' label = heading[index] if index < len(heading) else None width = len(s) if len(s) >= fixed_width_length else 0 table.AddColumn(align=align, label=label, width=width) # Collect the column data by rows. Blank or + line terminates the data. rows = [] while True: line = self._ReadLine() if line in (None, '', '\n', '+\n'): self._PushBackLine(line) break row = re.split(r' *\| *', line.rstrip()) rows.append(row) if rows: self._renderer.Table(table, rows) self._buf = '' return -1