Example #1
0
    def process_response(self, request, response):
        if response.streaming:
            return response

        content = response.content.decode(self.encoding)
        match = CRITICAL_CSS_RE.search(content)

        if not match:
            return response

        critical_css_fragment = match.group(1)
        key_match = CRITICAL_KEY_RE.search(content)
        if key_match:
            content = CRITICAL_KEY_RE.sub('', content)
            key = key_match.group(1)
        else:
            key = 'default'

        h = hashlib.sha1()
        if six.PY2:
            h.update(critical_css_fragment)
        else:
            h.update(critical_css_fragment.encode("utf-8"))
        cache_key = 'django_critical:{hash}:{key}'.format(hash=h.hexdigest(),
                                                          key=key)

        cached = cache.get(cache_key)

        if cached is not None:
            new_fragment, css_entries = cached
        else:
            css_entries = extract_css_entries(request, critical_css_fragment)
            css = download_css(css_entries, self.encoding)
            critical_css = get_critical_css(content, css)
            if six.PY2:
                critical_css = cssmin(critical_css)
            else:
                critical_css = cssmin(critical_css.decode('utf-8'))
            new_fragment = '<style>{css}</style>'.format(
                css=critical_css.replace('\\', '\\\\'))
            cache.set(cache_key, (new_fragment, css_entries))

        content = CRITICAL_CSS_RE.sub(new_fragment, content)

        async_snippet_template = get_template('critical/async_snippet.html')
        async_snippet = async_snippet_template.render({
            'critical_css_fragment':
            critical_css_fragment,
            'css_entries':
            css_entries,
        })

        content = ASYNC_SNIPPET_RE.sub(async_snippet, content)

        response.content = content.encode(self.encoding)
        return response
Example #2
0
def minify(text, css=True, html=True, remove_comments=True):
    """Minify html and css part of text"""
    if html:
        text = htmlmin.minify(text, remove_comments=remove_comments)
        if css:
            text = cssmin.cssmin(text)
        return text
    if cssmin:
        text = cssmin.cssmin(text)
        return text
Example #3
0
def minify(text, css = True, html = True, remove_comments = True):
    """Minify html and css part of text"""
    if html:
        text = htmlmin.minify( text, remove_comments = remove_comments)
        if css:
            text = cssmin.cssmin( text )
        return text
    if cssmin:
        text = cssmin.cssmin( text )
        return text
Example #4
0
    def _work_css_files(self, folder):
        for root, dirs, files in os.walk(folder):
            for f in files:
                if f.endswith('.scss'):
                    scss_filename = os.path.join(root, f)
                    css_filename = os.path.join(root, f[:-4] + 'css')

                    try:
                        css_string = sass.compile(filename=scss_filename)
                    except sass.CompileError as e:
                        raise SassError('Could not compile your scss file:\n', e.message[:-1])
                    except:
                        raise FileNotFoundError('Could not find your scss style file.')

                    try:
                        css_file = open(css_filename, 'w+')
                    except:
                        raise FileNotWritableError('Could not write the new css file:\n' + css_filename)

                    if self._config['minify_css']:
                        css_string = cssmin(css_string)

                    css_file.write(css_string)
                    css_file.close();

                    try:
                        os.remove(scss_filename)
                    except:
                        raise FileNotWritableError('Could not remove already converted scss file:\n' + scss_filename)
                elif f.endswith('.css'):
                    if self._config['minify_css']:
                        css_filename = os.path.join(root, f)

                        try:
                            css_file = open(f, 'r')
                        except:
                            raise FileNotReadableError('Could not read your css file:\n' + css_filename)

                        css_string = f.read()
                        css_file.close()

                        try:
                            os.remove(css_file)
                        except:
                            raise FileNotWritableError('Could not remove your non-minified css file:\n' + css_filename)

                        try:
                            css_file = open(css_filename, 'w+')
                        except:
                            FileNotWritableError('Could not write to the new minified css file:\n' + css_filename)

                        css_string = cssmin(css_string)
                        css_file.write(css_string)
                        css_file.close()
Example #5
0
	def cssmin(css_code=None, css_url=None):
		""" Pretty much just a pass-through to cssmin.cssmin """
		if css_url is not None:
			css_code = HtmlMinifier.read_asset(css_url)
		elif css_code is None:
			raise ValueError('Must specify a value for either css_code or css_url')
		return cssmin.cssmin(css_code)
Example #6
0
def _rewrite_relative_url(content, asset_path, static_path):
    """Rewrite relative url in `url('')` of css file to absolute url."""
    from os.path import dirname

    content = cssmin(content)
    pattern = re.compile(r"url\([\'\"]?([^\'\"/][^\'\"\)]+)[\'\"]?\)")

    for match in pattern.finditer(content):
        full = match.group(0)
        inner_url = match.group(1)

        if inner_url.startswith("data:"):
            continue

        if inner_url.startswith("../"):
            dir_path = dirname(dirname(asset_path))
            absolute_path = "%s/%s" % (dir_path, inner_url[3:])
        else:
            dir_path = dirname(asset_path)
            absolute_path = "%s/%s" % (dir_path, inner_url)

        absolute_url = "/static%s" % absolute_path.split(static_path)[1]
        result = "url('%s')" % absolute_url
        content = content.replace(full, result)
    return content
