def init_build(): global config, path if os.path.exists(path[1] + 'build'): shutil.rmtree(path[1] + 'build') shutil.copytree(path[1] + 'assets', path[1] + 'build') with open(config.get('core', 'build'), 'w') as build: build.write(get_html()) os.remove(path[1] + 'build/index.jinja') for top, dirs, files in os.walk('build/'): for nm in files: if os.path.join(top, nm).split('.')[-1] == "js": with open(os.path.join(top, nm)) as f: file_str = f.read() file_str = jsmin(file_str) with open(os.path.join(top, nm), "w") as f: f.write(file_str) print(prf('OK'), "Compressing file", nm, "..." ) if os.path.join(top, nm).split('.')[-1] == "css": with open(os.path.join(top, nm)) as f: file_str = f.read() file_str = cssmin(file_str) with open(os.path.join(top, nm), "w") as f: f.write(file_str) print(prf('OK'), "Compressing file", nm, "..." ) print(prf('OK'), "Saved in", config.get('core', 'build'), "->", config.get('head', 'title'))
def build(*components, color=None): """Build the demo page""" components = list(components) base = pathlib.Path(__file__).parent # Get the demo CSS with (base / "demo" / "style.css").open() as f: css_demo = rcssmin.cssmin(f.read()) # Get the source CSS css = compiler.build(*components, color=color, minify=True) # Always build the base component components.insert(0, "base") # Load the source of all the components' demos demos = [] for component in components: path = base / "components" / component / "demo.html" with path.open() as f: demos.append({"name": component, "src": f.read()}) return render_template(base / "demo" / "template.html", { "css": css, "css_demo": css_demo, "demos": demos, })
def minify_css_directory2(source, target): """ Move CSS resources from source directory to target directory and minify. Using rcssmin. """ import rcssmin if os.path.isdir(source): if not os.path.exists(target): os.makedirs(target) for root, dirs, files in os.walk(source): for current_file in files: if current_file.endswith(".css"): current_file_path = os.path.join(root, current_file) print " ", current_file_path with open(current_file_path) as css_file: with open( os.path.join( target, current_file.replace('.css', '.min.css')), "w") as minified_file: minified_file.write( rcssmin.cssmin(css_file.read(), keep_bang_comments=True))
def minified_css(style, keep_bang_comments=True): """Return minified CSS `style`. If neither `cssmin`_ nor `rcssmin`_ is installed, `style` is returned unchanged. .. _`cssmin`: https://github.com/zacharyvoase/cssmin .. _`rcssmin`: http://opensource.perlig.de/rcssmin/ """ cssmin = None try: from rcssmin import cssmin except ImportError: try: ### https://packages.qa.debian.org/c/cssmin.html from cssmin import cssmin as _cssmin cssmin = lambda style, keep_bang_comments: _cssmin(style) except ImportError: logging.warning("Couldn't import either rcssmin nor cssmin") if cssmin is not None: try: return cssmin(style, keep_bang_comments=keep_bang_comments) except Exception as exc: logging.error("Exception during minified_css\n %s" % (exc, )) return style
def minify_css(css_map): print("Minify CSS files:") for source, dest in css_map.items(): with open(source, "r") as infile: with open(dest, "w") as outfile: outfile.write(rcssmin.cssmin(infile.read())) print(f"{source} minified to {dest}")
def minify_css_directory(gen, source, target): """ Move CSS resources from source directory to target directory and minify. Using rcssmin. """ import rcssmin plugin_paths = gen.settings['PLUGIN_PATHS'] for path in plugin_paths: source_ = os.path.join(path, 'pelican-bglossary', source) target_ = os.path.join(path, 'pelican-bglossary', target) if os.path.isdir(source_): if not os.path.exists(target_): os.makedirs(target_) for root, dirs, files in os.walk(source_): for current_file in files: if current_file.endswith(".css"): current_file_path = os.path.join(root, current_file) with open(current_file_path) as css_file: with open( os.path.join( target_, current_file.replace( '.css', '.min.css')), "w") as minified_file: minified_file.write( rcssmin.cssmin(css_file.read(), keep_bang_comments=True))
def minify_css_directory2(source, target): """ Move CSS resources from source directory to target directory and minify. Using rcssmin. """ import rcssmin if os.path.isdir(source): if not os.path.exists(target): os.makedirs(target) for root, dirs, files in os.walk(source): for current_file in files: if current_file.endswith(".css"): current_file_path = os.path.join(root, current_file) print(" " + current_file_path) with open(current_file_path) as css_file: with open(os.path.join(target, current_file.replace('.css', '.min.css')), "w") as minified_file: minified_file.write(rcssmin.cssmin(css_file.read(), keep_bang_comments=True)) bundle_data = [] for root, dirs, files in os.walk(target): for current_file in files: if current_file.endswith(".css") and current_file != 'datatable.bundle.min.css': current_file_path = os.path.join(root, current_file) css_file = open(current_file_path, "r") bundle_data += css_file.readlines() css_file.close() bundle_filename = os.path.join(target, 'datatable.bundle.min.css') bundle_file = open(bundle_filename, 'w+') bundle_file.write(''.join(bundle_data)) bundle_file.close() print(" " + bundle_filename)
def build(self): logging.info("Started build") artifact_root = self.root / self.config["artifacts_dir"] shutil.rmtree(artifact_root, ignore_errors=True) artifact_root.mkdir() blob_root = artifact_root / "blob" blob_root.mkdir() shutil.copytree(self.root / "static", artifact_root / "static") self.tree.build_hierarchy(artifact_root, self.root / "templates") self.tree.build_blob_hierarchy(blob_root) jobs = self.tree.get_jobs(blob_root) self.convert_all(jobs, self.config["pandoc_args"]) self.tree.generate_peek(blob_root, self.config["peek_length"]) with (blob_root / "index.json").open("w", encoding=ENC) as f: json.dump(self.tree.index(), f, sort_keys=True) f.write("\n") for f in (artifact_root / "static").iterdir(): if f.suffix == ".js": with f.open(encoding=ENC) as fin: cont = fin.read() with f.open("w", encoding=ENC) as fout: fout.write(jsmin(cont)) fout.write("\n") elif f.suffix == ".css": with f.open(encoding=ENC) as fin: cont = fin.read() with f.open("w", encoding=ENC) as fout: fout.write(cssmin(cont)) fout.write("\n") with (artifact_root / "rss.xml").open("w", encoding=ENC) as f: f.write(self.rss())
def compress(css, **kwargs): capture_svg = re.compile(r'url\("(data:image/svg.*?svg%3[Ee])\"\)') data_urls = re.findall(capture_svg, css) for data_url in data_urls: css = css.replace(data_url, data_url.replace(' ', '%20')) css = cssmin(css, **kwargs) return css
def minifydir(dir_var): for file in glob.glob(dir_var+"*.min.js"): os.remove(file) for file in glob.glob(dir_var+"*.min.css"): os.remove(file) for file in glob.glob(dir_var+"*.js"): fileopen = open(file,"r+", encoding="utf8") text = fileopen.read() fileopen.close() fileopen = open(file[:-3]+".min.js","w", encoding="utf8") fileopen.write(jsmin(text)) fileopen.close() some_command = "js2coffee "+file output = subprocess.run(some_command,capture_output=True, shell=True).stdout fileopen = open(file[:-3]+".coffee","w", encoding="utf8") fileopen.write(output.decode("utf-8")) fileopen.close() print("☕",file,"compiled.") for file in glob.glob(dir_var+"*.scss"): fileopen = open(file,"r+", encoding="utf8") text = fileopen.read() fileopen.close() fileopen = open(file[:-5]+".min.css","w", encoding="utf8") fileopen.write(cssmin(sass.compile(string=text))) fileopen.close() print("📝",file,"compiled.")
async def css(request): """CSS route.""" return Response(cssmin( TEMPLATES.get_template('medieval.css').render( primary_color=site(request, 'primary_color'), foreground=get_foreground(site(request, 'primary_color')), domain=site(request, 'domain'))), media_type='text/css')
def main(): input_str = sys.stdin.read() output_str = rcssmin.cssmin(input_str) output_str = output_str.replace("\\", "\\\\") output_str = output_str.replace("\"", "\\\"") sys.stdout.write('"' + output_str + '"')
def inject_dict_for_all_templates(): css = "" try: with open('src/styles/style.scss', 'r') as scss: css = sass.compile(string=scss.read()) return dict(cssmin=rcssmin.cssmin(css)) except BaseException: return dict(cssmin="")
def buildCSSFile(filename, output_filename): if verbose: print("compiling " + filename + " to " + output_filename) with open(filename, "r") as myfile: css = myfile.read() minified_css = cssmin(css) with open(output_filename, "w") as text_file: text_file.write(minified_css) return 0
def minify_css(filename): with open(filename, 'r') as f: # Read file to minify uncompressed = f.read() with open(filename, 'w') as f: # Minify and override compressed = rcssmin.cssmin(uncompressed) f.write(compressed)
def get_bundled(elems, attr, kind, merge): # concat all input in the block content = '' # construct a bundle by converting tags to import statements or by merging the raw content for el in elems: # is inclusion or inline block? if el.get(attr): # check if we're loading an alternative source in production file = el[attr] if not isDevelopment and el.get('prod'): file = el['prod'] # absolute path of the given asset if not el.get('base-dir'): asset = '%s/assets/%s' % (settings.BASE_DIR, file) else: asset = os.path.normpath( os.path.join(settings.BASE_DIR, '../', el['base-dir'].strip('/'), file.strip('/'))) # merged content is read from file and concatenated if merge: f = open(asset.replace('/', os.sep), 'r', encoding='utf8') f.seek(0) c = f.read() # separate each content block with a line sep content += c + '\n' else: # bundles are made up of appropriate import statements (to be imported and compiled by webpack) if 'js' in kind: content += 'import \'%s\';\n' % asset else: content += '@import \'%s\';\n' % asset else: # concat content held within tags after cleaning up all whitespace on each newline (consistent content regardless of indentation) content += '\n'.join( str(x).strip() for x in (''.join([str(x) for x in el.contents]).splitlines())) # compile any sass to css if 'css' in kind: import sass content = sass.compile(string='%s \n %s' % (get_sass_extras(), content)) # minify the content in production if not isDevelopment: if 'js' in kind: import rjsmin content = rjsmin.jsmin(content) elif 'css' in kind: import rcssmin content = rcssmin.cssmin(content) # content is compiled and minified (if in production) return content
def output_css(self): self.output.write('<style>') for file_path in self.files['css']: if self.options['v']: print('Inserting css from', file_path) if self.options['m']: self.output.write(cssmin(self.resolve_path(file_path)[0])) else: self.output.write(self.resolve_path(file_path)[0]) self.output.write('</style>') self.files['css'] = []
def get_content(self, provider_run): if self.template is not None: content = self.template.render(request=provider_run.request, context=provider_run.context) if self.options['minify']: if cssmin is not None: content = cssmin(content) else: raise ImproperlyConfigured( "Unable to minify {}.cssm because rcssmin is not available. Please `pip install rcssmin`." .format(self.template_name)) return '<style type="text/css">{}</style>'.format(content) return None
def process_css(doc, assets): regex = re.compile('url\([\'"]?(?P<url>.*?)[\'"]?\)') css = '' for item in doc('link[rel="stylesheet"]'): filename = item.get('href') style = open(filename).read() css += '/**** %s ****/\n' % filename style = regex.sub( lambda g: "url('%s')" % encode_image( os.path.join(os.path.dirname(filename), g.group('url'))), style) code = cssmin(style) css += code + '\n' return css
def get_css_chunks(config): css_path = os.path.join(config.theme_path, "css") css_chunks = {} if not os.path.exists(css_path): return css_chunks for filename in os.listdir(css_path): if not filename.endswith(".css"): continue filepath = os.path.join(css_path, filename) with open(filepath) as css_file: css_chunks[filename] = cssmin(css_file.read()) return css_chunks
def parse_and_validate(self, stylesheet_source): if len(stylesheet_source) > (MAX_SIZE_KIB * 1024): return ("", True) nodes = tinycss2.parse_stylesheet(stylesheet_source) source_lines = stylesheet_source.splitlines() backslash_errors = self.check_for_evil_codepoints(source_lines) validation_errors = self.validate_rule_list(nodes) print(backslash_errors) print(validation_errors) if backslash_errors or validation_errors: return ("", True) else: serialized = cssmin(tinycss2.serialize(nodes)) return (serialized.encode("utf-8"), False)
def get_template_css(self, request, context): '''Retrives the static and mako-rendered CSS''' ret = [] for template in reversed( self.template_chain ): # reverse so lower CSS overrides higher CSS in the inheritance chain ti = getattr(template, DMP_ATTR_NAME) if ti.css: ret.append( ti.css ) # the <link> was already created once in the constructor if ti.cssm: css_text = STYLE_RENDERERS[ti.app].render( request, ti.cssm, context.kwargs) if JSMIN and get_setting('MINIFY_JS_CSS', False): css_text = cssmin(css_text) ret.append('<style type="text/css">%s</style>' % css_text) return '\n'.join(ret)
def getcss(self): """ Returns the project's custom stylesheet, if it exists. """ css = "" try: with open(os.path.join(self.path, "project.css"), "r") as file: data = file.read().decode('utf-8') if data is not None: import rcssmin data = rcssmin.cssmin(data) if len(data) > 0: css = data except IOError: # Like with the getjs method, we can safely ignore any I/O error. pass return css
def build_assets(): name_map = [] for asset in os.listdir(ASSETS_SRC): name, ext = os.path.splitext(asset) if not ext: continue with open(os.path.join(ASSETS_SRC, asset), 'rb') as f: content = f.read() hash = sha256(content).hexdigest()[:20] dist_name = '%s-%s%s' % (name, hash, ext) if ext == '.css': content = cssmin(content.decode('utf-8')).encode('utf-8') with open(os.path.join(ASSETS_DIST, dist_name), 'wb') as f: f.write(content) name_map.append((asset, dist_name)) return name_map
def main(): """Start the CSS modification.""" css_in = css_path_in.read_text(encoding='utf-8') font_binary = font_path.read_bytes() font_b64 = base64.b64encode(font_binary).decode('utf-8') css_out = [] for css in css_in.split('\n'): if 'src: url(' in css: css = (f' src: url(data:font/woff2;charset=utf-8;' f'base64,{font_b64}) format("woff2");') elif 'url(' in css: continue css_out.append(css) css_out = '\n'.join(css_out) css_minified_out = rcssmin.cssmin(style=css_out) css_path_out.write_text(data=css_out, encoding='utf-8') css_minified_path_out.write_text(data=css_minified_out, encoding='utf-8')
def minifydir(dir): for file in glob.glob(dir+"*.min.js"): os.remove(file) for file in glob.glob(dir+"*.min.css"): os.remove(file) for file in glob.glob(dir+"*.js"): fileopen = open(file,"r+", encoding="utf8") text = fileopen.read() fileopen.close() fileopen = open(file[:-3]+".min.js","w", encoding="utf8") fileopen.write(jsmin(text)) fileopen.close() for file in glob.glob(dir+"*.css"): fileopen = open(file,"r+", encoding="utf8") text = fileopen.read() fileopen.close() fileopen = open(file[:-4]+".min.css","w", encoding="utf8") fileopen.write(cssmin(text)) fileopen.close()
def build(self): if os.path.exists('build'): shutil.rmtree('build') if not os.path.exists('assets') \ and not os.path.exists('sections') \ and not os.path.exists(config.file): print(prf('FAIL'), 'Structure does not exist', '!') sys.exit(1) shutil.copytree('assets', 'build') with open(config.settings.get('core', 'build'), 'w') as build: build.write(collector.html()) os.remove('build/index.jinja') for (top, dirs, files) in os.walk('build/'): for nm in files: if os.path.join(top, nm).split('.')[-1] == 'js': with open(os.path.join(top, nm)) as f: file_str = f.read() file_str = jsmin(file_str) with open(os.path.join(top, nm), 'w') as f: f.write(file_str) print(prf('OK'), 'Compressing file', nm, '...') if os.path.join(top, nm).split('.')[-1] == 'css': with open(os.path.join(top, nm)) as f: file_str = f.read() file_str = cssmin(file_str) with open(os.path.join(top, nm), 'w') as f: f.write(file_str) print(prf('OK'), 'Compressing file', nm, '...') print(prf('OK'), 'Saved in', config.settings.get('core', 'build'), '->', config.settings.get('head', 'title'))
def build(*components, color=None, minify=False): """Build multiple components""" base = pathlib.Path(__file__).parent to_build = [ base / "globals.scss", base / "components" / "base" / "style.scss", ] # Check if components are valid for component in components: path = base / "components" / component / "style.scss" if not path.exists(): raise NameError("Component %s doesn't exist" % component) to_build.append(path) # Build the CSS files result = compile(*to_build, color=color) if minify: result = rcssmin.cssmin(result) return result
def upload(username, file): start = time() page = site.Pages["User: %s/%s" % (username, file)] fp = open(file, "r") text = fp.read() fp.close() if file.find(".js") != -1: text = rjsmin.jsmin(text) text = "// <nowiki>\n" + text + "\n// </nowiki>" elif file.find(".css") != -1: text = rcssmin.cssmin(text) if sha256(page.text().encode("utf-8")).hexdigest() == sha256(text).hexdigest(): print "%s unchanged" % file return 0 comment = "更新Twinkle至Gitlab最新版本。" page.save(text.decode("utf-8"), summary=comment, bot=False) print "Uploaded %s" % file end = time() return 15 + start - end
def minimise(dummy, dirname, filesindir): """ Minimises any .js or .css files found Args: dummy: unused dirname: the directory in which to minimise files filesindir: lists the files in the directory """ for fname in filesindir: if fname[-4:] == '.css' and fname[-8:] != '.min.css': oldfile = os.path.join(dirname, fname) newfile = os.path.join(dirname, fname[:-4] + '.min.css') print("CSS: Minimising: " + oldfile + " -> " + newfile) fp_old = open(oldfile, "rb") fp_new = open(newfile, "wb") fp_new.write(cssmin(fp_old.read(), keep_bang_comments=False)) fp_old.close() fp_new.close() elif fname[-3:] == '.js' and fname[-7:] != '.min.js': oldfile = os.path.join(dirname, fname) newfile = os.path.join(dirname, fname[:-3] + '.min.js') print("JS : Minimising: " + oldfile + " -> " + newfile) fp_old = open(oldfile, "rb") fp_new = open(newfile, "wb") fp_new.write(jsmin(fp_old.read(), keep_bang_comments=False)) fp_old.close() fp_new.close()