def csscombine(path=None, url=None, cssText=None, href=None, sourceencoding=None, targetencoding=None, minify=True, resolveVariables=True): """Combine sheets referred to by @import rules in given CSS proxy sheet into a single new sheet. :returns: combined cssText, normal or minified :Parameters: `path` or `url` or `cssText` + `href` path or URL to a CSSStyleSheet or a cssText of a sheet which imports other sheets which are then combined into one sheet. `cssText` normally needs `href` to be able to resolve relative imports. `sourceencoding` = 'utf-8' explicit encoding of the source proxysheet `targetencoding` encoding of the combined stylesheet `minify` = True defines if the combined sheet should be minified, in this case comments are not parsed at all! `resolveVariables` = True defines if variables in combined sheet should be resolved """ css_parser.log.info('Combining files from %r' % url, neverraise=True) if sourceencoding is not None: css_parser.log.info('Using source encoding %r' % sourceencoding, neverraise=True) parser = css_parser.CSSParser(parseComments=not minify) if path and not cssText: src = parser.parseFile(path, encoding=sourceencoding) elif url: src = parser.parseUrl(url, encoding=sourceencoding) elif cssText: src = parser.parseString(cssText, href=href, encoding=sourceencoding) else: sys.exit('Path or URL must be given') result = css_parser.resolveImports(src) result.encoding = targetencoding css_parser.log.info('Using target encoding: %r' % targetencoding, neverraise=True) oldser = css_parser.ser css_parser.setSerializer(css_parser.serialize.CSSSerializer()) if minify: css_parser.ser.prefs.useMinified() css_parser.ser.prefs.resolveVariables = resolveVariables cssText = result.cssText css_parser.setSerializer(oldser) return cssText
def test_setCSSSerializer(self): "css_parser.setSerializer() and css_parser.ser" s = css_parser.parseString('a { left: 0 }') exp4 = '''a { left: 0 }''' exp1 = '''a { left: 0 }''' self.assertEqual(exp4.encode(), s.cssText) newser = css_parser.CSSSerializer( css_parser.serialize.Preferences(indent=' ')) css_parser.setSerializer(newser) self.assertEqual(exp1.encode(), s.cssText) newser = css_parser.CSSSerializer( css_parser.serialize.Preferences(indent=' ')) css_parser.ser = newser self.assertEqual(exp4.encode(), s.cssText)
def reformat_css(css_string, useoneline): new_css_string = "" css_errors = "" css_warnings = "" prefs = get_prefs() if useoneline: prefs.indent = '' prefs.lineSeparator = '' prefs.listItemSpacer = ' ' prefs.paranthesisSpacer = '' prefs.propertyNameSpacer = '' prefs.selectorCombinatorSpacer = '' prefs.spacer = ' ' prefs.validOnly = False prefs.linesAfterRules = 1 * '\n' css_parser.setSerializer(MyCSSSerializer(prefs)) aparser = css_parser.CSSParser(raiseExceptions=True, validate=False, fetcher=nofetch) try: parsed_css = aparser.parseString(css_string) except Exception as E: # css_parser.xml.dom.HierarchyRequestErr as E: # parsing error - make no changes css_errors = str(E) new_css_string = css_string else: # 0 means UNKNOWN_RULE, as from cssparser.css.cssrule.CSSRule for unknown_rule in parsed_css.cssRules.rulesOfType(0): line = css_string[:css_string.find(unknown_rule.atkeyword)].count( '\n') + 1 css_warnings += "Unknown rule: " + unknown_rule.atkeyword + " at line: " + line + '\n' # we want unicode not a byte string here new_css_string = parsed_css.cssText.decode('utf-8', errors='replace') return (new_css_string, css_errors, css_warnings)
def setUp(self): if not new_parser: cssutils.setSerializer(customcssutils.MyCSSSerializer()) self.css = cssutils.parseFile( os.path.join(os.path.dirname(__file__), 'resources', 'base.css'))