Example #7
0
    def run(self):
        self.mkpath(self.build_dir)

        outfiles = []
        updated_files = []

        # consider replacing this with a better utility
        jsm = JavascriptMinify()

        for f in self.web_files:
            inf = convert_path(f)
            outf = os.path.join(self.build_dir, f)

            self.mkpath(os.path.dirname(outf))

            if self.compress:
                log.info("minifying %s -> %s" % (f, outf))

                input = open(inf, 'r')
                output = open(outf, 'wb')

                if f.endswith('.js'):
                    # eat the first line (JSLint directive)
                    input.readline()

                # copy 5 lines of the header
                for l in range(6):
                    output.write(input.readline())

                if f.endswith('.js'):
                    jsm.minify(input, output)
                elif f.endswith('.css'):
                    output.write(cssmin(input.read()))

                input.close()
                output.close()
            else:
                self.copy_file(inf, outf)

            self.outfiles.append(outf)

        # build a regular expression to substitute the HTML files
        regex = ';'.join([
            's/{{%s}}/%s/' % (re.escape(k), re.escape(v[self.compress]))
            for k, v in self.html_subst.iteritems()
        ])
        regex += ';s/{{VERSION}}/%s/' % (re.escape(self.version))

        for f in self.html_files:
            inf = convert_path(f)
            outf = os.path.join(self.build_dir, os.path.basename(f))

            log.info("substituting %s -> %s" % (f, outf))
            output = open(outf, 'wb')

            # use sed, because I like sed
            subprocess.check_call(['sed', regex, inf], stdout=output)
            output.close()

            self.outfiles.append(outf)
Example #8
0
    def _compress(self):
        output = []

        src_paths = []

        for src in self.includes:

            if src.endswith('less'):
                src_paths.append('%s' % src)
                src = src.replace(
                    'less', 'css')  # less/example.less -> css/example.css
                src = '%s.less.css' % src[:
                                          -4]  # css/example.css -> css/example.less.css
            else:
                src_paths.append('www/%s' % src)

            with open('www/%s' % src) as f:
                print '- compressing %s' % src
                output.append(cssmin(f.read()))

        context = make_context()
        context['paths'] = src_paths

        header = render_template('_css_header.css', **context)
        output.insert(0, header)

        return '\n'.join(output)
Example #9
0
def docss(conf, inputs, outputs):
    import cssmin
    bcss = [
        'body.css', 'layout.css', 'forms.css', 'grid.css', 'notification.css',
        'paginate.css', 'plot.css', 'settings.css', 'toolbars.css',
        'tooltip.css', 'tree.css', 'ui.css', 'upload.css', 'window.css',
        'misc.css'
    ]
    ccss = [
        'body.css', 'layout.css', 'forms.css', 'grid.css', 'icons.css',
        'toolbars.css', 'progress.css', 'tree.css', 'ui.css', 'window.css',
        'misc.css'
    ]
    outputs["Result"]["value"] = ""
    for a in bcss:
        f = open(conf["main"]["mmPath"] + '/new-themes/themes/default/' + a,
                 'r')
        outputs["Result"]["value"] += f.read()
        f.close()
    for a in ccss:
        f = open(
            conf["main"]["mmPath"] + '/new-themes/themes/' +
            inputs['color']['value'] + '/' + a, 'r')
        outputs["Result"]["value"] += f.read()
        f.close()
    if conf["main"].has_key('cssCache') and conf["main"]["cssCache"] == "prod":
        import cssmin
        outputs["Result"]["value"] = cssmin.cssmin(outputs["Result"]["value"])
    return zoo.SERVICE_SUCCEEDED
Example #10
0
File: pie.py Project: priyatam/pie
def bake(config, minify):
    """Bakes contents, templates, scripts, and styles together"""
    params = {"title": config["title"]}
    logger.info("Baking...")
    contents_data = contents.load(config)
    dynamic_templates = templates.load(config)
    lambdas_data = lambdas.load(config, contents_data, dynamic_templates)

    style, script = mix(config)

    logger.info("Baking contents and templates")
    contents.bake(config, contents_data, lambdas_data)
    templates.bake(config, dynamic_templates, lambdas_data)

    params.update({"json_data": json.dumps(contents_data + dynamic_templates)})

    logger.info("Baking styles and scripts")
    if minify:
        params.update({"style_sheet": cssmin.cssmin(style), "script": jsmin.jsmin(script)})
    else:
        params.update({"style_sheet": style, "script": script})

    logger.info("Baking lambdas")
    params.update(lambdas_data)
    params.update({"config": config})

    logger.info("Compiling assets into an index page")
    pie = merge_pages(config, templates.get_index(config), params)
    build_index_html(pie, config)

    return pie
Example #11
0
    def getApplicationCssCode(self):
        if not session.get('user', None) is None:
            user = session['user']
            userobj = db.session.query(User).filter_by(name=user).one()
            session['color'] = userobj.color

        color = session['color']
        css = str()

        for filename in self.cssFiles:
            css += open(filename).read().strip()

        if len(color) == 4:
            cval = int(color[1], 16)\
                 + int(color[2], 16)\
                 + int(color[3], 16)

        else:
            cval = int(color[1:3], 16)\
                 + int(color[3:5], 16)\
                 + int(color[5:7], 16)

        if cval < (3 * 256) / 2:
            contrast = '#FFF'

        else:
            contrast = '#000'

        css = css.replace('%%COLOR%%', color)
        css = css.replace('%%CONTRAST%%', contrast)
        return cssmin(css)
