-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgendocset.py
More file actions
374 lines (353 loc) · 11.6 KB
/
gendocset.py
File metadata and controls
374 lines (353 loc) · 11.6 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Generate Dash docset for Git
http://git-scm.com/docs
"""
import itertools
import json
import os
import re
import sqlite3
from shutil import copy
from sys import argv
import requests
from bs4 import BeautifulSoup as bs
def initialise():
global docset_name
global output
global root_url
global parser
global db
global cur
global sections
global lang
docset_name = "Git.docset"
output = f"{docset_name}/Contents/Resources/Documents"
root_url = "http://git-scm.com/docs"
parser = "html.parser"
docpath = f"{output}/"
if not os.path.exists(docpath):
os.makedirs(docpath)
copy("Git-Icon-1788C.png", f"{docset_name}/icon.png")
db = sqlite3.connect(f"{docset_name}/Contents/Resources/docSet.dsidx")
cur = db.cursor()
try:
cur.execute("DROP TABLE searchIndex;")
except Exception:
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);")
sections = {
"index.html": ("Git - Reference", "Index"),
"docs/git.html": ("git", "Command"),
}
if len(argv) > 1:
lang = argv[1]
else:
lang = "en"
def get_git(url):
global lang
global sections
# Unwisely, the Git manpages translation project
# chose to translate tag id attributes.
# Furthermore, current translations of 'git.html'
# are lacking the 'Guides' section etc.
# So, always fetch the English version to
# trawl for links, then download and save
# the preferred language version
page = requests.get(url).text
soup = bs(page, parser)
title = soup.find("title")
soup = soup.find(id="main")
files = {"gitattributes", "gitignore", "gitmailmap", "gitmodules"}
sects = {
"_high_level_commands": "Command",
"_low_level_commands": "Command",
"_guides": "Guide",
"_repository_command_and_file_interfaces": "Interface",
"_file_formats_protocols_and_other_developer_interfaces": None,
}
pattern = str(list(sects.keys()))[3:-3].replace("', '", "|")
headings = soup.findAll("h2", {"id": re.compile(pattern)})
types = list(sects.values())
for i in range(0, len(sects)):
dlists = headings[i].parent.findAll("div", attrs={"class": "dlist"})
for dlist in dlists:
links = dlist.findAll("a", {"class": False, "href": True})
for link in links:
path = link["href"].lstrip("/")
if path.startswith("docs/git-"):
name = path.split("-", 1)[1]
else:
name = path.replace("docs/", "")
type = types[i]
if type == "Interface":
if name in files:
type = "File"
elif name == "githooks":
type = "Hook"
elif type is None:
if name.startswith("gitformat"):
type = "File"
elif name.startswith("gitprotocol"):
type = "Protocol"
sections.update({f"{path}.html": (name, type)})
fix_links(soup)
if lang != "en":
page = requests.get(f"{url}/{lang}").text
soup = bs(page, parser)
soup = soup.find(id="main")
fix_links(soup, update_sections=False)
soup.insert(0, title)
folder = os.path.join(output, "docs")
os.makedirs(folder, exist_ok=True)
with open(
os.path.join(output, "docs/git.html"),
"w",
encoding="iso-8859-1",
errors="ignore",
) as f:
f.write(str(soup))
def get_index(url):
page = requests.get(url).text
soup = bs(page, parser)
title = soup.find("title")
soup = soup.find(id="main")
soup.insert(0, title)
fix_links(soup, index=True)
with open(
os.path.join(output, "index.html"),
"w",
encoding="iso-8859-1",
errors="ignore",
) as f:
f.write(str(soup))
def add_docs(start, end):
global sections
start_prev = len(sections) - 1
for path, (name, _) in dict(
itertools.islice(sections.items(), start, end)
).items():
folder = os.path.join(output)
for i in range(0, len(path.split("/")) - 1):
folder += f"/{path.split(f'/')[i]}"
if not os.path.exists(folder):
os.makedirs(folder)
if "#" not in path:
page_id = path.split("/")[-1].split(".")[0]
get_doc(page_id, fixlinks=True)
start = end + 1
end = len(sections) - 1
if end > start_prev:
add_docs(start, end)
def get_doc(
page_id,
url="http://git-scm.com/docs",
ignore_lang=False,
strip_ext=True,
txt2html=False,
fixlinks=False,
update_sections=False,
type="",
):
global lang
global sections
print(f"Downloading document: {page_id}")
if ignore_lang or lang == "en":
suffix = ""
else:
suffix = f"/{lang}"
response = requests.get(f"{url}/{page_id}{suffix}", stream=True)
if response.status_code != 200:
print(f"HTTP error: {response.status_code}")
return
if strip_ext:
page_id = page_id.split(".")[0]
if txt2html:
doc = (
f"<html><body><pre><title>{page_id}"
f"</title>{response.text}</pre></body></html>"
)
else:
soup = bs(response.text, parser)
title_tag = soup.new_tag("title")
title_tag.append(page_id)
soup = soup.find(id="main")
soup.insert(0, title_tag)
if fixlinks:
soup = fix_links(soup)
doc = str(soup)
with open(
os.path.join(output, f"docs/{page_id}.html"),
"w",
encoding="iso-8859-1",
errors="ignore",
) as f:
f.write(doc)
print("Success")
if update_sections:
sections.update({f"docs/{page_id}.html": (page_id, type)})
def fix_links(soup, index=False, update_sections=True):
global lang
global sections
for link in soup.findAll("a", {"href": True}):
if link.attrs:
pattern = re.compile("^(http|mailto)")
if not re.match(pattern, link["href"]):
if "#" not in link.attrs["href"]:
path_noext = re.split(f"(^/|/{lang})", link["href"])[2]
path = f"{path_noext}.html"
if index:
link["href"] = path
else:
if path not in sections:
if path_noext.startswith("docs/git-"):
name = path_noext.split("-", 1)[1]
else:
name = path_noext.replace("docs/", "")
if link.text.endswith("[1]"):
type = "Command"
elif link.text.endswith("[5]"):
type = "File"
elif (
link.text.endswith("[7]")
or link.text[0].isupper()
):
type = "Guide"
else:
type = "Unknown"
if update_sections:
sections.update({path: (name, type)})
link["href"] = path.replace("docs/", "")
else:
link_parts = link["href"].split("#")
if index:
ext = ".html"
else:
ext = ""
if link_parts[0] != "":
link[
"href"
] = f"{link_parts[0][1:]}{ext}#{link_parts[1]}"
return soup
def misc_fixes():
global lang
global sections
page_ids = {
"api-simple-ipc": "Interface",
"api-merge": "Interface",
"api-error-handling": "Interface",
"api-parse-options": "Interface",
"partial-clone": "Guide"
}
for page_id, doc_type in page_ids.items():
get_doc(
page_id,
fixlinks=False,
update_sections=True,
type=doc_type,
)
get_doc(
"gitweb.conf",
ignore_lang=True,
strip_ext=False,
fixlinks=True,
update_sections=True,
type="File",
)
url = "https://api.github.com/repos/git/git/contents/Documentation/howto"
dir = json.loads(requests.get(url).content.decode())
url = "https://raw.githubusercontent.com/git/git/master/Documentation/howto"
for entry in dir:
get_doc(
entry["name"],
url,
ignore_lang=True,
txt2html=True,
update_sections=True,
type="Guide",
)
try:
os.rmdir(os.path.join(output, "docs/howto"))
except (OSError):
pass
deletions = [
"docs/api-index.html",
"docs/howto/update-hook-example.html",
"docs/howto/setup-git-server-over-http.html",
"docs/howto/revert-a-faulty-merge.html",
"docs/howto-index.html",
# pt_BR clanger
"docs/gitignorar.html",
]
for path in deletions:
try:
sections.pop(path)
except (KeyError):
pass
if lang == "en":
jump_variables = "_environment_variables"
else:
if lang == "de":
jump_commands = "_git_befehle"
jump_variables = "_umgebungsvariablen"
elif lang == "pt_BR":
jump_commands = "_os_comandos_do_git"
jump_variables = "_as_vari%C3%A1veis_do_ambiente"
with open(
os.path.join(output, "index.html"),
"r+",
encoding="iso-8859-1",
errors="ignore",
) as f:
html = f.read().replace("_git_commands", jump_commands)
f.seek(0)
f.write(html)
f.truncate()
sections.update(
{f"docs/git.html#{jump_variables}": ("Variables", "Environment")}
)
sections.update({"docs/gitglossary.html": ("Git Glossary", "Glossary")})
sections.update({"docs/gitweb.html": ("gitweb", "Interface")})
def update_db(sections):
for path, (name, type) in sections.items():
cur.execute(
"INSERT OR IGNORE INTO searchIndex(name,type,path) VALUES (?,?,?)",
(name, type, path),
)
print("DB add >> name: %s, path: %s" % (name, path))
def add_info_plist():
name = docset_name.split(".")[0]
info = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"'
' "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
'<plist version="1.0">\n'
"<dict>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>{0}</string>\n"
" <key>CFBundleName</key>\n"
" <string>{1}</string>\n"
" <key>DocSetPlatformFamily</key>\n"
" <string>{2}</string>\n"
" <key>isDashDocset</key>\n"
" <true/>\n"
" <key>dashIndexFilePath</key>\n"
" <string>index.html</string>\n"
"</dict>\n"
"</plist>\n".format(name.lower(), name, name.lower())
)
with open(f"{docset_name}/Contents/Info.plist", "w") as f:
f.write(info)
if __name__ == "__main__":
initialise()
get_git(f"{root_url}/git")
get_index(root_url)
add_docs(2, len(sections) - 1)
misc_fixes()
update_db(sections)
add_info_plist()
db.commit()
db.close()