#!/usr/bin/env python3 import sys, re, os from collections import deque from bs4 import BeautifulSoup, Tag from jsmin import jsmin from csscompressor import compress IGNORED = 'gui//webui.js' if __name__ == '__main__': # html input param html = sys.argv[1] # target output param target = sys.argv[2] # path from html param path = re.sub(r'[^\/]*$', '', html) # open html file print('๐Ÿ“‚ Open HTML file...', end=' ') soup = BeautifulSoup(open(html), 'html.parser') print('OK!') # find last script as anchorpoint lastScript = soup.findAll('script', attrs={'src': True})[-1] # get all scripts containing src attribute (= external scripts) scripts = soup.findAll('script', attrs={'src': True}) # find last style link as anchorpoint lastStylesheet = soup.findAll('link', attrs={'rel': 'stylesheet'})[-1] # get all links to css stylesheets stylesheets = soup.findAll('link', attrs={'rel': 'stylesheet'}) # create list of script srcs print('๐Ÿ”Ž Create list of scripts...', end=' ') scriptsSrc = deque() for script in scripts: scriptsSrc.append(path + script.attrs['src']) print('Complete!') # create list of stylesheets srcs print('๐Ÿ”Ž Create list of stylesheets...', end=' ') stylesheetsSrc = deque() for stylesheet in stylesheets: stylesheetsSrc.append(path + stylesheet.attrs['href']) print('Complete!') # merge scripts to .temp.js print('๐Ÿ“ฅ Merge scripts...', end=' ') with open('.temp.js', 'w') as outfileScript: for fname in scriptsSrc: # add space every script if fname not in IGNORED: outfileScript.write('\n') with open(fname) as infile: for line in infile: outfileScript.write(line) print('Complete!') print('๐Ÿ“ฅ Merge stylsheets...', end=' ') # merge stylsheets to temp.css with open('.temp.css', 'w') as outfileCSS: for fname in stylesheetsSrc: # add space every script outfileCSS.write('\n') with open(fname) as infile: for line in infile: outfileCSS.write(line) print('Complete!') # minify javascript print('๐Ÿ—ƒ๏ธ Minify scripts...', end=' ') with open('.temp.js') as js: minified_js = jsmin(js.read()) print('Complete!') # minify css print('๐Ÿ—ƒ๏ธ Minify stylsheets...', end=' ') with open('.temp.css') as css: minified_css = compress(css.read()) print('Complete!') # replace scripts with merged and min embed script / css print('๐Ÿ”„ Embedding script and stylsheets...', end=' ') tag = soup.new_tag('script') tag['type'] = 'text/javascript' tag.append(minified_js) lastScript.replace_with(tag) webui_tag = soup.new_tag('script') webui_tag['type'] = 'text/javascript' webui_tag['src'] = '/webui.js' tag.insert_before(webui_tag) tag = soup.new_tag('style') tag['type'] = 'text/css' tag.append(minified_css) lastStylesheet.replace_with(tag) print('Complete!') # remove script and style tags print('๐Ÿงน Cleaning...', end=' ') for script in scripts: script.decompose() for stylesheet in stylesheets: stylesheet.decompose() # remove temp files os.remove('.temp.js') os.remove('.temp.css') print('Complete!') # save html as target print('๐Ÿ’พ Save builded document...', end=' ') file = open(target, 'w') file.write(soup.prettify()) file.close() print('Complete!', end='\n\n') print('๐Ÿ Complete')