💾 Archived View for mozz.us › files › unwrap_gemini_file.py captured on 2024-08-25 at 01:43:59.

View Raw

More Information

⬅️ Previous capture (2020-09-24)

-=-=-=-=-=-=-

#!/usr/bin/env python3
"""
Remove hard line-wraps from a gemini text file.

Blocks surrounded by pre-formatted ``` ticks will have their line breaks
preserved, and the ``` will be removed. This will overwrite the existing
file, use at your own risk!

Usage:
    ./unwrap_gemini_file.py FILENAME
"""
import sys

filename = sys.argv[1]
preformat = False
outlines = []
with open(filename) as fp:
    for line in fp.readlines():
        line = line.rstrip('\r\n')
        if line.startswith('```'):
            preformat = not preformat
        elif preformat:
            outlines.append(line)
        elif line.startswith(('=>', '- ', '> ')):
            outlines.append(line)
        elif not line:
            outlines.append(line)
        elif outlines and outlines[-1]:
            outlines[-1] = outlines[-1] + ' ' + line
        else:
            outlines.append(line)

with open(filename, 'w') as fp:
    fp.write('\n'.join(outlines))