import markdown, urllib2, datetime, re, string from bs4 import BeautifulSoup 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() text = unicode(text, "utf-8") print("Pre-generation replacements...") 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']) html = md.convert(text) print("Post-generation replacements...") links = """
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("\n")
file.write("This is lua_api.txt nicely formated: I did not write this
\n")
file.write(credit)
file.write("\n")
file.write("Table of Contents
\n")
file.write(md.toc)
file.write(html)
file.close()
print("Done")