-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rebuild.py
executable file
·151 lines (113 loc) · 4.85 KB
/
rebuild.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
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
#!/usr/bin/env python
# encoding: utf-8
import copy, os, re, sqlite3, string, urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup, NavigableString, Tag
DOCUMENTS_DIR = os.path.join('Slim_Framework.docset', 'Contents', 'Resources', 'Documents')
HTML_DIR = os.path.join('www.slimframework.com/docs/v4')
db = sqlite3.connect('Slim_Framework.docset/Contents/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);')
page = open(os.path.join(DOCUMENTS_DIR, HTML_DIR, 'index.html'), encoding='utf8').read()
soup = BeautifulSoup(page, 'html5lib')
menu = soup.find('div', class_='col-md-3')
headline = ""
for item in menu.find_all():
if item.name == "h3":
headline = item.text.strip()
if item.name == "ul":
for item in item.find_all('a'):
title = item.text.strip()
name = headline + " - " + title
if item.attrs["href"].startswith("http"):
continue
path = os.path.join(HTML_DIR, item.attrs["href"])
cur.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)', (name, 'Guide', path))
print('name: %s, path: %s' % (name, path))
continue
main = soup.find('div', class_='section')
for root, dirs, files in os.walk(os.path.join(DOCUMENTS_DIR, HTML_DIR), topdown=True):
# build search index and tables of contents
for filename in files:
if not filename.endswith('.html'):
continue
page = open(os.path.join(root, filename), encoding='utf8').read()
soup = BeautifulSoup(page, 'html5lib')
main = soup.find('div', class_='docs-content')
if not main:
continue
for tag in main.find_all(['h1', 'h2', 'h3', 'h4']):
dashAnchor = tag.find('a', class_='dashAnchor')
if dashAnchor:
continue
text = tag.text.strip()
try: print('adding toc tag for section: %s' % text)
except: pass
name = '//apple_ref/cpp/Section/' + urllib.parse.quote(text.encode('utf8'), '')
dashAnchor = BeautifulSoup('<a name="%s" class="dashAnchor"></a>' % name, 'html5lib').a
tag.insert(0, dashAnchor)
# strip unecessary javascript
[t.extract() for t in soup("script")]
# strip Google-Fonts
for link in soup.find_all("link", { "rel" : "stylesheet" }):
test = re.compile("^https?.*googleapis.*", re.IGNORECASE)
if "href" in link.attrs and test.match(link["href"].strip()):
link.extract()
for a in soup.find_all("a"):
test = re.compile("^https?.*travis-ci.*", re.IGNORECASE)
if "href" in a.attrs and test.match(a["href"].strip()):
a.extract()
for a in soup.find_all("a"):
test = re.compile("^https?.*cdn.jsdelivr.*", re.IGNORECASE)
if "href" in a.attrs and test.match(a["href"].strip()):
a.extract()
# remove header navbar with blog link
try:
soup.find('nav', class_='navbar-fixed-top').decompose()
except AttributeError:
pass
# remove Slim logo
try:
soup.find('header', class_='site-header').decompose()
except AttributeError:
pass
# remove left menu
try:
soup.find('form', class_='searchbox').parent.decompose()
except AttributeError:
pass
# remove version switcher
try:
soup.find('button', class_='dropdown-toggle').parent.decompose()
except AttributeError:
pass
# remove link to v3 docs
try:
for alert in soup.find_all('div', class_='alert-info'):
if 'Slim 3 Docs' in str(alert):
alert.decompose()
except (AttributeError, TypeError):
pass
# remove license icon
try:
soup.find('a', {'rel': 'license'}).decompose()
except AttributeError:
pass
# replace donation icon with a tag
try:
donation = soup.find('input', {'type': 'image'}).parent
id = soup.find('input', {'name': 'hosted_button_id'}).get('value')
link = BeautifulSoup(
'<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=' + id + '" alt="Donate with PayPal">Donate with PayPal</a>',
'html.parser'
)
donation.replaceWith(link)
except AttributeError:
pass
fp = open(os.path.join(root, filename), 'w', encoding='utf8')
fp.write(str(soup))
fp.close()
db.commit()
db.close()