chriswarbo-net: b571a435d3e2d7b186eacd1f66a2c18a2a2ca9f0
1: #!/usr/bin/env python3
2:
3: # Update a HTML file to use relative links, e.g. '../css/foo.css' rather than
4: # '//chriswarbo.net/css/foo.css' or '/css/foo.css'
5:
6: import sys
7: err = sys.stderr.write
8:
9: import os
10: to_root = os.getenv('TO_ROOT')
11: del(os)
12:
13: if to_root is None:
14: err('No TO_ROOT given, not relativising\n')
15: sys.exit(0)
16:
17: import fileinput
18: stdin = ""
19: for line in fileinput.input():
20: stdin += line
21: del(fileinput)
22:
23: from bs4 import BeautifulSoup
24: doc = BeautifulSoup(stdin, 'html.parser')
25: del(BeautifulSoup, stdin)
26:
27: def prefix(attr):
28: def f(e):
29: if attr in e.attrs and e[attr].startswith(u'/'):
30: e[attr] = to_root + e[attr]
31: return f
32:
33: for tag_attr in [
34: ('a' , 'href'),
35: ('link' , 'href'),
36: ('img' , 'src' ),
37: ('script', 'src' )
38: ]:
39: tag, attr = tag_attr
40: f = prefix(attr)
41: for elem in doc.find_all(tag):
42: f(elem)
43:
44: print(doc)
Generated by git2html.