more markdown tooling

This commit is contained in:
Q
2023-04-18 20:32:25 +03:00
parent da5e20e867
commit d5c4390813
6 changed files with 197 additions and 0 deletions

77
reporting/md-wrap Executable file
View File

@@ -0,0 +1,77 @@
#!/usr/bin/env python3
import argparse
import sys
import re
def get_opts():
parser = argparse.ArgumentParser(
description="Line wrapper for markdown.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"-w",
help="Line width. Defaults to 78",
action="store",
default=78,
type=int,
)
parser.add_argument(
"markdown",
help="Filename to read. - for stdin",
action="store",
default="-",
nargs="?",
)
args = parser.parse_args()
return args
def main():
opts = get_opts()
if opts.markdown == "-":
fp = sys.stdin
else:
fp = open(opts.markdown, "rt")
markdown = fp.read().splitlines()
in_code = False
for i, row in enumerate(markdown):
in_table = row.startswith("|")
if row.startswith("```"):
in_code = not in_code
if not any((in_code, in_table)):
row = row.rstrip(" ")
if len(row.strip(" ")) == 0:
row = ""
if len(row) > opts.w:
# Row is long, let's cut it
leading_spaces = len(row) - len(row.lstrip(" -*"))
row_split = row.split(" ")
new_row = []
new_padding = ""
while len(row_split) > 0:
while (
len(row_split) > 0
and len(new_padding)
+ len(" ".join(new_row))
+ len(row_split[0])
< opts.w
):
new_row.append(row_split.pop(0))
if len(new_row) == 0:
new_row.append(row_split.pop(0))
print(new_padding + " ".join(new_row))
new_row = []
new_padding = " " * leading_spaces
row = None
if not row is None:
print(row)
if __name__ == "__main__":
main()