-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline.py
75 lines (65 loc) · 3.14 KB
/
inline.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
import sys, base64
from os import listdir
from os.path import isfile, join, realpath, dirname
def guessMimetype(filename):
extension = filename.split('.')[-1].lower()
knowntypes = {'wav': 'audio/x-wav',
'js': 'application/javascript',
'otf': 'application/font-sfnt',
'ttf': 'application/font-sfnt'}
if extension in knowntypes:
return knowntypes[extension]
else:
return 'application/octet-stream'
def inlineBinaryFile(prefix, postfix, filename, outstream):
data = base64.b64encode(open(filename).read())
mimetype = guessMimetype(filename)
print >>outstream, '{0}data:{1};base64,{2}{3}'.format(prefix, mimetype, data, postfix)
def inlineJavascript(filename, indentation, outstream):
jslines = [line.rstrip() for line in open(filename).readlines()]
print >>outstream, '{0}<script type="text/javascript">'.format(indentation * ' ')
print >>outstream, '{0}<![CDATA['.format((indentation * 2) * ' ')
for jsline in jslines:
print >>outstream, '{0}{1}'.format((indentation * 3) * ' ', jsline)
print >>outstream, '{0}//]]>'.format((indentation * 2) * ' ')
print >>outstream, '{0}</script>'.format(indentation * ' ')
def processLine(filenames, line, outstream):
for filename in filenames:
if '"'+filename+'"' in line:
if filename.endswith('.js'):
print "Javascript found! Let's try to inline {0} with mimetype {1}...".format(filename, guessMimetype(filename))
inlineJavascript(filename, len(line) - len(line.lstrip(' ')), outstream)
return
else:
print "Binary file found! Let's try to base64 encode {0} with mimetype {1}...".format(filename, guessMimetype(filename))
start = line.find(filename)
prefix = line[:start]
postfix = line[start+len(filename):]
inlineBinaryFile(prefix, postfix, filename, outstream)
return
## If we made it down to here, we didn't find any file reference. We can safely just output the input line.
print >>outstream, line
def processFile(path, infilename, outfilename):
files = [f for f in listdir(path) if isfile(join(path,f))]
print "Found {0} files in the input directory".format(len(files))
input = [line.rstrip() for line in open(infilename).readlines()]
print "Read {0} lines from {1}".format(len(input), infilename)
outfile = open(outfilename, 'w')
for line in input:
processLine(files, line, outfile)
outfile.flush()
outfile.close()
if __name__ == '__main__':
print "Tech Talk Tuesday Timer Inliner Tool for SVG (or T^4ITS) v0.1 (c) 2013-2014 A.T.Brask <[email protected]>"
print ""
if len(sys.argv) != 3:
print "Syntax: python inline.py [input file] [output file]"
exit()
infilename = realpath(sys.argv[1])
outfilename = realpath(sys.argv[2])
mypath = dirname(infilename)
print "Input file: {0}".format(infilename)
print "Output file: {0}".format(outfilename)
print "Processing..."
processFile(mypath, infilename, outfilename)
print "Done."