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.
[
EDIT: Since version 1.5 Sigil has replaced the old "Delete Unused Stylesheet Classes" with "Delete Unused Stylesheet Selectors", so you'd probably be okay just by using that. You might want to use this plugin if:
- You're stuck with an old version of Sigil.
- You're using the plugin in an Automated List and want to take advantage of the new quiet mode to speed things up.
- You like the plugin css styling preferences more than the ones provided by Sigil.
The plugin and the builtin Sigil function use different css parsers, so there might be some edge case odd shaped css selector that's handled differently by the two, but these cases should be very rare. ]
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.).
Version 0.3.5 is the last version that uses the Tkinter graphical toolkit for the graphical interface: since version 0.4.0 the plugin uses PyQt5/PySide6 (older versions of Sigil use PyQt5, newer versions use PySide6. The compatibility layer is offered by the plugin_utils module by DiapDealer, embedded in the plugin).
You should be able to use the latest version of the plugin with any reasonable recent version of Sigil. If you encounter problems, versions 0.3.5 and 0.4.0 are functionally equivalent.
Quiet mode
Since version 0.3.4 you can skip the graphical interface by manually adding the quiet option in the preferences file. In quiet mode, the plugin will just delete all unused selectors without letting you choose which ones to delete and which ones to keep, and it will just report possible errors.
The preferences file should be $SIGIL_PREFS/plugin_prefs/cssRemoveUnusedSelectors/cssRemoveUnusedSelectors.json (you might need to launch the plugin at least once in order to find it). The $SIGIL_PREFS directory is system dependent, but you should be able to open it from the Manage Plugins window in Sigil.
In order to activate the Quiet mode, you need to add the line
in the preferences file, together with the other preferences.
An example of the file content is:
Code:
{
"parseAllXMLFiles": true,
"indent": " ",
"indentClosingBrace": true,
"keepEmptyRules": true,
"omitLastSemicolon": true,
"omitLeadingZero": true,
"formatUnknownAtRules": false,
"linesAfterRules": "\n",
"quiet": true
}
At the moment I tested the last version of the plugin successfully with Sigil 2.6 on Windows 11 and Sigil 2.0 on Ubuntu 24.04.
Current version: 0.4.0.
Requires Python 3.6 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
v0.3.3- Improve efficiency by reducing the number of passages over xml/xhtml files performed by the parser.
v0.3.4- Add quiet mode.
v0.3.5- Fix a typo and a warning.
- Last version that uses the Tkinter graphical toolkit.
v0.4.0- First version to use PyQt5/PySide6 for the graphical interface.