Skip to content

Instantly share code, notes, and snippets.

@mainframed
Created August 15, 2021 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mainframed/99b9eae1ec094a52dfd75a540786d765 to your computer and use it in GitHub Desktop.
Save mainframed/99b9eae1ec094a52dfd75a540786d765 to your computer and use it in GitHub Desktop.
Convert ANSI carriage control characters to markdown/text
#!/usr/bin/env python3
import sys
# ANSI carriage control characters
# Some MVS documents use 0/1/+/- as carriage control. This python will
# convert them to markdown/text. The standard ANSI characters are:
#
# - `space` Single space the line and print.
# - `0` Double space the line and print.
# - `-` Triple space the line and print.
# - `+` Do not space the line and print.
# - `1` New Page
#
# When complete you can convert to PDF with:
# pandoc klingon_manual.txt --pdf-engine=pdflatex -V geometry:top=1in -V geometry:left=1.6in -o Klingon\ Manual.pdf
if len(sys.argv) < 2:
print("Usage: {} file.to.convert.txt".format(sys.argv[0]))
sys.exit(1)
if len(sys.argv) > 2 and sys.argv[2].isdigit():
start = int(sys.argv[2])
else:
start = 1
with open(sys.argv[1],'r') as mcfile:
for mc_line in mcfile.readlines():
#print('DEBUG: {} "{}":{}'.format(len(mc_line), repr(mc_line[0]),repr(mc_line)))
mc_line = mc_line.strip("\n")
if mc_line[0] == " " or mc_line[0] == "+":
print(mc_line[start:])
elif mc_line[0] == "0":
print("\n" + mc_line[start:])
elif mc_line[0] == "-":
print("\n\n" + mc_line[start:])
elif mc_line[0] == "1":
print("\pagebreak")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment