Commits

Rodrigo Fonseca committed db7b179

Initial commit.
Working script with a few options.
Add MIT License

  • Participants
  • Tags 1.0.0

Comments (0)

Files changed (2)

+Copyright (c) 2012, Rodrigo Jardim da Fonseca <http://dev.fonseca.eti.br/>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of 
+this software and associated documentation files (the "Software"), to deal in 
+the Software without restriction, including without limitation the rights to 
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
+of the Software, and to permit persons to whom the Software is furnished to do 
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all 
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
+SOFTWARE.

File hjc-fusion.py

+#-------------------------------------------------------------------------------
+# Name:        hjc-fusion
+# Purpose:     Join the HTML with the external Javascript and CSS files into
+#              a unique HTML file.
+#
+# Author:      Rodrigo Fonseca
+#
+# Created:     21/03/2012
+# Copyright:   (c) 2012, Rodrigo Jardim da Fonseca <http://dev.fonseca.eti.br/>
+# Licence:     MIT
+#-------------------------------------------------------------------------------
+#!/usr/bin/env python
+
+import re
+import os
+import argparse
+import subprocess
+
+# Path to the yuicompressor
+yuicompressor = None
+
+def isminified (text):
+    result = re.match(r'.*\.min\.(js|css)$',text)
+    if (result is not None):
+        return True
+    else:
+       return False
+
+def minify(filename):
+    minfilename = re.sub(r'^(.*)\.(js|css)$','\g<1>.min.\g<2>',filename)
+    if (os.path.isfile(filename)):
+        subprocess.call(['java', '-jar', yuicompressor, '-o', minfilename, filename])
+    if (os.path.isfile(minfilename)):
+        return minfilename
+    else:
+        return None
+
+def getjs(matchobj):
+    result = re.match(r'.*src=(?:\'|\")(.*?)(?:\'|\").*',matchobj.group(1))
+    filename = result.group(1)
+    if not isminified(filename):
+        filename = minify(filename)
+    with open(filename) as js_file:
+        js = js_file.read()
+    return '<script type="text/javascript">'+js+'</script>'
+
+def getcss(matchobj):
+    result = re.match(r'.*href=(?:\'|\")(.*?)(?:\'|\").*',matchobj.group(1))
+    filename = result.group(1)
+    if not isminified(filename):
+        filename = minify(filename)
+    with open(filename) as css_file:
+        css = css_file.read()
+    return '<style type="text/css">'+css+'</style>'
+
+def main():
+    parser = argparse.ArgumentParser(description="Join the HTML with the external Javascript and CSS files into a unique HTML file.")
+    parser.add_argument('input', metavar='input_file', help='original HTML file')
+    parser.add_argument('-o', '--output',  metavar='output_file', default='output.html', help='new HTML file')
+    parser.add_argument('-yuic','--yuicompressor', metavar='input_file', default='yuicompressor-2.4.7.jar',help="Path to the yuicompressor")
+    args = parser.parse_args()
+    global yuicompressor
+    yuicompressor = args.yuicompressor
+    with open(args.input) as html_file:
+        text = html_file.read()
+        text = re.sub(r'(<script.*?src\s*=.*?>\s*?</script>)',getjs, text)
+        text = re.sub(r'(<link.+?href\s*=.+? />)',getcss,text)
+        text = re.sub(r'[\n\r\t]','',text);
+        with open(args.output, mode='w') as new_file:
+            new_file.write(text)
+
+if __name__ == '__main__':
+    main()
+
+
+