minetest_modding_book/update_lua_api.py

113 lines
2.9 KiB
Python
Raw Normal View History

2015-11-22 19:41:52 +03:00
import markdown, urllib2, datetime, re, string
from bs4 import BeautifulSoup
2015-11-22 19:41:52 +03:00
def get_key(index, li, title):
title = title.replace(" ", "-")
title = title.replace(" ", "-")
title = title.replace(" ", "-")
#title = re.sub(r'\([^)]*\)', '', title)
title = pattern.sub('', title)
if title == "":
return None
i = 0
while True:
key = title
if i > 0:
key = key + "_" + str(i)
i = i + 1
try:
existing = index[key]
except KeyError:
return key
#
# Downloading lua_api.txt
#
print("Downloading lua_api.txt...")
url = "https://raw.githubusercontent.com/minetest/minetest/master/doc/lua_api.txt"
text = urllib2.urlopen(url).read()
2016-08-29 15:43:46 +03:00
text = unicode(text, "utf-8")
2015-11-21 18:26:52 +03:00
2015-11-22 19:41:52 +03:00
print("Pre-generation replacements...")
2015-11-21 18:26:52 +03:00
2016-12-29 00:57:48 +03:00
header = """Minetest Lua Modding API Reference 0.4.15
========================================="""
text = text.replace(header, "")
#
# Generating HTML
#
print("Generating HTML...")
md = markdown.Markdown(extensions=['markdown.extensions.toc'])
2015-11-21 18:26:52 +03:00
html = md.convert(text)
2015-11-22 19:41:52 +03:00
print("Post-generation replacements...")
links = """<ul>
<li>More information at <a href="http://www.minetest.net/">http://www.minetest.net/</a></li>
<li>Developer Wiki: <a href="http://dev.minetest.net/">http://dev.minetest.net/</a></li>
</ul>"""
2015-11-21 18:26:52 +03:00
html = html.replace("{{", "{ {")
html = html.replace(links, "")
credit = "This page was last updated "
credit += datetime.date.today().strftime("%d/%B/%Y")
credit += ".<br />See <a href=\"https://github.com/minetest/minetest/blob/master/doc/lua_api.txt\">doc/lua_api.txt</a> for the latest version (in plaintext)."
credit += "<br />Generated using <a href=\"https://github.com/rubenwardy/minetest_modding_book/blob/gh-pages/update_lua_api.py\">a Python script</a>."
links += credit
html = html.replace("<h2 id=\"programming-in-lua\">", links + "<h2 id=\"programming-in-lua\">")
2015-11-22 19:41:52 +03:00
print("Parsing HTML...")
soup = BeautifulSoup(html, 'html.parser')
pattern = re.compile('[\W]+')
lis = soup.find_all("li")
index = {}
# Build index of anchors
headings = soup.find_all({"h1", "h2", "h3", "h4", "h5", "h6"})
for tag in headings:
if tag.has_attr("id"):
index[tag["id"]] = True
if tag.has_attr("name"):
index[tag["name"]] = True
# Add anchors to <li>s containing <code>
for li in lis:
code = li.find_all('code')
if len(code) > 0:
key = get_key(index, li, code[0].string)
if key is not None:
index[key] = True
#print("Created " + key)
new_tag = soup.new_tag('a', href="#" + key)
new_tag['class'] = "anchor"
new_tag['name'] = key
new_tag.string = "#"
li.insert(0, new_tag)
html = str(soup)
#
# Writing to file
#
print("Writing to file...")
file = open("lua_api.html", "w")
file.write("---\ntitle: Lua Modding API Reference\nlayout: default\n---\n")
file.write("<div class='notice'>\n")
file.write("<h2>This is lua_api.txt nicely formated: I did not write this</h2>\n")
file.write(credit)
file.write("</div>\n")
file.write("<h2 id=\"table-of-contents\">Table of Contents</h2>\n")
file.write(md.toc)
file.write(html)
file.close()
print("Done")