View Single Post
Old 06-05-2016, 05:02 PM   #1
wrCisco
Enthusiast
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
[Plugin] cssRemoveUnusedSelectors

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 to 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://cssselect.readthedocs.io/en/...rted-selectors.

Since version 0.2.0 there is a graphical interface to set preferences about output options for the "restyled" stylesheets (indentation, semicolon after last value in a rule etc.).

At the moment I tested the last version of the plugin successfully with Sigil v1.3.0 on Windows 10, Linux Mint 19.3 and Manjaro.

Current version: 0.3.2.

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

Changes:
Spoiler:
v0.1.0
- Initial release
v0.1.1
- Added support for amzn-mobi and amzn-kf8 in stylesheets @media rules
v0.1.2
- Added warning for unknown @rules in stylesheets
- Minor adjustments
v0.2.0
- Added a GUI to set and save preferences on css formatting output
- Subclassed cssutils's serializer to increase user's choices
v0.2.1
- Bug fix
v0.2.2
- Fix bug in default preference "blankLinesAfterRules"
- Preferences dialog window is now modal.
v0.2.3
- Added compatibility with Sigil v0.9.7, which reads css and js files as text and not as binary anymore.
v0.3.0
- Added option to parse all xml files in epub.
- Better handling of default (unprefixed) namespaces in css and some particular prefix for selectors ('|' - no namespace - and '*|' - every namespace).
- Added a "Select/Unselect all" button in selector's list dialog.
- Switch MIT license with GPLv3.
- Minor improvements in style and speed.
v0.3.1
- Add compatibility with Sigil 0.9.18 and the new css-parser module which replaces cssutils.
- Minor GUI improvements: the list of selectors is now scrollable with the mousewheel, selectors longer than the width of the window wrap around dynamically, buttons are bound with the <Return> key.
- If there is an XML syntax error in one of the files, the plugin shows an error message and exits (while before it would have simply crashed).
v0.3.2
- Add plugin icon for Sigil and the window manager
Attached Files
File Type: zip cssRemoveUnusedSelectors_v0.3.1.zip (21.7 KB, 992 views)
File Type: zip cssRemoveUnusedSelectors_v0.3.2.zip (23.8 KB, 2089 views)

Last edited by wrCisco; 08-22-2020 at 12:42 PM. Reason: Release 0.3.2
wrCisco is offline   Reply With Quote