# -*- coding: utf-8 -*- print("**Minetest lua_api.txt Markdown parser - mt-lua-api-pyparse - converts Markdown to HTML document with navigation**") print("**Written by Lars Müller alias LMD in Python 3.5 - requires Python >= 3.2**") print("**Furthermore, this script has to be in the same folder as generate_bg.py, template.html, jumbotron.css, and the images taken from Minetest Game.**") print("**The images taken from Minetest Game are licensed under CC-BY-SA 3.0, credits go to the Minetest Artists.**") from cgi import html import urllib.request as url import urllib.parse as parse print("**Generating underground Minetest scene...**") exec(open("generate_bg.py","r").read()) # IMPORTANT - GENERATES THE BEAUTIFUL BACKGROUND ! print("**...finished generating scene.**") import math # This python script grabs the newest lua_api.txt from Minetest GitHub repo and converts it to HTML, plus adding some bookmarks & css # So mainly MD -> HTML. Written by me to improve my rusty Python skills. # © Lars Müller @appguru.eu print("**Grabbing newest lua_api.txt, make sure you have an internet connection...**") link = "https://raw.githubusercontent.com/minetest/minetest/master/doc/lua_api.txt" # Grab newest lua_api.txt f = url.urlopen(link) markdown = parse.unquote(str(f.read().decode("ascii","ignore"))) # Read & convert print("**...grabbed newest lua_api.txt from official Minetest repository.**") liste=0 # Which sublist we are in right NOW headers=[] # Stores all the headers + IDs ID=0 # Stores header ID counter print("**Starting parsing...**") def parse_markdown(string,lco=0,parent=False): # PARSES A SINGLE LINE ! global liste global ID global headers suffix="" prefix="" if string.find("*") != -1 and (string[0:string.find("*")].count(" ") == string.find("*")) and not (parent or string[string.find("*")+1]=="*"): # LISTS #print("Liste : "+str(liste)+", lco : "+str(lco)) prevliste=liste liste=1+int(string.find("*")/3) if (liste > prevliste): for i in range(0,liste-prevliste): prefix+="
","
"
elif tag[1]=="bold":
p,s="",""
elif tag[1]=="link":
if len(tag) == 2:
if tag[0][0:4] == "http": # CHECK LINKS !
p,s='',""
else:
p,s='',""
elif tag[1]=="italic":
p,s="",""
result+=p+html.escape(string)+s
return prefix+""+result+"
" def parse_md(string): # Parse line by line lines=string.split("\n") ret="" for i in range(len(lines)-1,0,-1): # Convert alternate header writings(underlines) if abs(len(lines[i-1])-len(lines[i])) < 3: if lines[i].count("=")==len(lines[i]): lines[i]="" lines[i-1]="# "+lines[i-1] elif lines[i].count("-")==len(lines[i]): lines[i]="" lines[i-1]="## "+lines[i-1] i=0 for line in lines: ret+=parse_markdown(line) i=i+1 return ret def code(): # Parse multi-line code fragments global markdown last=-1 i=0 stuff=[] while (i < len(markdown)): if markdown[i:i+3]=="`"*3: i=i+3 if last < 0: start=-(last+1) last=i stuff.append((markdown[start:last-3],False)) else: stuff.append((markdown[last:i-3],True)) last=-i-1 i=i+1 start=-(last+1) stuff.append((markdown[start:],False)) #print(stuff) markdown="" for s in stuff: if s[1]: markdown+=""+s[0]+"
"
else:
markdown+=parse_md(s[0])
code()
print("**...finished parsing.**")
nav=""
print("**Creating content table...**")
for header in headers:
nav+="""