mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor to separate the logic for iterating over all styles in a book
This commit is contained in:
parent
5ca8ccf78a
commit
85f38c0bb4
@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
from cssutils.css import CSSRule
|
from cssutils.css import CSSRule
|
||||||
from css_selectors import parse, SelectorSyntaxError
|
from css_selectors import parse, SelectorSyntaxError
|
||||||
|
|
||||||
@ -141,7 +143,7 @@ def remove_unused_css(container, report=None, remove_unused_classes=False):
|
|||||||
report(_('No unused class attributes found'))
|
report(_('No unused class attributes found'))
|
||||||
return num_of_removed_rules + num_of_removed_classes > 0
|
return num_of_removed_rules + num_of_removed_classes > 0
|
||||||
|
|
||||||
def filter_declaration(style, properties):
|
def filter_declaration(style, properties=()):
|
||||||
changed = False
|
changed = False
|
||||||
for prop in properties:
|
for prop in properties:
|
||||||
if style.removeProperty(prop) != '':
|
if style.removeProperty(prop) != '':
|
||||||
@ -159,7 +161,7 @@ def filter_declaration(style, properties):
|
|||||||
style.setProperty(prop, normalized[prop])
|
style.setProperty(prop, normalized[prop])
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
def filter_sheet(sheet, properties):
|
def filter_sheet(sheet, properties=()):
|
||||||
from cssutils.css import CSSRule
|
from cssutils.css import CSSRule
|
||||||
changed = False
|
changed = False
|
||||||
remove = []
|
remove = []
|
||||||
@ -173,27 +175,21 @@ def filter_sheet(sheet, properties):
|
|||||||
return changed
|
return changed
|
||||||
|
|
||||||
|
|
||||||
def filter_css(container, properties, names=()):
|
def transform_css(container, transform_sheet=None, transform_style=None, 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:
|
if not names:
|
||||||
types = OEB_STYLES | OEB_DOCS
|
types = OEB_STYLES | OEB_DOCS
|
||||||
names = []
|
names = []
|
||||||
for name, mt in container.mime_map.iteritems():
|
for name, mt in container.mime_map.iteritems():
|
||||||
if mt in types:
|
if mt in types:
|
||||||
names.append(name)
|
names.append(name)
|
||||||
properties = normalize_filter_css(properties)
|
|
||||||
doc_changed = False
|
doc_changed = False
|
||||||
|
|
||||||
for name in names:
|
for name in names:
|
||||||
mt = container.mime_map[name]
|
mt = container.mime_map[name]
|
||||||
if mt in OEB_STYLES:
|
if mt in OEB_STYLES:
|
||||||
sheet = container.parsed(name)
|
sheet = container.parsed(name)
|
||||||
filtered = filter_sheet(sheet, properties)
|
filtered = transform_sheet(sheet)
|
||||||
if filtered:
|
if filtered:
|
||||||
container.dirty(name)
|
container.dirty(name)
|
||||||
doc_changed = True
|
doc_changed = True
|
||||||
@ -201,9 +197,9 @@ def filter_css(container, properties, names=()):
|
|||||||
root = container.parsed(name)
|
root = container.parsed(name)
|
||||||
changed = False
|
changed = False
|
||||||
for style in root.xpath('//*[local-name()="style"]'):
|
for style in root.xpath('//*[local-name()="style"]'):
|
||||||
if style.text and style.get('type', 'text/css') in {None, '', 'text/css'}:
|
if style.text and (style.get('type') or 'text/css').lower() == 'text/css':
|
||||||
sheet = container.parse_css(style.text)
|
sheet = container.parse_css(style.text)
|
||||||
if filter_sheet(sheet, properties):
|
if transform_sheet(sheet):
|
||||||
changed = True
|
changed = True
|
||||||
style.text = force_unicode(sheet.cssText, 'utf-8')
|
style.text = force_unicode(sheet.cssText, 'utf-8')
|
||||||
pretty_script_or_style(container, style)
|
pretty_script_or_style(container, style)
|
||||||
@ -211,7 +207,7 @@ def filter_css(container, properties, names=()):
|
|||||||
text = elem.get('style', None)
|
text = elem.get('style', None)
|
||||||
if text:
|
if text:
|
||||||
style = container.parse_css(text, is_declaration=True)
|
style = container.parse_css(text, is_declaration=True)
|
||||||
if filter_declaration(style, properties):
|
if transform_style(style):
|
||||||
changed = True
|
changed = True
|
||||||
if style.length == 0:
|
if style.length == 0:
|
||||||
del elem.attrib['style']
|
del elem.attrib['style']
|
||||||
@ -223,6 +219,17 @@ def filter_css(container, properties, names=()):
|
|||||||
|
|
||||||
return doc_changed
|
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 _classes_in_selector(selector, classes):
|
def _classes_in_selector(selector, classes):
|
||||||
for attr in ('selector', 'subselector', 'parsed_tree'):
|
for attr in ('selector', 'subselector', 'parsed_tree'):
|
||||||
s = getattr(selector, attr, None)
|
s = getattr(selector, attr, None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user