-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate_docset.py
More file actions
executable file
·283 lines (230 loc) · 9.88 KB
/
create_docset.py
File metadata and controls
executable file
·283 lines (230 loc) · 9.88 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
#!/usr/bin/env python
# encoding: utf-8
import re
import os
import shutil
import subprocess
import os.path
import codecs
from bs4 import BeautifulSoup
## Tries to find docsetutil
possible_docsetutil_path = [
"/Developer/usr/bin/docsetutil",
"/Applications/Xcode.app/Contents/Developer/usr/bin/docsetutil",
]
docsetutil_path = [path
for path in possible_docsetutil_path
if os.path.exists(path)]
if len(docsetutil_path) == 0:
print ("Could not find docsetutil. Please check for docsetutil's "
"location and set it inside the script.")
exit(1)
docsetutil_path = docsetutil_path[0]
## Script should run in the folder where the docs live
source_folder = os.getcwd()
## Find the Python version of the docs
python_version = None
with codecs.open(os.path.join(source_folder, "index.html"), 'r', encoding="utf-8") as f:
for line in f:
search = re.search("dash; (.*?) documentation</title>", line)
if search:
python_version = search.group(1)
break
search = re.search("<title>.*?dash; (.*? v[^ <]+) ", line)
if search:
python_version = search.group(1)
break
if python_version == None:
print ("I could not find Python's version in the index.html "
"file. Are you in the right folder??")
exit(1)
docset_name = python_version.strip().lower().replace(" ", "_")
dest_folder = os.path.join(source_folder, ("%s.docset/" % docset_name))
def is_something(tag, something):
""" Function to help BeautifulSoup find our tokens """
return (tag.name == "dt"
and tag.has_key("id")
and tag.parent.name == "dl"
and tag.parent['class'][0] == something)
def collect(soup, what, identifier, names):
""" Collects all nodes of a certain type from a BeautifulSoup document """
whats = soup.find_all(lambda tag: is_something(tag, what))
for n in whats:
apple_ref = "//apple_ref/cpp/%s/%s" % (identifier, n["id"])
new_tag = soup.new_tag("a")
new_tag['name'] = apple_ref
n.insert_before(new_tag)
names.append(apple_ref)
def find_existing_file(possible):
path = [path for path in possible if os.path.exists(os.path.join(source_folder, path))]
if len(path) == 0:
print ("Could not find %s. Please check your doc folder structure and "
"try again." % " or ".join(possible))
exit(2)
return path[0]
## Clean up first
if os.path.exists(dest_folder):
shutil.rmtree(dest_folder)
## Create all the necessary folder hierarchy
os.makedirs(dest_folder + "Contents/Resources/Documents/")
docset_folder = dest_folder
dest_folder = os.path.join(dest_folder, "Contents")
## Find the module's index file. It's different in Python's 3 docs
modindex_path = find_existing_file([
"modindex.html",
"py-modindex.html",
])
genindex_path = find_existing_file([
"genindex-all.html",
"genindex.html",
])
## Create Info.plist
with codecs.open(os.path.join(dest_folder, "Info.plist"), "w", encoding="utf-8") as info:
info.write("""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>python.%s</string>
<key>CFBundleName</key>
<string>%s</string>
<key>DocSetPlatformFamily</key>
<string>python</string>
</dict>
</plist>
""" % (python_version.strip().lower().replace(" ", "."), python_version.strip()))
## Create Nodes.xml
dest_folder = os.path.join(dest_folder, "Resources")
nodes = codecs.open(os.path.join(dest_folder, "Nodes.xml"), "w", encoding="utf-8")
nodes.write("""<?xml version="1.0" encoding="UTF-8"?>
<DocSetNodes version="1.0">
<TOC>
<Node type="folder">
<Name>Modules Index</Name>
<Path>%s</Path>
</Node>
</TOC>
</DocSetNodes>
""" % modindex_path)
## Create the tokens file
token_path = os.path.join(dest_folder, "Tokens.xml")
dest_folder = os.path.join(dest_folder, "Documents")
## Copy some static files
shutil.copy(os.path.join(source_folder, "searchindex.js"), dest_folder)
shutil.copy(os.path.join(source_folder ,modindex_path), dest_folder)
shutil.copy(os.path.join(source_folder, genindex_path), dest_folder)
if os.path.exists(os.path.join(source_folder , "library/index.html")):
shutil.copy(os.path.join(source_folder, "library/index.html"), dest_folder)
shutil.copytree(os.path.join(source_folder , "_images"), os.path.join(dest_folder , "_images"))
shutil.copytree(os.path.join(source_folder , "_static"), os.path.join(dest_folder, "_static"))
## I'll hide the header because it makes no sense in a docset
## and messes up Dash
with codecs.open(os.path.join(dest_folder, "_static/basic.css"), "a+", encoding="utf-8") as css:
css.write("div.related {display:none;}\n")
css.write("div.sphinxsidebar {display:none;}\n")
with codecs.open(os.path.join(dest_folder, "_static/default.css"), "a+", encoding="utf-8") as css:
css.write("a.headerlink {display:none;}\n")
css.write("div.bodywrapper {margin: 0 0 0 0px;}")
## Collect pages first
pages = {}
print("figuring out what pages we need to process")
## Collect pages from the modules index
with codecs.open(os.path.join(source_folder, modindex_path), 'r', encoding="utf-8") as f:
for line in f:
search = re.search("<a href=\"(.*)#.*?\"><tt class=\"xref\">(.*?)</tt>", line)
if search:
href = search.group(1)
name = search.group(2)
if not href in pages:
pages[href] = []
apple_ref = "//apple_ref/cpp/Module/%s" % name
pages[href].append(apple_ref)
## Collect pages from the general index
with codecs.open(os.path.join(source_folder, genindex_path), 'r', encoding="utf-8") as f:
for line in f:
for search in re.finditer("(<dt>|, )<a href=\"([^#]+).*?\">", line):
href = search.group(2)
if not href in pages:
pages[href] = []
## Collect pages from the library index
if os.path.exists(os.path.join(source_folder, "library/index.html")):
with codecs.open(os.path.join(source_folder, "library/index.html"), 'r', encoding="utf-8") as f:
for line in f:
for search in re.finditer("<a class=\"reference external\" href=\"([^#\"]+).*?\">", line):
href = "library/" + search.group(1)
if not ("http://" in href or "https://" in href or href in pages):
pages[href] = []
with codecs.open(token_path, "w", encoding="utf-8" ) as tokens:
## Start of the tokens file
tokens.write("""<?xml version="1.0" encoding="UTF-8"?>
<Tokens version="1.0">
""")
counter = 1
total = len(pages)
## Now write to tokens
for href, names in pages.items():
# print progress
print("%s/%s - processing %s" % (counter, total, href))
counter += 1
soup = None
if not os.path.exists (os.path.join(source_folder, href)):
print(" -- not found")
continue
with codecs.open(os.path.join(source_folder, href), "r", encoding="utf-8") as tmp:
soup = BeautifulSoup(tmp)
collect(soup, "class", "cl", names)
collect(soup, "method", "clm", names)
collect(soup, "classmethod", "clm", names)
collect(soup, "function", "func", names)
collect(soup, "exception", "Exception", names)
collect(soup, "attribute", "Attribute", names)
## This adds some hidden tags that makes Dash display this page's
## TOC on the left side of the screen, just like with iOS and OSX docs
toc = soup.find('div', 'sphinxsidebarwrapper').findAll("a", "reference")
if len(toc) > 0:
toc_tag = soup.new_tag("div", style="display:none;")
soup.body.append(toc_tag)
a_tag = soup.new_tag("a")
a_tag["name"] = "#"
toc_tag.append(a_tag)
h3_tag = soup.new_tag("h3")
h3_tag["class"] = "tasks"
h3_tag.append("TOC")
toc_tag.append(h3_tag)
ul_tag = soup.new_tag("ul")
ul_tag["class"] = "tooltip"
toc_tag.append(ul_tag)
for t in toc:
li_tag = soup.new_tag("li")
li_tag["class"] = "tooltip"
ul_tag.append(li_tag)
a_tag = soup.new_tag("a")
a_tag["href"] = t['href']
a_tag.append(t.text)
li_tag.append(a_tag)
# As reported by Dash's author: Module references (added as categories)
# do not have proper anchor in the html files, so I have to add them.
for module in soup.findAll(["div", "span"], id=re.compile(r'^module-.+$')):
a_tag = soup.new_tag("a")
a_tag["name"] = "//apple_ref/cpp/Module/%s" % module["id"][7:]
module.insert(0, a_tag)
if len(names) > 0:
tokens.write("<File path=\"%s\">\n" % href)
for name in names:
tokens.write("\t<Token><TokenIdentifier>%s</TokenIdentifier><Anchor>%s</Anchor></Token>\n" % (name, name))
tokens.write("</File>\n")
newFilePath = os.path.join(dest_folder, href)
if not os.path.exists(os.path.dirname(newFilePath)):
os.makedirs(os.path.dirname(newFilePath)) # might be a bug...if given something/test.html, it creates test.html as a directory!
with codecs.open(newFilePath, "w", encoding="utf-8") as newFile:
newFile.write(unicode(soup))
tokens.write("</Tokens>")
try:
print("calling docsetutil")
subprocess.call([docsetutil_path, "index", docset_folder])
except OSError as e:
print("something went wrong trying to call docsetutil: ", e)
## Cleanup
os.remove(os.path.join(docset_folder, "Contents/Resources/Nodes.xml"))
os.remove(os.path.join(docset_folder, "Contents/Resources/Tokens.xml"))
print("done")