Written in 2019 to handle some documents' recurring operations.
It executes the document text word wrap based on the supplied characters number in the MAX_WIDTH
argument.
MAX_WIDTH = 80
import argparse
# Parse arguments
parser = argparse.ArgumentParser(description='Wrap text of provided file in a new file')
parser.add_argument('--source', type=str, help='The file of which the text has to be wrapped.')
parser.add_argument('--destination', type=str, help='The destination file.')
args = parser.parse_args()
def chunk(line, by):
newline = ""
to_split = False
for nr, char in enumerate(line):
newline += char
if nr % by is 0 and nr > 1:
to_split = True
if char == " " and to_split is True:
yield newline
newline = ""
to_split = False
print("@@@@@@@@@")
print(args.destination)
print(args.source)
with open(args.destination, "w") as out:
with open(args.source) as f:
skip = False
for line_no, line in enumerate(f):
if "*/" in line:
skip = True
if "/*" in line:
skip = False
if skip is False:
if len(line) > MAX_WIDTH:
sublines = chunk(line, MAX_WIDTH)
out.write("\n".join(sublines))
else:
out.write(line + "\n")
else:
out.write(line)