Example #12
0
    def _compress(self):
        output = []

        src_paths = []

        for src in self.includes:

            if src.endswith('less'):
                src_paths.append('%s' % src)
                src = src.replace(
                    'less', 'css')  # less/example.less -> css/example.css
                src = '%s.less.css' % src[:
                                          -4]  # css/example.css -> css/example.less.css
            else:
                src_paths.append('www/%s' % src)

            with codecs.open('%s/www/%s' % (self.static_path, src),
                             encoding='utf-8') as f:
                print '- compressing %s' % src
                output.append(cssmin(f.read().encode('utf-8')))

        context = make_context()
        context['paths'] = src_paths

        # header = render_template('posts/%s/templates/_css_header.css' % self.slug, **context)
        # output.insert(0, header)

        return '\n'.join(output)
Example #13
0
def minify_website(args):
    css_in = ' '.join(get_css_in(args))
    css_out = f'{args.output_dir}/css/base.css'
    if args.minify:
        command = f"purifycss -w '*algolia*' --min {css_in} '{args.output_dir}/*.html' " \
            f"'{args.output_dir}/docs/en/**/*.html' '{args.website_dir}/js/**/*.js' > {css_out}"
    else:
        command = f'cat {css_in} > {css_out}'
    logging.info(command)
    output = subprocess.check_output(command, shell=True)
    logging.debug(output)
    with open(css_out, 'rb') as f:
        css_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]

    js_in = get_js_in(args)
    js_out = f'{args.output_dir}/js/base.js'
    if args.minify:
        js_in = [js[1:-1] for js in js_in]
        closure_args = [
            '--js', *js_in, '--js_output_file', js_out, '--compilation_level',
            'SIMPLE', '--dependency_mode', 'NONE', '--third_party',
            '--use_types_for_optimization', '--isolation_mode', 'IIFE'
        ]
        logging.info(closure_args)
        if closure.run(*closure_args):
            raise RuntimeError('failed to run closure compiler')

    else:
        logging.info(command)
        js_in = ' '.join(js_in)
        output = subprocess.check_output(f'cat {js_in} > {js_out}', shell=True)
        logging.debug(output)
    with open(js_out, 'rb') as f:
        js_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]
        logging.info(js_digest)

    if args.minify:
        logging.info('Minifying website')
        for root, _, filenames in os.walk(args.output_dir):
            for filename in filenames:
                path = os.path.join(root, filename)
                if not (filename.endswith('.html')
                        or filename.endswith('.css')):
                    continue

                logging.info('Minifying %s', path)
                with open(path, 'rb') as f:
                    content = f.read().decode('utf-8')
                if filename.endswith('.html'):
                    content = htmlmin.minify(content, remove_empty_space=False)
                    content = content.replace('base.css?css_digest',
                                              f'base.css?{css_digest}')
                    content = content.replace('base.js?js_digest',
                                              f'base.js?{js_digest}')
                elif filename.endswith('.css'):
                    content = cssmin.cssmin(content)
                elif filename.endswith('.js'):
                    content = jsmin.jsmin(content)
                with open(path, 'wb') as f:
                    f.write(content.encode('utf-8'))
Example #14
0
    def _compress(self):
        output = []

        src_paths = []

        for src in self.includes:

            if src.endswith('less'):
                src_paths.append('%s' % src)
                src = src.replace('less', 'css') # less/example.less -> css/example.css
                src = '%s.less.css' % src[:-4]   # css/example.css -> css/example.less.css
            else:
                src_paths.append('www/%s' % src)

            with open('www/%s' % src) as f:
                print '- compressing %s' % src
                output.append(cssmin(f.read().encode('utf-8')))

        context = make_context()
        context['paths'] = src_paths

        header = render_template('_css_header.css', **context)
        output.insert(0, header)


        return '\n'.join(output)
Example #15
0
    def process(self):
        try:
            from cssmin import cssmin
        except ImportError:
            raise ProcessorError('Unable to import cssmin module.')

        self.save_contents(cssmin(self.read()))
Example #16
0
def make_css_from_statements(lines, compressed=True, empty_selectors=True, indent='    ', target=None):
    if compressed:
        indent = ''
        try:
            from cssmin import cssmin
        except ImportError:
            def cssmin(css, wrap=None):
                pass
    _media, _indent = [], ''
    for media, selectors, declarations in last(lines, ([], [], [])):
        if media != _media:
            if _media:
                target.send('}')
                yield '}'
                _indent = ''
            if media:
                yield (','.join(media) + '{') if compressed else (', '.join(media) + ' {')
                _indent = indent
        if selectors == ['@import']:
            for _, path in declarations:
                with open(path) as f:
                    content = f.read()
                    if content:
                        yield cssmin(content) if compressed else content
        elif empty_selectors or declarations:
            declarations = [_indent + indent + k + (':' if compressed else ': ') + str(v) + ';' for k, v in declarations]
            if selectors:
                if compressed:
                    line = ','.join(selectors) + '{' + ''.join(declarations) + '}'
                else:
                    line = _indent + (',\n' + _indent).join(selectors)
                    line += ' {\n' + '\n'.join(declarations) + '\n' + _indent + '}'
                if line:
                    yield line
        _media = media
