61 lines
1.6 KiB
Python
Executable file
61 lines
1.6 KiB
Python
Executable file
#!/bin/env python
|
|
|
|
import textwrap
|
|
import sys
|
|
|
|
# with open(sys.argv[1], 'r') as f:
|
|
# lines = f.read().splitlines()
|
|
# print(lines)
|
|
|
|
# first pass: reflow paragraphs
|
|
# paragraph = []
|
|
# lines = []
|
|
# for line in sys.stdin:
|
|
# line = line.rstrip()
|
|
# if line:
|
|
# paragraph.append(line)
|
|
# else:
|
|
# if paragraph:
|
|
# lines.append(' '.join(paragraph))
|
|
# lines.append("\n")
|
|
# if paragraph:
|
|
# lines.append(' '.join(paragraph))
|
|
|
|
|
|
skipping = False
|
|
in_par = False
|
|
for line in sys.stdin:
|
|
if line.startswith(">"):
|
|
# ignore quoted line
|
|
print(line, end = "")
|
|
elif line.startswith("```"):
|
|
# ignore code block
|
|
skipping = not skipping
|
|
print(line, end = "")
|
|
elif line.startswith("--"):
|
|
skipping = True
|
|
print(line, end = "")
|
|
elif not line.rstrip():
|
|
in_par = not in_par
|
|
print()
|
|
# print(line, end="")
|
|
else:
|
|
if not skipping and in_par:
|
|
par = []
|
|
for next_line in sys.stdin:
|
|
# if not line.startswith((">", "```", "--")):
|
|
stripped = next_line.rstrip()
|
|
if stripped:
|
|
par.append(stripped)
|
|
else:
|
|
in_par = False
|
|
break
|
|
line = " ".join(par)
|
|
# empty line, treat next section as paragraph
|
|
# TODO: fix signature being mangled and newlines erased
|
|
if not skipping:
|
|
wrapped = textwrap.wrap(line, width=74, break_long_words=False, replace_whitespace=True)
|
|
|
|
print("\n".join(wrapped))
|
|
else:
|
|
print(line, end = "")
|