-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathminify.py
More file actions
115 lines (106 loc) · 3.25 KB
/
minify.py
File metadata and controls
115 lines (106 loc) · 3.25 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import codecs, json, os, re
from urllib2 import Request, urlopen
from urllib import urlencode
replacements = {
'contentDiv': 'c',
'headerClass': 'h',
'footerClass': 'f',
'submitText': 'b',
'resultDiv': 'r',
'resultText': 'e',
'geoDiv': 'g',
'headerText': 't',
'queryLabel': 'l',
'resultTbody': 'o',
'docsDiv': 'd',
'translationsDiv': 's',
'ajax(': 'a(',
'getRequest': 'gr',
'jsonParse': 'j',
'byId(': 'x(',
'newElement(': 'ne(',
'updateElement(': 'o(',
'updateTable(': 'z(',
'makeClickable(': 'ma(',
'ie8_fix(': 'ix(',
'ie8_fix_div': 'i8',
'nullFunction': 'nf',
'setMap': 'ms',
'unSetMap': 'mu',
'MapOpacity': 'mo',
'get_internal': 'gi',
'parse_edns': 'gd',
'query(': 'q(',
'currentQuery': 'cq',
'setLanguage': 'sl',
'getLanguage': 'gl',
'defaultLanguage': 'dl',
'translations': 'lo',
'protocol': 'p',
'myIp': 'mi',
'currentLanguage': 'cl',
'init(': 'k(',
'footerLanguages': 'i',
'contact(': 'cn(',
'calculateZoomLevel': 'cz',
'APIendpoint': 'ep',
'parse_fingerprint': 'pfp',
'encodeHTML': 'eh',
'isUrl': 'iu',
'zoomLevel': 'zl',
'cache': 'ch'
}
f = codecs.open('source.html', 'r', 'utf-8')
contents = f.read()
f.close()
i = 0
lang = langOld = re.search('var translations = {(.*)},', contents, re.DOTALL).group(1)
langKeys = re.search('var translations = {.*{(.*)},', contents, re.DOTALL).group(1)
for k in re.findall(r'(\w*): ', langKeys, re.MULTILINE):
contents = re.sub(r'\bl.%s\b' % k, "l[%i]" % i, contents)
if k == 'LANG':
contents = contents.replace('translations[lng].LANG', 'translations[lng][%i]' % i)
lang = re.sub(r'\t%s: ' % k, '', lang)
i+=1
contents = contents.replace(langOld, lang.replace('{', '[').replace('}', ']'))
for what, to in replacements.iteritems():
contents = contents.replace(what, to)
post = {
'code': contents.encode('utf-8'),
'options[removeEmptyAttributes]': 'false',
'options[useShortDoctype]': 'true',
'options[removeEmptyElements]': 'false',
'options[customAttrAssign]': '',
'options[removeScriptTypeAttributes]': 'true',
'options[collapseBooleanAttributes]': 'false',
'options[removeCommentsFromCDATA]': 'false',
'options[removeComments]': 'false',
'options[minifyCSS]': 'true',
'options[keepClosingSlash]': 'false',
'options[minifyJS]': 'true',
'options[ignoreCustomComments]': '',
'options[customAttrCollapse]': '',
'options[removeStyleLinkTypeAttributes]': 'true',
'options[minifyURLs]': 'true',
'options[maxLineLength]': '',
'options[removeAttributeQuotes]': 'false',
'options[removeCDATASectionsFromCDATA]': 'false',
'options[preserveLineBreaks]': 'false',
'options[removeOptionalTags]': 'false',
'options[processScripts]': '',
'options[caseSensitive]': 'true',
'options[removeIgnored]': 'false',
'options[conservativeCollapse]': 'false',
'options[removeRedundantAttributes]': 'true',
'options[customAttrSurround]': '',
'options[collapseWhitespace]': 'true',
'options[preventAttributesEscaping]': 'true',
'options[lint]': 'false'
}
req = urlopen(Request('http://refresh-sf.herokuapp.com/html/', data=urlencode(post)))
contents = json.loads(req.read())['code']
contents += "<!--https://github.com/vladc/ip-api.com-->"
out = codecs.open('index.html', 'w', 'utf-8')
out.write(contents)
out.close()
print 'original:', os.stat('source.html').st_size, 'minified:', os.stat('index.html').st_size