Skip to content

Commit

Permalink
use the common table class
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Sep 25, 2024
1 parent d891905 commit 4f6a03b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def create_md(self) -> str:
# column sanity check
columns = [len(row) for row in self.header_rows + self.data_rows]
if len(set(columns)) not in (0, 1):
print(f"Amount of columns differs: {columns}")
LOGGER.warning(f"Amount of columns differs: {columns}")

def create_md_row(cells: list[str]) -> str:
return "| " + " | ".join(cells) + " |"
Expand Down
18 changes: 6 additions & 12 deletions src/formats/cherrytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@


def convert_table(node):
table_md = []
# TODO: a constant row and column count is expected
# | Syntax | Description |
# | --- | --- |
# | Header | Title |
# | Paragraph | Text |
table_md = common.MarkdownTable()
for row_index, row in enumerate(node):
assert row.tag == "row"
columns = []
for cell in row:
assert cell.tag == "cell"
cell_text = "" if cell.text is None else cell.text.replace("\n", "<br>")
columns.append(cell_text)
table_md.append("| " + " | ".join(columns) + " |")

if row_index == 0:
# header row
separator = ["---"] * len(columns)
table_md.append("| " + " | ".join(separator) + " |")
return "\n".join(table_md)
if row_index == 0: # header row
table_md.header_rows.append(columns)
else:
table_md.data_rows.append(columns)
return table_md.create_md()


def fix_inline_formatting(md_content: str) -> str:
Expand Down
16 changes: 7 additions & 9 deletions src/formats/zettelkasten.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,17 @@ def _render_internal_link(name, value, options, parent, context):

# tables
def _render_table(name, value, options, parent, context):
table_md = []
table_md = common.MarkdownTable()
for line in value.split("\n"):
if not line.strip():
continue
if "^" in line:
# header
table_md.append("| " + " | ".join(line.split("^")) + " |")
separator = ["---"] * len(line.split("^"))
table_md.append("| " + " | ".join(separator) + " |")
table_md.header_rows.append(line.split("^"))
elif "|" in line:
# row
table_md.append("| " + " | ".join(line.split("|")) + " |")
table_md.data_rows.append(line.split("|"))
else:
table_md.append(line)
return "\n".join(table_md)
table_md.caption += line
return table_md.create_md()

parser.add_formatter("table", _render_table)
parser.add_simple_formatter("tc", "%(value)s\n")
Expand Down

0 comments on commit 4f6a03b

Please sign in to comment.