def __call__(self, oeb, context): oeb.logger.info('Flattening CSS and remapping font sizes...') self.context = self.opts =context self.oeb = oeb self.filter_css = frozenset() if self.opts.filter_css: try: self.filter_css = {x.strip().lower() for x in self.opts.filter_css.split(',')} except: self.oeb.log.warning('Failed to parse filter_css, ignoring') else: from calibre.ebooks.oeb.normalize_css import normalize_filter_css self.filter_css = frozenset(normalize_filter_css(self.filter_css)) 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()
def filter_css(container, properties, names=()): ''' Remove the specified CSS properties from all CSS rules in the book. :param properties: Set of properties to remove. For example: :code:`{'font-family', 'color'}`. :param names: The files from which to remove the properties. Defaults to all HTML and CSS files in the book. ''' properties = normalize_filter_css(properties) return transform_css(container, transform_sheet=partial(filter_sheet, properties=properties), transform_style=partial(filter_declaration, properties=properties), names=names)
def __call__(self, oeb, context): oeb.logger.info('Flattening CSS and remapping font sizes...') self.context = self.opts = context self.oeb = oeb self.items = list(self.oeb.spine) titlepage = self.oeb.guide.get('titlepage') if titlepage is not None: titlepage = titlepage.item if titlepage is not None and titlepage not in self.items: self.items.append(titlepage) epub3_nav = None if getattr(self.opts, 'epub3_nav_href', None): epub3_nav = self.oeb.manifest.hrefs.get(self.opts.epub3_nav_href) if epub3_nav is not None and epub3_nav not in self.items: self.items.append(epub3_nav) self.filter_css = frozenset() if self.opts.filter_css: try: self.filter_css = { x.strip().lower() for x in self.opts.filter_css.split(',') } except: self.oeb.log.warning('Failed to parse filter_css, ignoring') else: from calibre.ebooks.oeb.normalize_css import normalize_filter_css self.filter_css = frozenset( normalize_filter_css(self.filter_css)) 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: css_parser.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() if epub3_nav is not None: self.opts.epub3_nav_parsed = epub3_nav.data self.store_page_margins()
def filter_css(container, properties, names=()): ''' Remove the specified CSS properties from all CSS rules in the book. :param properties: Set of properties to remove. For example: :code:`{'font-family', 'color'}`. :param names: The files from which to remove the properties. Defaults to all HTML and CSS files in the book. ''' if not names: types = OEB_STYLES | OEB_DOCS names = [] for name, mt in container.mime_map.iteritems(): if mt in types: names.append(name) properties = normalize_filter_css(properties) doc_changed = False for name in names: mt = container.mime_map[name] if mt in OEB_STYLES: sheet = container.parsed(name) filtered = filter_sheet(sheet, properties) if filtered: container.dirty(name) doc_changed = True elif mt in OEB_DOCS: root = container.parsed(name) changed = False for style in root.xpath('//*[local-name()="style"]'): if style.text and style.get( 'type', 'text/css') in {None, '', 'text/css'}: sheet = container.parse_css(style.text) if filter_sheet(sheet, properties): changed = True style.text = force_unicode(sheet.cssText, 'utf-8') pretty_script_or_style(container, style) for elem in root.xpath('//*[@style]'): text = elem.get('style', None) if text: style = container.parse_css(text, is_declaration=True) if filter_declaration(style, properties): changed = True if style.length == 0: del elem.attrib['style'] else: elem.set( 'style', force_unicode(style.getCssText(separator=' '), 'utf-8')) if changed: container.dirty(name) doc_changed = True return doc_changed
def filter_css(container, properties, names=()): ''' Remove the specified CSS properties from all CSS rules in the book. :param properties: Set of properties to remove. For example: :code:`{'font-family', 'color'}`. :param names: The files from which to remove the properties. Defaults to all HTML and CSS files in the book. ''' properties = normalize_filter_css(properties) return transform_css(container, transform_sheet=partial(filter_sheet, properties=properties), transform_style=partial(filter_declaration, properties=properties), names=names)
def filter_css(container, properties, names=()): """ Remove the specified CSS properties from all CSS rules in the book. :param properties: Set of properties to remove. For example: :code:`{'font-family', 'color'}`. :param names: The files from which to remove the properties. Defaults to all HTML and CSS files in the book. """ if not names: types = OEB_STYLES | OEB_DOCS names = [] for name, mt in container.mime_map.iteritems(): if mt in types: names.append(name) properties = normalize_filter_css(properties) doc_changed = False for name in names: mt = container.mime_map[name] if mt in OEB_STYLES: sheet = container.parsed(name) filtered = filter_sheet(sheet, properties) if filtered: container.dirty(name) doc_changed = True elif mt in OEB_DOCS: root = container.parsed(name) changed = False for style in root.xpath('//*[local-name()="style"]'): if style.text and style.get("type", "text/css") in {None, "", "text/css"}: sheet = container.parse_css(style.text) if filter_sheet(sheet, properties): changed = True style.text = force_unicode(sheet.cssText, "utf-8") pretty_script_or_style(container, style) for elem in root.xpath("//*[@style]"): text = elem.get("style", None) if text: style = container.parse_css(text, is_declaration=True) if filter_declaration(style, properties): changed = True if style.length == 0: del elem.attrib["style"] else: elem.set("style", force_unicode(style.getCssText(separator=" "), "utf-8")) if changed: container.dirty(name) doc_changed = True return doc_changed
def filter_css(container, properties, names=()): if not names: types = OEB_STYLES | OEB_DOCS names = [] for name, mt in container.mime_map.iteritems(): if mt in types: names.append(name) properties = normalize_filter_css(properties) doc_changed = False for name in names: mt = container.mime_map[name] if mt in OEB_STYLES: sheet = container.parsed(name) filtered = filter_sheet(sheet, properties) if filtered: container.dirty(name) doc_changed = True elif mt in OEB_DOCS: root = container.parsed(name) changed = False for style in root.xpath('//*[local-name()="style"]'): if style.text and style.get( 'type', 'text/css') in {None, '', 'text/css'}: sheet = container.parse_css(style.text) if filter_sheet(sheet, properties): changed = True style.text = force_unicode(sheet.cssText, 'utf-8') pretty_script_or_style(container, style) for elem in root.xpath('//*[@style]'): text = elem.get('style', None) if text: style = container.parse_css(text, is_declaration=True) if filter_declaration(style, properties): changed = True if style.length == 0: del elem.attrib['style'] else: elem.set( 'style', force_unicode(style.getCssText(separator=' '), 'utf-8')) if changed: container.dirty(name) doc_changed = True return doc_changed
def filter_css(container, properties, names=()): if not names: types = OEB_STYLES | OEB_DOCS names = [] for name, mt in container.mime_map.iteritems(): if mt in types: names.append(name) properties = normalize_filter_css(properties) doc_changed = False for name in names: mt = container.mime_map[name] if mt in OEB_STYLES: sheet = container.parsed(name) filtered = filter_sheet(sheet, properties) if filtered: container.dirty(name) doc_changed = True elif mt in OEB_DOCS: root = container.parsed(name) changed = False for style in root.xpath('//*[local-name()="style"]'): if style.text and style.get('type', 'text/css') in {None, '', 'text/css'}: sheet = container.parse_css(style.text) if filter_sheet(sheet, properties): changed = True style.text = force_unicode(sheet.cssText, 'utf-8') pretty_script_or_style(container, style) for elem in root.xpath('//*[@style]'): text = elem.get('style', None) if text: style = container.parse_css(text, is_declaration=True) if filter_declaration(style, properties): changed = True if style.length == 0: del elem.attrib['style'] else: elem.set('style', force_unicode(style.getCssText(separator=' '), 'utf-8')) if changed: container.dirty(name) doc_changed = True return doc_changed