-
Notifications
You must be signed in to change notification settings - Fork 3
/
drfdoc2set.py
executable file
·41 lines (31 loc) · 1.31 KB
/
drfdoc2set.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
#!/usr/bin/env python
import os, re, sqlite3, sys
from bs4 import BeautifulSoup, NavigableString, Tag
docset_path = 'django-rest-framework.docset'
print(docset_path)
db = sqlite3.connect('%s/Contents/Resources/docSet.dsidx' % docset_path)
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);')
docpath = '%s/Contents/Resources/Documents' % docset_path
page = open(os.path.join(docpath,'index.html')).read()
soup = BeautifulSoup(page, features="html.parser")
any = re.compile('.*')
for tag in soup.find_all('a', {'href':any}):
name = tag.text.strip()
if len(name) > 0:
path = tag.attrs['href'].strip()
if path.split('/')[0] in ['tutorial', 'api-guide', 'topics']:
if '#' in path:
parts = path.split('#')
path = '%s.html#%s' % (
parts[0] if parts[0][-1] != '/' else parts[0][:-1], parts[1])
path = path.replace('/#', '.html#')
cur.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)', (name, 'Guide', path))
print('name: %s, path: %s' % (name, path))
db.commit()
db.close()