Example #17
0
    def Recreate(self):
        css = ""
        for cssfile in self.Files:
            css += open(cssfile, 'r').read()

        if self.Minimize:
            if cssmin is not None:
                css = cssmin.cssmin(css)
            else:
                L.warning(
                    "Active tag 'cssloader' was asked to minimalize CSS files but 'cssmin' module not found"
                )

        csshash = hashlib.md5(css).hexdigest()

        cachefname = '{0:x}'.format(self.CacheTag) + '-' + csshash + '.css'
        if cachefname[0] == '-':
            cachefname = chr(int(cachefname[1], 16) + 103) + cachefname[2:]

        self.CacheFile = os.path.join(CacheManager.CacheDir, cachefname)
        open(self.CacheFile, 'w').write(css)

        self.URL = self.CacheURL + cachefname

        self.Touch = time.time()
Example #18
0
    def build(cls, basepath, bundle_key, ext):
        bundle = {}

        # Iterate over files in bundle; determine last modified time and
        # assemble content
        last_mtime = 0
        contents = ""
        for path in ASSET_MANIFEST[bundle_key]:
            path = os.path.join(os.path.abspath(basepath),
                path[len('/static/'):])
            last_mtime = max(last_mtime, os.stat(path)[stat.ST_MTIME])
            contents += open(path, "rb").read() + "\n"

        if ext == "js":
            bundle["contents"] = slimit.minify(contents, mangle=True,
                mangle_toplevel=True)
        elif ext == "css":
            bundle["contents"] = cssmin.cssmin(contents)
        else:
            assert False

        bundle["sha1"] = hashlib.sha1(bundle["contents"]).hexdigest()
        bundle["last_modified"] = datetime.datetime.fromtimestamp(last_mtime)
        bundle["mime_type"] = "text/javascript" if ext == "js" else "text/css"

        StaticBuild._bundles[bundle_key] = bundle
Example #19
0
def build_css():
    build_map = {
        'main.min.css': ['pure-min.css', 'base.css', 'iconfont.css'],
        'index.min.css':
        ['main.min.css', 'balloon.min.css', 'index.css', 'widget.css'],
        'topic.min.css': ['main.min.css', 'topic.css'],
        'post.min.css': [
            'main.min.css', 'post.css', 'react.css', 'gitment.css',
            'dracula.css', 'social-sharer.css'
        ]
    }
    css_map = {}
    css_dir = Path(HERE) / 'static/css/'
    for css, files in build_map.items():
        data = ''
        for file in files:
            if file in css_map:
                data += css_map[file]
            else:
                with open(css_dir / file) as f:
                    data_ = f.read()
                    css_map[file] = data_
                data += data_
        with open(css_dir / css, 'w') as f:
            f.write(cssmin.cssmin(data))
            css_map[css] = data
Example #20
0
    def run(self):
        log.debug("[%s.%s] Compressing CSS." %
                  (__name__, self.__class__.__name__))

        CSSFULL_DIR = get_path(
            [BASEDIR, 'tribus', 'data', 'static', 'css', 'full'])
        CSSMIN_DIR = get_path(
            [BASEDIR, 'tribus', 'data', 'static', 'css', 'min'])

        try:
            os.makedirs(CSSMIN_DIR)
        except Exception as e:
            print e

        for CSS_FILE in find_files(path=CSSFULL_DIR, pattern='*.css'):

            CSSMIN_FILE = get_path([CSSMIN_DIR, os.path.basename(CSS_FILE)])

            try:

                with open(CSSMIN_FILE, 'w') as _file:
                    _file.write(cssmin.cssmin(open(CSS_FILE).read()))
                    _file.close()

            except Exception as e:
                print e

            log.debug(
                "[%s.%s] %s > %s." %
                (__name__, self.__class__.__name__, CSS_FILE, CSSMIN_FILE))
Example #21
0
def update_css(css):
    print ("Starting update css...\nConnecting to reddit")
    splitkey = "/**botcss**/"
    r = praw.Reddit(user_agent='css updater [praw]')
    r.login(user, password)
    print("Connected!\nUpdating css...")
    subreddit = r.get_subreddit(subr_name)

    # wtf T_T why htmlentity2ascii ? idk bug without...
    cur_css = htmlentity2ascii.convert(subreddit.get_stylesheet()['stylesheet'].split(splitkey, 1)[0])
    print('minimizing css before upload..')
    newcss = '%s\n%s\n%s\n' % (cssmin.cssmin(cur_css, False), splitkey, cssmin.cssmin(css, False))
    save_css(newcss)
    subreddit.set_stylesheet(newcss)
    print ('Done!')
    return newcss
Example #22
0
def generate_script(pk, logo_path, title, greeting_message, end_message):

    sprite_path = settings.MEDIA_URL + 'sprite.png'

    path_to_js = settings.MEDIA_ROOT + "/script.js"
    path_to_html = settings.MEDIA_ROOT + "/basic.html"
    path_to_css = settings.MEDIA_ROOT + "/style.css"

    file_js = open(path_to_js, "r")
    file_html = open(path_to_html, "r")
    file_css = open(path_to_css, "r")

    text_js = file_js.read()
    text_js = text_js.replace("SITE_DOMAIN", settings.SITE_DOMAIN)
    text_js = text_js.replace("GREETING_MESSAGE", greeting_message)
    text_js = text_js.replace("END_MESSAGE", end_message)  #fix it
    text_html = file_html.read()
    text_html = text_html.replace("CHATBOT_TITLE", title)
    text_html = htmlmin.minify(text_html)
    text_css = file_css.read()
    text_css = text_css.replace("LOGO_PATH", logo_path)
    text_css = text_css.replace("SPRITE_PATH", sprite_path)
    text_css = cssmin(text_css)

    text_js = text_js.replace("HTML_CODE", text_html)
    text_js = text_js.replace("CSS_CODE", text_css)
    text_js = text_js.replace("APP_ID", str(pk))

    minified = jsmin(text_js)

    return minified
Example #23
0
class Bundle:
	"""
		Concatenate, compress and mix (if required) js+css files from build.json
	"""
	no_compress = False
	timestamps = {}
	path = '.'
	
	def concat(self, filelist, outfile=None):	
		"""
			Concat css and js files into a bundle
		"""
		from cStringIO import StringIO
		
		out_type = outfile and outfile.split('.')[-1] or 'js'
		
		outtxt = ''
		for f in filelist:
			suffix = None
			if ':' in f:
				f, suffix = f.split(':')
			
			if not os.path.exists(f) or os.path.isdir(f):
				continue
			
			self.timestamps[f] = os.path.getmtime(f)
			
			# get datas
			try:
				with open(f, 'r') as infile:			
					# get file type
					ftype = f.split('.')[-1] 

					data = unicode(infile.read(), 'utf-8', errors='ignore')

				outtxt += ('\n/*\n *\t%s\n */' % f)
					
				# append
				if suffix=='concat' or out_type != 'js' or self.no_compress or ('.min.' in f):
					outtxt += '\n' + data + '\n'
				else:
					jsm = JavascriptMinify()
					tmpin = StringIO(data.encode('utf-8'))
					tmpout = StringIO()
					
					jsm.minify(tmpin, tmpout)
					tmpmin = unicode(tmpout.getvalue() or '', 'utf-8')
					tmpmin.strip('\n')
					outtxt += tmpmin
			except Exception, e:
				print "--Error in:" + f + "--"
				print webnotes.getTraceback()

		if not self.no_compress and out_type == 'css':
			outtxt = cssmin(outtxt)
						
		with open(outfile, 'w') as f:
			f.write(outtxt.encode("utf-8"))
		
		print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))
Example #24
0
def build(r):
    settings = config.getSettings()
    # Get the base stylesheet
    if 'disable' in settings['stylesheet'] and settings['stylesheet'][
            'disable']:
        return None
    stylesheet = config.getStylesheet()
    # Grab the "license" text
    header = config.getStylesheetHeader()
    # Handle the demonyms, if specified in the settings to do so
    if settings['stylesheet']['demonyms_enabled']:
        demonyms = getDemonymsCss()
    else:
        demonyms = ''
    # Handle the multiple-headers settings
    if settings['stylesheet']['num_of_headers'] > 1:
        stylesheet = stylesheet.replace('%%header1%%', getHeader())
    # Handle the new header cycle if enabled
    if settings['stylesheet']['new_header_cycle']['enabled']:
        getNextHeader(r)
    # Prepend the demonyms
    stylesheet = demonyms + "\n\n" + stylesheet
    # Handle the minify stylesheet setting without affecting the header
    if settings['stylesheet']['minify']:
        stylesheet = cssmin(stylesheet)
    # Add the header
    stylesheet = header + "\n\n" + stylesheet
    return stylesheet
Example #25
0
def process(sources, preserve_paths=True, outdir=None):
    if not outdir:
        raise TypeError("Missing the required 'outdir' keyword argument.")

    sources.sort()
    if sources:
        ensure_directory(outdir)

        # Copy the CSS resource file to the documentation directory.
        output_css_filepath = os.path.join(outdir, "pycco.css")
        input_css_filepath = os.path.join(DIR_PATH, "pycco.css")
        minified_css = cssmin.cssmin(open(input_css_filepath).read())
        with open(output_css_filepath, 'wb') as f:
            f.write(minified_css)

        def next_file():
            s = sources.pop(0)
            dest = destination(s, preserve_paths=preserve_paths, outdir=outdir)

            try:
                os.makedirs(os.path.split(dest)[0])
            except OSError:
                pass

            with open(destination(s,
                                  preserve_paths=preserve_paths,
                                  outdir=outdir), "w") as f:
                f.write(generate_documentation(s, outdir=outdir))

            print "pycco = %s -> %s" % (s, dest)

            if sources:
                next_file()

        next_file()
Example #26
0
	def _processCCSS( self, path ):
		"""Processes a CCSS file, minifying it if `cssmin` is installed.
		Requires `clevercss`"""
		data = self.app().load(path)
		data = clevercss.convert(data)
		if self.minify and cssmin: data = cssmin.cssmin(data)
		return data
 def css(self) -> str:
     try:
         file = Path(self.getCurrentDir(), f'css/{self.name}.css')
         content = cssmin(file.read_text())
         return content
     except:
         return ''
Example #28
0
def handle_css(css_data):
    """
    压缩css文件
    :param css_data: css字符串
    :return:
    """
    return cssmin.cssmin(css_data)
Example #29
0
    def __js_css_render(self, should_minify, path, dist_path, extension):
        ''' . '''
        for file in os.listdir(path):
            #
            if os.path.isfile(path + file) and file.endswith(extension):
                #
                if should_minify and '.min.' not in file:
                    with open(path + file, 'r') as f:
                        min_name = file.replace(extension, '.min' + extension)

                        with self.__write_file_path(dist_path +
                                                    min_name) as min_f:
                            if extension == '.js':
                                min_f.write(jsmin(f.read()))
                            elif extension == '.css':
                                min_f.write(cssmin(f.read()))
                else:
                    #
                    shutil.copy(path + file, dist_path + file)
            elif os.path.isdir(path + file):
                if Settings.get_instance().prop('app_settings.use_symlinks'):
                    try:
                        # create symlink of the directory
                        os.symlink(path + file, dist_path + file)
                    except Exception as e:
                        pass  # the symlink already exists
Example #30
0
    def _compress(self):
        output = []

        for src in self.includes:
            with codecs.open(self._get_path(src), encoding='utf-8') as f:
                output.append(cssmin(f.read()))

        return '\n'.join(output)
    def _compress(self):
        output = []

        for src in self.includes:
            with codecs.open(self._get_path(src), encoding='utf-8') as f:
                output.append(cssmin(f.read()))

        return '\n'.join(output)
    def text_resource_complete(self, resource, text):
        """
        Minify all CSS
        """

        if not resource.source_file.kind == 'css':
            return

        return cssmin(text)
Example #33
0
 def _min_compress(self, original_file, file_type):
     if file_type == "css":
         return cssmin.cssmin(original_file.read())
     elif file_type == "js":
         return jsmin(original_file.read(), keep_bang_comments=True)
         #return _rjsmin.jsmin(str(original_file.read()), keep_bang_comments=True)
         #return uglipyjs.compile(original_file.read())
         #return slimit.minify(original_file.read())
     return original_file
    def get(self):
        # key is provided in the query string
        key = self.request.get("id")
        try:
            # it might be an invalid key so we better check

            object = db.get(key)

        except db.BadKeyError:
            # if it is then return a 404

            self.error(404)
            return

        logging.getLogger().info("prop %s" % self.property)

        if self.property == "css":
            if object and object.file:
                file = None
                if str(self.request.get("org")).__eq__("true"):
                    file = object.original
                else:
                    file = object.file

                compress = self.request.get("compress")
                if compress == "true":
                    file = cssmin.cssmin(file)
                # we have an file so prepare the response
                # with the relevant headers
                self.response.headers["Content-Type"] = "text/css"
                filename = object.name.encode("utf-8")
                self.response.headers["Content-Disposition"] = 'inline; filename="%s"' % filename
                # and then write our the image data direct to the response
                self.response.out.write(file)
            else:
                self.error(404)
        elif self.property == "jscript":
            if object and object.file:
                file = object.file
                # we have an file so prepare the response
                # with the relevant headers
                self.response.headers["Content-Type"] = "text/javascript"
                filename = object.name.encode("utf-8")
                self.response.headers["Content-Disposition"] = 'inline; filename="%s"' % filename
                # and then write our the image data direct to the response
                self.response.out.write(file)
            else:
                self.error(404)
        elif self.property == "image" or self.property == "thumb":
            if object and object.image:
                # we have an image so prepare the response
                # with the relevant headers
                self.response.headers["Content-Type"] = "image/png"
                # and then write our the image data direct to the response
                self.response.out.write(eval("object.%s" % self.property))
            else:
                self.error(404)
Example #35
0
 def render(self):
     try:
         from cssmin import cssmin
     except ImportError:
         return
     else:
         data = cssmin(self.data)
         if data:
             return data
Example #36
0
def render_asset(template, request, content_type="text/plain",
                 force_shrink=True):
    language = getattr(request, 'LANGUAGE_CODE', "")
    key = "asset-%s-%s" % (template, language)
    resp = cache.get(key, False)
    debug = settings.DEBUG

    if resp is False:
        try:
            resp = render_to_string(template_name=template, request=request)
        except Exception as e:
            if debug:
                raise e
            else:
                logger.warning(e)
            return HttpResponseBadRequest("bad Request: %s" % template)

        if not debug or force_shrink is True:
            try:
                if content_type.endswith("javascript"):
                    logger.info("js minified")
                    from jsmin import jsmin
                    resp = jsmin(resp)
                else:
                    logger.info("content Type: %s", content_type)
            except Exception as e:
                logger.warning("error shrinking js: %s", e)

            try:
                if content_type.endswith("css"):
                    from cssmin import cssmin
                    resp = cssmin(resp)
            except Exception as e:
                logger.warning("error shrinking css: %s", e)

        resp = HttpResponse(resp)
        etag = sha224(resp.content).hexdigest()
        resp['Content-Type'] = content_type
        resp['etag'] = etag

        now = timezone.now()
        modified = now.astimezone(timezone.utc).strftime('%H:%M:%S-%a/%d/%b/%Y')
        resp['Last-Modified'] = modified

        if not debug:
            resp['Cache-Control'] = 'public, max-age=6000'
            expires = now.astimezone(timezone.utc) + timedelta(days=365)
            resp['Expires'] = expires.strftime('%H:%M:%S-%a/%d/%b/%Y')
            cache.set(key, resp, 600)
            logger.info("caching: %s" % template)
        else:
            resp['Cache-Control'] = 'public, max-age=60'
            expires = now.astimezone(timezone.utc) + timedelta(minutes=3)
            resp['Expires'] = expires.strftime('%H:%M:%S-%a/%d/%b/%Y')
            cache.set(key, resp, 60)
            logger.info("caching: %s" % template)
    return resp
    def process_response(self, request, response):
        if response.streaming:
            return response

        try:
            if request.accepts('application/json'):
                return response
        except:
            pass

        content = response.content.decode(self.encoding)

        match = CRITICAL_CSS_RE.search(content)

        if not match:
            return response

        critical_css_fragment = match.group(1)

        key_match = CRITICAL_KEY_RE.search(content)
        if key_match:
            content = CRITICAL_KEY_RE.sub('', content)
            key = key_match.group(1)
        else:
            key = 'default'

        h = hashlib.sha1()
        h.update(critical_css_fragment)
        cache_key = 'django_critical:{hash}:{key}'.format(
            hash=h.hexdigest(), key=key)

        cached = cache.get(cache_key)

        if cached is not None:
            new_fragment, css_entries = cached
        else:
            css_entries = extract_css_entries(request, critical_css_fragment)
            css = download_css(css_entries, self.encoding)
            critical_css = get_critical_css(content, css)
            critical_css = cssmin(critical_css)
            new_fragment = '<style>{css}</style>'.format(
                css=critical_css.replace('\\', '\\\\'))
            cache.set(cache_key, (new_fragment, css_entries))

        content = CRITICAL_CSS_RE.sub(new_fragment, content)

        async_snippet_template = get_template('critical/async_snippet.html')
        async_snippet = async_snippet_template.render(Context({
            'critical_css_fragment': critical_css_fragment,
            'css_entries': css_entries,
        }))

        content = ASYNC_SNIPPET_RE.sub(async_snippet, content)

        response.content = content.encode(self.encoding)
        return response
Example #38
0
def compile_critical(pelican):
    """Add Webassets to Jinja2 extensions in Pelican settings."""
    theme_path = os.path.join(pelican.settings.get('PATH'), os.pardir,
                              'pelican_site', 'theme')
    critical_css_path = os.path.join(theme_path, 'static', 'scss',
                                     'critical.scss')
    compiled_css = compile_file(critical_css_path)
    minified_css = cssmin(compiled_css)
    with open(os.path.join(theme_path, 'templates', 'critical.css'), 'w') as f:
        f.write(minified_css)
Example #39
0
def cssmin(input_file):
    """
    :type   input_file: File 

    :returns: File 
    """

    content_bytes = input_file.read()
    output_bytes  = cssmin_mod.cssmin(content_bytes)

    return ContentFile(output_bytes)
Example #40
0
    def _compress(self):
        output = []

        for src in self.includes:
            if src.endswith('less'):
                src = src.replace('less', 'css')

            with open('www/%s' % src) as f:
                output.append(cssmin(f.read()))

        return '\n'.join(output)
Example #41
0
def compress_css(path):
    """Compress CSS files in `path` with cssmin."""
    import cssmin
    files = glob.iglob(os.path.join(path, '*.css'))

    for css in files:
        with open(css, 'r+') as source:
            compressed = cssmin.cssmin(source.read())
            source.seek(0)
            source.write(compressed)
            source.truncate()
Example #42
0
def cssmin(input_file):
    """
    :type   input_file: File 

    :returns: File 
    """

    content_bytes = input_file.read()
    output_bytes = cssmin_mod.cssmin(content_bytes)

    return ContentFile(output_bytes)
Example #43
0
def main():
    """ for each CSS file, run it through cssmin.py and compare the result
        to the supplied .min file.
    """
    tests = glob.glob('tests/*.css')
    for test_file in tests:
        computed = cssmin.cssmin(open(test_file).read())
        control = open('%s.min' % test_file).read()
        if computed == control:
            PASSED.append(test_file)
        else:
            FAILED.append(test_file)

    if '-v' in sys.argv:

        test_file = FAILED[0]
        print 'FIRST FAILURE: %2d %s' % (tests.index(test_file), test_file)
        print '========%s=\n' % ('=' * len(test_file))
        computed = cssmin.cssmin(open(test_file).read())
        control = open('%s.min' % test_file).read()

        differ = difflib.Differ()
        diff = differ.compare(
            cssmin.css_expand(control).split('\n'),
            cssmin.css_expand(computed).split('\n'),
        )
        print '\n'.join(diff)

    else:

        print 'Passed: %d' % len(PASSED)
        print '============='
        for test_file in PASSED:
            print '  %2d: %s' % (tests.index(test_file), test_file)

        print '\n\n'

        print 'Failed: %d' % len(FAILED)
        print '============='
        for test_file in FAILED:
            print '  %2d: %s' % (tests.index(test_file), test_file)
    def icon(self) -> str:
        try:
            file = Path(self.getCurrentDir(), f'templates/{self.name}.html')
            content = cssmin(file.read_text())
            header = re.search(r'<icon>(.*)</icon>', content)
            if header:
                return header.group(1)

            return ''
        except:
            self.logWarning(f"Widget doesn't have any icon")
            return ''
Example #45
0
def minify_css_file(filename):
    """Create a minified CSS file, overwriting the original.

    :param str filename: The file to minify.
    """

    with open(filename, encoding='utf-8') as f:
        raw = f.read()

    with open(filename, 'w', encoding='utf-8') as f:
        logger.debug('Minifying: %s' % filename)
        f.write(cssmin(raw))
