Last active
June 3, 2022 16:10
-
-
Save mainframed/94e4a85c8bd44fdd397e8dd11a0a9953 to your computer and use it in GitHub Desktop.
Python script to create an ISPF panel from markdown
This file contains hidden or 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 python | |
import os | |
import re | |
def make_header(name): | |
return ''')ATTR DEFAULT(%+_) | |
% type(text) intens(high) | |
~ type(text) intens(high) caps(off) just(asis ) color(red) | |
+ type(text) color(turq) caps(off) | |
` type(text) intens(high) caps(off) just(asis) color(yellow) | |
# AREA(SCRL) EXTEND(ON) | |
)Body Expand(\\\\) WIDTH(80) | |
%-\-\-~Evil Mainframe WIKI%-\-\- | |
%Command ===>_zcmd | |
%=\=\= '''+name+''' =\=\= | |
#TextArea -------------------------------------------------------------# | |
)AREA TEXTAREA''' | |
footer = ''' | |
)Init | |
.help = wikihelp | |
.cursor = zcmd | |
)End''' | |
def natural_sort(l): | |
convert = lambda text: int(text) if text.isdigit() else text.lower() | |
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] | |
return sorted(l, key=alphanum_key) | |
def find_all(a_str, sub): | |
start = 0 | |
while True: | |
start = a_str.find(sub, start) | |
if start == -1: return | |
yield start | |
start += len(sub) # use start += 1 to find overlapping matches | |
for root, dirs, files in os.walk("."): | |
f = natural_sort(files) | |
for filename in f: | |
text = '' | |
if filename.find('png') < 0: | |
try: | |
num = int(filename.split()[0]) | |
name = filename.split(" ",1)[1].split(".")[0] | |
text = make_header(name) | |
phile = open(filename, "r") | |
md = phile.read().splitlines() | |
phile.close() | |
mode = "normal" | |
for line in md: | |
# Match ** with ** | |
# replace `` with `+ | |
# Split lines at 70 chars | |
newline = ' ' | |
bullet = 0 | |
if len(line) == 0: | |
newline = "+" | |
elif line.find("## **",0) >= 0: | |
if line.find("### **") >= 0: | |
newline = "%"+line[6:-2] | |
else: | |
newline = "%"+line[5:-2] | |
elif line == "```": | |
if mode == "normal": | |
mode = "code" | |
newline = "`" | |
else: | |
mode = "normal" | |
newline = "+" | |
elif line[0] == "*": | |
if line[0:3] == "***": | |
newline = "+"+" -"+line[3:] | |
bullet = 6 | |
elif line[0:2] == "**": | |
newline = "+"+" -"+line[2:] | |
bullet = 4 | |
elif line[0] == "*": | |
newline = "+"+"-"+line[1:] | |
bullet = 2 | |
else: | |
if mode == "normal": | |
newline = "+"+line | |
else: | |
newline = "`"+line | |
# Ok we caught all the lines | |
#remove bold | |
if newline.find("**") > 0: | |
newline = newline.replace("*","") | |
#make inline code work | |
if newline[1:].find("`") >= 0: | |
code = "off" | |
l = list(find_all(newline,"`")) | |
for x in l: | |
if code == "off": | |
code = "on" | |
else: | |
code = "off" | |
newline = newline[:x] + "+" + newline[x + 1:] | |
#remove images | |
if newline.find("[img") >= 0: | |
newline = newline[:newline.find("[img")]+ newline[newline.find("]]")+2:] | |
if len(newline) + bullet > 62: | |
s = newline.rfind(" ",0,62 + bullet) | |
if len(newline[s:]) + bullet > 62: | |
if len(newline[s+62+bullet:])+bullet > 62: | |
newline = (newline[:s] + "\n" + bullet*" "+ | |
newline[s:s+62+bullet] +"\n" + bullet*" "+" "+ newline[s+62+bullet:s+62+62+bullet] + | |
"\n" + bullet*" "+" "+ | |
newline[s+62+62+bullet:]) | |
else: | |
newline = newline[:s] + "\n" + bullet*" "+ newline[s:s+62+bullet] +"\n" + bullet*" "+" "+ newline[s+62+bullet:] | |
else: | |
newline = newline[:s] +"\n" + bullet*" "+ newline[s:] | |
newline = newline.replace("#"," ") | |
newline = newline.replace("_"," ") | |
newline = newline.replace("\\"," ") | |
text = text + "\n" + newline | |
text = text + footer | |
with open("output/W"+str(num), "w") as text_file: | |
text_file.write(text) | |
except ValueError: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment