How to use the cssutils.replaceUrls function in cssutils

To help you get started, we’ve selected a few cssutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jorgebastida / cssbuster / cssbuster.py View on Github external
# Create the parser
    parser = cssutils.CSSParser(log=log,
                                raiseExceptions=True,
                                parseComments=not options.minified,
                                validate=False)
    try:
        # Parse the original file
        sheet = parser.parseFile(args[0])
    except Exception, e:
        sys.stderr.write('Error: %s %s\n' % (css_path, e.args[0]))
        sys.exit(1)

    # Replace all the urls
    replacer = partial(cache_bust_replacer, options, css_path, img_rel_path)
    cssutils.replaceUrls(sheet, replacer, ignoreImportRules=True)

    # print the new css
    sys.stdout.write(sheet.cssText)
github palexu / send2kindle / calibre / ebooks / oeb / transforms / flatcss.py View on Github external
self.filter_css = frozenset()
        if self.opts.filter_css:
            try:
                self.filter_css = frozenset([x.strip().lower() for x in
                    self.opts.filter_css.split(',')])
            except:
                self.oeb.log.warning('Failed to parse filter_css, ignoring')
            else:
                self.oeb.log.debug('Filtering CSS properties: %s'%
                    ', '.join(self.filter_css))

        for item in oeb.manifest.values():
            # Make all links to resources absolute, as these sheets will be
            # consolidated into a single stylesheet at the root of the document
            if item.media_type in OEB_STYLES:
                cssutils.replaceUrls(item.data, item.abshref,
                        ignoreImportRules=True)

        self.body_font_family, self.embed_font_rules = self.get_embed_font_info(
                self.opts.embed_font_family)
        # Store for use in output plugins/transforms that generate content,
        # like the AZW3 output inline ToC.
        self.oeb.store_embed_font_rules = EmbedFontsCSSRules(self.body_font_family,
                self.embed_font_rules)
        self.stylize_spine()
        self.sbase = self.baseline_spine() if self.fbase else None
        self.fmap = FontMapper(self.sbase, self.fbase, self.fkey)
        self.flatten_spine()
github hsoft / pdfmasher / ebooks / oeb / base.py View on Github external
if v.CSS_PRIMITIVE_VALUE == v.cssValueType and \
           v.CSS_URI == v.primitiveType:
                v.setStringValue(v.CSS_URI,
                        link_repl_func(v.getStringValue()))

    for el in root.iter():
        try:
            tag = el.tag
        except UnicodeDecodeError:
            continue

        if tag == XHTML('style') and el.text and \
                (_css_url_re.search(el.text) is not None or '@import' in
                        el.text):
            stylesheet = parseString(el.text)
            replaceUrls(stylesheet, link_repl_func)
            repl = stylesheet.cssText
            if isbytestring(repl):
                repl = repl.decode('utf-8')
            el.text = '\n'+ repl + '\n'

        if 'style' in el.attrib:
            text = el.attrib['style']
            if _css_url_re.search(text) is not None:
                try:
                    stext = parseStyle(text)
                except:
                    # Parsing errors are raised by cssutils
                    continue
                for p in stext.getProperties(all=True):
                    v = p.cssValue
                    if v.CSS_VALUE_LIST == v.cssValueType:
github hsoft / pdfmasher / ebooks / oeb / transforms / filenames.py View on Github external
def __call__(self, oeb):
        self.oeb = oeb

        for item in oeb.manifest.items:
            self.current_item = item
            if etree.iselement(item.data):
                rewrite_links(self.current_item.data, self.url_replacer)
            elif hasattr(item.data, 'cssText'):
                cssutils.replaceUrls(item.data, self.url_replacer)

        if self.oeb.guide:
            for ref in list(self.oeb.guide.values()):
                href = urlnormalize(ref.href)
                href, frag = urldefrag(href)
                replacement = self.rename_map.get(href, None)
                if replacement is not None:
                    nhref = replacement
                    if frag:
                        nhref += '#' + frag
                    ref.href = nhref

        if self.oeb.toc:
            self.fix_toc_entry(self.oeb.toc)
github hsoft / pdfmasher / ebooks / html / input.py View on Github external
for f in filelist:
            path = f.path
            dpath = os.path.dirname(path)
            oeb.container = DirContainer(dpath, ignore_opf=True)
            item = oeb.manifest.hrefs[htmlfile_map[path]]
            rewrite_links(item.data, partial(self.resource_adder, base=dpath))

        for item in list(oeb.manifest.values()):
            if item.media_type in self.OEB_STYLES:
                dpath = None
                for path, href in list(self.added_resources.items()):
                    if href == item.href:
                        dpath = os.path.dirname(path)
                        break
                cssutils.replaceUrls(item.data, partial(self.resource_adder, base=dpath))

        toc = self.oeb.toc
        headers = []
        for item in self.oeb.spine:
            if not item.linear:
                continue
            html = item.data
            for header in find_headers(html):
                headers.append((item, header))
        for i, (item, header) in enumerate(headers):
            if not item.linear:
                continue
            tocid = 'tocid{}'.format(i)
            header.attrib['id'] = tocid
            link = '{}#{}'.format(item.href, tocid)
            toc.add(header.text, link)
github kovidgoyal / calibre / src / calibre / ebooks / oeb / transforms / package.py View on Github external
def rewrite_links_in_css(self, sheet, base, new_base):
        repl = partial(self.link_replacer, base=base, new_base=new_base)
        cssutils.replaceUrls(sheet, repl)
github palexu / send2kindle / calibre / ebooks / oeb / base.py View on Github external
stylesheet = parser.parseString(el.text, validate=False)
            replaceUrls(stylesheet, link_repl_func)
            repl = stylesheet.cssText
            if isbytestring(repl):
                repl = repl.decode('utf-8')
            el.text = '\n'+ repl + '\n'

        if 'style' in el.attrib:
            text = el.attrib['style']
            if _css_url_re.search(text) is not None:
                try:
                    stext = parser.parseStyle(text, validate=False)
                except:
                    # Parsing errors are raised by cssutils
                    continue
                replaceUrls(stext, link_repl_func)
                repl = stext.cssText.replace('\n', ' ').replace('\r',
                        ' ')
                if isbytestring(repl):
                    repl = repl.decode('utf-8')
                el.attrib['style'] = repl