Last active
April 21, 2022 21:27
-
-
Save Epicpkmn11/8011e3c0d0734b9a735bbe1695ee1e1e to your computer and use it in GitHub Desktop.
wrap.py - Wraps a text file to 32 columns, doing its best to preserve ASCII formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import re | |
import textwrap | |
from argparse import ArgumentParser, FileType | |
def processLine(line: str) -> str: | |
line = line.rstrip() | |
# Reduce indent | |
if len(line) > 32 and len(line.strip()) <= 32: | |
line = line.strip() | |
line = (" " * ((32 - len(line)) // 2)) + line | |
elif len(re.findall("^ *", line)[0]) > 4: | |
line = re.sub("^ +", lambda x: x[0][:len(x[0]) // 4], line) | |
# probably a horizontal rule, just cut it | |
if len(re.findall(r"[^\-=+* ]", line)) == 0: | |
line = line[:32] | |
return line | |
parser = ArgumentParser(description="Wraps a text file to 32 columns, doing its best to preserve ASCII formatting") | |
parser.add_argument("input", type=FileType("r", encoding="Windows 1252"), help="input file") | |
parser.add_argument("output", type=FileType("w"), help="output file") | |
args = parser.parse_args() | |
input = args.input.read().split("\n") | |
grouped = [] | |
temp = [] | |
indent = 0 | |
lineCount = 0 | |
for line in input: | |
line = processLine(line) | |
if len(re.findall("^ *", line)[0]) == indent and len(line) > 60: | |
temp.append(line) | |
else: | |
if len(temp) > 0 and max(len(x) for x in temp) > 32: | |
temp = textwrap.fill("\n".join(temp), 32, initial_indent=" " * indent, subsequent_indent=" " * indent) | |
if(len(re.findall(r"[_\-=+*|/\\^#\[\],.'\" ]", temp)) > len(temp) * .8): | |
print(f"Warning: Probably ASCII art at line {lineCount}") | |
else: | |
temp = "\n".join(temp) | |
grouped.append(temp) | |
lineCount += len(re.findall("\n", temp)) + 1 | |
if line == "": | |
grouped.append("") | |
lineCount += 1 | |
temp = [] | |
indent = 0 | |
else: | |
temp = [line] | |
indent = len(re.findall("^ *", line)[0]) | |
if len(temp) > 0: | |
if max(len(x) for x in temp) > 32: | |
temp = textwrap.fill("\n".join(temp), 32, initial_indent=" " * (indent * 32 // 80)) | |
else: | |
temp = "\n".join(temp) | |
grouped.append(temp) | |
args.output.write("\n".join(grouped)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment