Quantcast
Channel: MobileRead Forums
Viewing all articles
Browse latest Browse all 46898

[Plugin] cssRemoveUnusedSelectors

$
0
0
Hi to all,

this edit plugin is a somewhat more powerful version of the builtin Sigil function "Delete Unused Stylesheet Classes...". The plugin is not limited to classes but can deal with (almost) all kinds of style rule's selectors.

It uses cssutils to parse the stylesheets and lxml/cssselect to check if css selectors match at least one element in xhtml files of that epub.

(Cssutils, lxml and cssselect are all bundled in Sigil installers).

All css selectors without corresponding elements in xhtml files are proposed to the user for deletion.

If css parser encounters errors, it raises a warning and the user can choose to proceed or to stop the plugin. In any case, for safety, the specific stylesheets that caused the errors will be left untouched (cssutils implements many but not all of the CSS3 features, e.g. @media rules nested inside other @media rules).

To make the survey in xhtml files, css selectors are converted in XPath by lxml/cssselect. Some of the selectors (those who contain ":hover", ":active", ":focus", ":target", ":visited") will never match anything, so the plugin lets them be. Same thing for selectors that are not yet implemented (*:first-of-type, *:last-of-type, *:nth-of-type, *:nth-last-of-type, *:only-of-type - they work only if an element type is specified). For reference: https://pythonhosted.org/cssselect/#supported-selectors.

One feature I'd like to implement in future releases is a GUI to let the user choose her preferred output options for the "restyled" stylesheets (indentation, semicolon after last value in a rule etc.). For now, who is capable of, can tweak those values overwriting the function setCssOutputPrefs in plugin.py.

At the moment I tested this plugin successfully with Sigil v0.9.5 on Mac OS X 10.11 and on Ubuntu 15.04.

Current version: 0.1.0.

Requires Python 3.4 or later (the interpreter bundled in Sigil will work fine).

Attached Files
File Type: zip cssRemoveUnusedSelectors.zip (6.0 KB)

Viewing all articles
Browse latest Browse all 46898

Trending Articles