-
Notifications
You must be signed in to change notification settings - Fork 761
/
insert_github_links.py
45 lines (36 loc) · 1.28 KB
/
insert_github_links.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
"""Replace paragraph symbol with link to github with line numbering."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import edward
import inspect
import glob
import string
from bs4 import BeautifulSoup
def find_filename_and_line(method_name):
components = string.split(method_name, '.')
method_obj = edward
for i in range(1, len(components)):
method_obj = getattr(method_obj, components[i])
rel_path = inspect.getsourcefile(method_obj).split("edward/", 1)[1]
line_no = inspect.getsourcelines(method_obj)[-1]
return rel_path, line_no
path = ("api/*.html")
filenames = glob.glob(path)
for filename in filenames:
soup = BeautifulSoup(open(filename), 'html.parser')
github = "https://github.com/blei-lab/edward/blob/master/"
for a in soup.find_all("a", "headerlink"):
try:
rel_path, line_no = find_filename_and_line(a['href'][1:])
link = github + rel_path + '#L' + str(line_no)
a['href'] = link
a['class'] = 'u-pull-right'
a['title'] = "Link to definition on GitHub."
a.string.replace_with("[source]")
except: # e.g., if method_obj is a property
a['href'] = ""
a.string.replace_with("")
html = str(soup)
with open(filename, 'wb') as file:
file.write(html)