forked from denilsonsa/small_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed-external-files-into-html.py
executable file
·75 lines (63 loc) · 2.13 KB
/
embed-external-files-into-html.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# vi:ts=4 sw=4 et
from __future__ import print_function
import re
import sys
def embedScriptsAndStyles(fin, fout):
"""Reads an HTML file and tries to embed the scripts and stylesheets.
Only one script or stylesheet per input line is supported. The entire line
is discarded, so there should be no other markup in there. Any attribute
(such as language or type) is also discarded.
fin: File-like input object for reading.
fout: File-like output object for writing.
"""
re_script = re.compile(r''' \s*
<script \s [^>]* (?:
src="([^">]+)" |
src='([^'>]+)' |
src=([^\s>'"]+)
) [^>]* > \s* </script>
''', re.I | re.VERBOSE)
re_style = re.compile(r''' \s*
<link \s [^>]* rel=['"]?stylesheet['"]? \s [^>]* (?:
href="([^">]+)" |
href='([^'>]+)' |
href=([^\s>'"]+)
) [^>]* > |
<link \s [^>]* (?:
href="([^">]+)" |
href='([^'>]+)' |
href=([^\s>'"]+)
) \s [^>]* rel=['"]?stylesheet['"]? [^>]* >
''', re.VERBOSE)
for line in fin:
prefix = ''
external_file = None
suffix = ''
match = re_script.match(line)
if match:
prefix = '<script>\n'
external_file = [g for g in match.groups() if g][0]
suffix = '\n</script>\n'
else:
match = re_style.match(line)
if match:
prefix = '<style>\n'
external_file = [g for g in match.groups() if g][0]
suffix = '\n</style>\n'
if external_file:
fout.write(prefix)
fout.writelines(open(external_file))
fout.write(suffix)
else:
fout.write(line)
def main():
if len(sys.argv) == 1:
embedScriptsAndStyles(sys.stdin, sys.stdout)
elif len(sys.argv) == 2:
embedScriptsAndStyles(open(sys.argv[1]), sys.stdout)
else:
print('Wrong arguments.')
if __name__ == "__main__":
main()