forked from Moddus/tldr-python-dash-docset
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
executable file
·96 lines (81 loc) · 4 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import requests as req, zipfile, io, markdown2 as md, sqlite3, os, shutil, tarfile
import re
html_tmpl = """<html><!-- Online page at {url} -->
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../style.css"/>
</head>
<body>
<section id="tldr">
<div id="page">{content}</div>
</section>
</body>
</html>"""
online_url = "https://github.com/tldr-pages/tldr/blob/main/pages"
doc_source = "https://github.com/tldr-pages/tldr/archive/refs/heads/main.zip"
docset_path = "tldrpages.docset"
doc_path_contents = docset_path + "/Contents/"
doc_path_resources = docset_path + "/Contents/Resources/"
doc_path = docset_path + "/Contents/Resources/Documents/"
doc_pref = "tldr-main/pages/"
if os.path.exists(doc_path):
try: shutil.rmtree(doc_path)
except OSError as e:
print("Could not delete dirs " + e.strerror)
raise SystemExit
os.makedirs(doc_path)
try: r = req.get(doc_source)
except req.exceptions.ConnectionError:
print("Could not load tldr-pages from " + doc_source)
raise SystemExit
if r.status_code != 200:
print("Could not load tldr-pages.")
raise SystemExit
db = sqlite3.connect(doc_path_resources+"/"+"docSet.dsidx")
cur = db.cursor()
try: cur.execute('DROP TABLE searchIndex;')
except: pass
cur.execute('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);')
cur.execute('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);')
# Generate tldr pages to HTML documents
markdowner = md.Markdown(extras=["code-friendly"])
with zipfile.ZipFile(io.BytesIO(r.content), "r") as archive:
for path in archive.namelist():
if path.startswith(doc_pref) and path.endswith(".md"):
cmd_name = path[path.rfind("/")+1:-3]
sub_dir = path[len(doc_pref):path.rfind("/")]
sub_path = os.path.join(doc_path, sub_dir)
if not os.path.exists(sub_path):
try: os.mkdir(sub_path)
except OSError as e:
print("Could not create dir " + e.strerror)
raise SystemExit
if sub_dir != "common":
cur.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)', (sub_dir + " " + cmd_name, 'Command', sub_dir+'/'+cmd_name+".html"))
else:
cur.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)', (cmd_name, 'Command', sub_dir+'/'+cmd_name+".html"))
doc = markdowner.convert(archive.read(path))
doc = re.sub(r'{{(.*?)}}', r'<em>\1</em>', doc)
doc = html_tmpl.format(url=online_url+'/'+sub_dir+'/'+cmd_name+'.md', content=doc)
with open(os.path.join(doc_path, path[len(doc_pref):].replace(".md", ".html")), "wb") as html:
html.write(doc.encode("utf-8"))
db.commit()
db.close()
# Generate tldr pages index.html
with open(os.path.join(doc_path, "index.html"), "w+") as html:
html.write('<html><!-- Online page at '+online_url+' --><head><meta charset="UTF-8"><title>TLDR pages</title></head><body><h1>TLDR pages</h1><br/>powered by <a href="http://tldr-pages.github.io">tldr-pages.github.io/</a>')
for dir in sorted(os.listdir(doc_path)):
if os.path.isdir(os.path.join(doc_path, dir)):
html.write('<a name="//apple_ref/cpp/Section/{name}" class="dashAnchor"></a><h2>{name}</h2><ul>'.format(name=dir))
html.writelines(['<li><a href="%s/%s">%s</a></li>' % (dir, f, f[:-5]) for f in sorted(os.listdir(os.path.join(doc_path, dir)))])
html.write("</ul>")
html.write('</body></html>')
# copy static content
shutil.copyfile("static/style.css", doc_path+"/style.css")
shutil.copyfile("static/Info.plist", doc_path_contents+"/Info.plist")
shutil.copyfile("static/icon.png", docset_path+"/icon.png")
shutil.copyfile("static/[email protected]", docset_path+"/[email protected]")
# create tgz
with tarfile.open("tldr_pages.tgz", "w") as docset:
docset.add(docset_path)