#!/usr/bin/python3 # -*- coding: utf-8 -*- # ############################################################################# # Copyright (C) 2018 manatlan manatlan[at]gmail(dot)com # # MIT licence # # https://github.com/manatlan/vbuild # ############################################################################# __version__ = "0.8.1" # py2.7 & py3.5 !!!! import re, os, json, glob, itertools, traceback, subprocess, pkgutil try: from HTMLParser import HTMLParser import urllib2 as urlrequest import urllib as urlparse except ImportError: from html.parser import HTMLParser import urllib.request as urlrequest import urllib.parse as urlparse transHtml = lambda x: x # override them to use your own transformer/minifier transStyle = lambda x: x transScript = lambda x: x partial = "" fullPyComp = True # 3 states ;-) # None : minimal py comp, it's up to u to include "pscript.get_full_std_lib()" # False : minimal py comp, vbuild will include the std lib # True : each component generate its needs (default) hasLess = bool(pkgutil.find_loader("lesscpy")) hasSass = bool(pkgutil.find_loader("scss")) hasClosure = bool(pkgutil.find_loader("closure")) class VBuildException(Exception): pass def minimize(code): if hasClosure: return jsmin(code) else: return jsminOnline(code) def jsminOnline(code): """ JS-minimize (transpile to ES5 JS compliant) thru a online service (https://closure-compiler.appspot.com/compile) """ data = [ ("js_code", code), ("compilation_level", "SIMPLE_OPTIMIZATIONS"), ("output_format", "json"), ("output_info", "compiled_code"), ("output_info", "errors"), ] try: req = urlrequest.Request( "https://closure-compiler.appspot.com/compile", urlparse.urlencode(data).encode("utf8"), {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"}, ) response = urlrequest.urlopen(req) r = json.loads(response.read()) response.close() code = r.get("compiledCode", None) except Exception as e: raise VBuildException("minimize error: %s" % e) if code: return code else: raise VBuildException("minimize error: %s" % r.get("errors", None)) def jsmin(code): # need java & pip/closure """ JS-minimize (transpile to ES5 JS compliant) with closure-compiler (pip package 'closure', need java !) """ if hasClosure: import closure # py2 or py3 else: raise VBuildException( "jsmin error: closure is not installed (sudo pip closure)" ) cmd = ["java", "-jar", closure.get_jar_filename()] try: p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE ) except Exception as e: raise VBuildException("jsmin error: %s" % e) out, err = p.communicate(code.encode("utf8")) if p.returncode == 0: return out.decode("utf8") else: raise VBuildException("jsmin error:" + err.decode("utf8")) def preProcessCSS(cnt, partial=""): """ Apply css-preprocessing on css rules (according css.type) using a partial or not return the css pre-processed """ if cnt.type in ["scss", "sass"]: if hasSass: from scss.compiler import compile_string # lang="scss" return compile_string(partial + "\n" + cnt.value) else: print("***WARNING*** : miss 'sass' preprocessor : sudo pip install pyscss") return cnt.value elif cnt.type in ["less"]: if hasLess: import lesscpy, six return lesscpy.compile( six.StringIO(partial + "\n" + cnt.value), minify=True ) else: print("***WARNING*** : miss 'less' preprocessor : sudo pip install lesscpy") return cnt.value else: return cnt.value class Content: def __init__(self, v, typ=None): self.type = typ self.value = v.strip("\n\r\t ") def __repr__(self): return self.value class VueParser(HTMLParser): """ Just a class to extract