forked from Neoteroi/essentials-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd.py
More file actions
75 lines (63 loc) · 1.84 KB
/
md.py
File metadata and controls
75 lines (63 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
This module provides common functions to handle Markdown.
These functions apply to any kind of Markdown work.
"""
from typing import Iterable
def write_row(
row: Iterable[str],
columns_widths: dict[int, int],
padding: int = 1,
indent: int = 0,
) -> str:
"""
Writes a single row for a Markdown table.
"""
indent_chars = " " * indent
pad_chars = " " * padding
return (
indent_chars
+ "|"
+ "|".join(
pad_chars + str(cell_value).ljust(columns_widths[i]) + pad_chars
for i, cell_value in enumerate(row)
)
+ "|"
)
def write_table_lines(
matrix: Iterable[Iterable[str]],
write_headers: bool = True,
padding: int = 1,
indent: int = 0,
) -> Iterable[str]:
"""
Writes the lines of a Markdown table from a matrix (iterable of string records).
"""
# TODO: assert that all rows have the same number of cells
columns_widths = {
i: max(len(str(value)) for value in column)
for i, column in enumerate(zip(*matrix))
}
for row in matrix:
yield write_row(row, columns_widths, padding, indent)
if write_headers:
# add separator line after headers
yield write_row(
["-" * column_len for column_len in columns_widths.values()],
columns_widths,
padding,
indent,
)
write_headers = False
def write_table(
matrix: Iterable[Iterable[str]],
write_headers: bool = True,
padding: int = 1,
) -> str:
"""
Writes a Markdown table from a matrix (iterable of string records).
"""
return "\n".join((write_table_lines(matrix, write_headers, padding)))
def normalize_link(value: str) -> str:
if not value:
raise ValueError("Missing value")
return value.replace(".", "")