forked from aitatanit/python-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate_href_html.py
More file actions
39 lines (33 loc) · 1.51 KB
/
translate_href_html.py
File metadata and controls
39 lines (33 loc) · 1.51 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
from bs4 import BeautifulSoup
import json
import sys
# -------------------------------------------------------------------------------
#
# Script that converts inter-User Guide href in HTML NBs
# (using ./inputs/translate.json)
#
# -------------------------------------------------------------------------------
NAME = "translate_href-html" # file name
# Replace 'nbviewer'-domain hrefs to plot.ly
# and translate URL tails in HTML soup
def replace_href(soup, domains, translate):
for a in soup.findAll('a'):
for domain in [domains['nbviewer'],domains['plotly-ext'],'./']:
if a['href'].startswith(domain):
print "[{}]".format(NAME), '... link to translate found:', a['href']
a['href'] = a['href'].replace(domain, domains['plotly-int'])
for old, new in translate.items():
if old in a['href']:
a['href'] = a['href'].replace(old, new)
break
elif new in a['href']:
break
else:
print "[{}]".format(NAME), '!!! URL tail not found in translate.json'
print "[{}]".format(NAME), '!!! make sure the translated url exists'
print "[{}]".format(NAME), '... link translated to:', a['href']
return soup
# -------------------------------------------------------------------------------
def translate(soup, domains, translate):
soup = replace_href(soup, domains, translate)
return soup