Example #46
0
    def _compress(self):
        output = []

        for src in self.includes:
            if src.endswith('less'):
                src = src.replace('less', 'css') # less/example.less -> css/example.css
                src = '%s.less.css' % src[:-4]   # css/example.css -> css/example.less.css

            with open('www/%s' % src) as f:
                print '- compressing %s' % src
                output.append(cssmin(f.read()))

        return '\n'.join(output)
    def render(self, context):
        linebreaks = re.compile(r'[\n\r]')
        urls = re.compile(r'url\([\'"]?([^\(\)\'"]*)[\'"]?\)')
        output = re.sub(linebreaks, '', self.nodelist.render(context)).strip()
        html_checksum = md5(output).hexdigest()

        cache_key = "statictastic:{}".format(html_checksum)
        pipelined_html = cache.get(cache_key)
        if not pipelined_html:
            input_elements = html.fragments_fromstring(output)
            output_elements = []
            compiled_css_content = ""
            for element in input_elements:
                element.make_links_absolute(settings.BASE_URL)
                if element.tag == "link" and 'rel' in element.attrib and element.attrib[
                        'rel'] == "stylesheet":
                    css_url = element.attrib['href']
                    response = requests.get(css_url)
                    content = response.content
                    for match in urls.finditer(content):
                        if match:
                            relative_url = match.group(1)
                            content = content.replace(
                                relative_url, urljoin(css_url, relative_url))

                    compiled_css_content += content
                elif element.tag == 'style' and 'type' in element.attrib and element.attrib[
                        'type'] == 'text/css':
                    compiled_css_content += element.text_content()
                else:
                    output_elements.append(element)

            compiled_css_content = cssmin.cssmin(compiled_css_content)
            staticstorage = storage.staticfiles_storage
            checksum = md5(compiled_css_content).hexdigest()

            # TODO: This breaks relative links. Figure out a way to rewrite those appropriately.
            filename = "bundled/css/{}.css".format(checksum)
            staticstorage.save(filename, ContentFile(compiled_css_content))
            output_elements.append(
                etree.Element("link",
                              attrib={
                                  'rel': 'stylesheet',
                                  'href': staticstorage.url(filename)
                              }))

            pipelined_html = "".join(
                html.tostring(element) for element in output_elements)
            cache.set(cache_key, pipelined_html, 86400)

        return pipelined_html
    def post(self):
        fil = self.request.POST["file"]
        if not fil.value:
            self.redirect("/")
            return

        file = File()
        file.name = fil.filename
        file.file = db.Blob(cssmin.cssmin(fil.value))
        file.original = db.Blob(fil.value)
        file.content_type = fil.type
        file.user = users.get_current_user()
        file.put()
        self.redirect("/")
Example #49
0
def compile_critical(pelican):
    """Add Webassets to Jinja2 extensions in Pelican settings."""
    theme_path = os.path.join(pelican.settings.get('PATH'),
                              os.pardir,
                              'pelican_site',
                              'theme')
    critical_css_path = os.path.join(theme_path,
                                     'static',
                                     'scss',
                                     'critical.scss')
    compiled_css = compile_file(critical_css_path)
    minified_css = cssmin(compiled_css)
    with open(os.path.join(theme_path, 'templates', 'critical.css'), 'w') as f:
        f.write(minified_css)
def _minify_css(e):
    """Minifies the CSS in a style element"""
    import cssmin

    # Only handle CSS elements
    if e.nodeType != Node.ELEMENT_NODE \
            or not e.tagName == 'style' \
            or not e.getAttribute('type') == 'text/css':
        return

    for c in e.childNodes:
        if c.nodeType != Node.CDATA_SECTION_NODE:
            continue
        c.replaceWholeText(cssmin.cssmin(c.wholeText))
Example #51
0
def compress_css(paths):
    buf = StringIO()
    out = StringIO()
    gz = gzip.GzipFile(fileobj=out, mode='w')
    for path in paths:
        with open(path, 'r') as in_file:
            while 1:
                data = in_file.read(CHUNK_SIZE)
                if not data:
                    break
                buf.write(data)
        buf.write('\n')
    gz.write(cssmin.cssmin(buf.getvalue()))
    gz.close()
    return out.getvalue()
Example #52
0
def exportMinFileCSS(listFiles, outputDir):
    
    for i in range(len(listFiles)):
        files = open( listFiles[i]['path'], 'r')
        outPutFiles = open(outputDir/listFiles[i]['basename'], 'w')
        for linea in files.readlines():
            comment_end = linea.find("*/")
            if comment_end < 0:
                outPutFiles.write(linea)
            else:
                outPutFiles.write(linea)
                files.seek(0)
                break
        outPutFiles.write(cssmin.cssmin(files.read()))
        outPutFiles.close()
Example #53
0
 def getComponents(self, jsc, cssc, minimized):
     """
         Takes lists of Javascript and CSS resource specifications, and
         returns lists of corresponding data.
     """
     jslibs = []
     for i, exclusions in jsc:
         d = utils.data.read("components/%s.js" % i)
         d = snip(d, exclusions)
         jslibs.append(jsmin.jsmin(d) if minimized else d)
     css = []
     for i in cssc:
         d = utils.data.read("components/%s.css" % i)
         css.append(cssmin.cssmin(d) if minimized else d)
     return jslibs, css
Example #54
0
def compress_css(paths):
    buf = StringIO()
    out = StringIO()
    gz = gzip.GzipFile(fileobj=out, mode='w')
    for path in paths:
        with open(path, 'r') as in_file:
            while 1:
                data = in_file.read(CHUNK_SIZE)
                if not data:
                    break
                buf.write(data)
        buf.write('\n')
    gz.write(cssmin.cssmin(buf.getvalue()))
    gz.close()
    return out.getvalue()