1.1 --- a/moinformat/__init__.py Mon May 01 23:36:31 2017 +0200
1.2 +++ b/moinformat/__init__.py Tue May 02 16:39:30 2017 +0200
1.3 @@ -21,7 +21,8 @@
1.4
1.5 from moinformat.tree import Block, Break, DefItem, DefTerm, FontStyle, Heading, \
1.6 Larger, ListItem, Monospace, Region, Rule, Smaller, \
1.7 - Subscript, Superscript, Text, Underline
1.8 + Subscript, Superscript, TableCell, TableRow, Text, \
1.9 + Underline
1.10 import re
1.11
1.12 # Regular expressions.
1.13 @@ -52,6 +53,8 @@
1.14 "listitem_roman": r"^(\s+)([iI]\.)(\s+)",
1.15 # ws... dot-item [ws...]
1.16 "listitem_dot" : r"^(\s+)(\.)(\s*)",
1.17 + # ||
1.18 + "tablerow" : r"^\|\|",
1.19
1.20 # Region contents:
1.21 # Inline patterns:
1.22 @@ -79,6 +82,10 @@
1.23 "deftermend" : r"::(\s*?\n)",
1.24 "deftermsep" : r"::(\s+)",
1.25 "listitemend" : r"^", # next line
1.26 +
1.27 + # Table contents:
1.28 + "tablecell" : r"\|\|",
1.29 + "tableend" : r"(\s*?)^", # [ws...] next line
1.30 }
1.31
1.32 # Define inline pattern details.
1.33 @@ -220,6 +227,7 @@
1.34 "listitem_roman",
1.35 "regionstart", "regionend",
1.36 "rule",
1.37 + "tablerow",
1.38 ])
1.39
1.40 def parse_region_opaque(items, region):
1.41 @@ -408,6 +416,52 @@
1.42 else:
1.43 region.append_inline(Text(feature))
1.44
1.45 +def parse_table_row(items, region):
1.46 +
1.47 + "Handle the start of a table row within 'region'."
1.48 +
1.49 + row = TableRow([])
1.50 +
1.51 + while True:
1.52 + cell = TableCell([])
1.53 + parse_region_details(items, cell, ["tablecell", "tableend"])
1.54 +
1.55 + # Handle the end of the row.
1.56 +
1.57 + if items.matching == "tableend":
1.58 + trailing = items.read_match()
1.59 +
1.60 + # If the cell was started but not finished, convert the row into text.
1.61 +
1.62 + if not row.nodes or not cell.empty():
1.63 + region.append_inline(Text("||"))
1.64 +
1.65 + # Convert all cells.
1.66 +
1.67 + for node in row.nodes:
1.68 + region.append_inline_many(node.nodes)
1.69 + region.append_inline(Text("||"))
1.70 +
1.71 + region.append_inline_many(cell.nodes)
1.72 + region.append_inline(Text(trailing))
1.73 +
1.74 + new_block(region)
1.75 + return
1.76 +
1.77 + # Append the final cell, if not empty.
1.78 +
1.79 + else:
1.80 + row.trailing = trailing
1.81 +
1.82 + if not cell.empty():
1.83 + row.append(cell)
1.84 + break
1.85 +
1.86 + row.append(cell)
1.87 +
1.88 + region.add(row)
1.89 + new_block(region)
1.90 +
1.91 # Inline formatting handlers.
1.92
1.93 def parse_inline(items, region, cls, pattern_name):
1.94 @@ -456,6 +510,9 @@
1.95 "subend" : end_region,
1.96 "super" : parse_super,
1.97 "superend" : end_region,
1.98 + "tablerow" : parse_table_row,
1.99 + "tablecell" : end_region,
1.100 + "tableend" : end_region,
1.101 "underline" : parse_underline,
1.102 "underlineend" : end_region,
1.103 }