![]() |
[Plugin] cssRemoveUnusedSelectors
3 Attachment(s)
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:
|
Wow, very nice work indeed!
Thank you for your work improving Sigil. KevinH |
Very cool. Thanks for the contribution! I was hoping someone would see the potential of the cssselect module that is included with Sigil. :)
|
2 Attachment(s)
Thanks you for your nice plugin. :2thumbsup
I am used to append a second style-sheet to my books. Later, once I finished editing the book, I prune it with the Calibre editor. Among the CSS lines you can find these ones: Code:
.Title, .Heading, .Subtitle,{I would like to know if the above code is wrong and if I should break it in three parts. The rationale behind this code is that the converter writes these above-mentioned styles in the first style-sheet without any text-indent, and it spoils the display. Without these offending lines, the plugin seems to work smoothly (second screenshot) and offers a very customizable choice, which is always a plus. |
Quote:
|
:thanks:
Good to know. I remove it immediately. It had never been signalled by Epubcheck or CSSLint but indeed it was the reason. :o Sorry for this. |
NOTE: I'm not saying the trailing comma isn't technically valid css (it could be, for all I know). It just seemed a likely point of failure for an external parsing algorithm.
No apology necessary. :) |
Thanks to all for appreciation and testing!
Yes, a trailing comma seems to be a syntax error (and in my Sigil's Preview and in Calibre's ebook-viewer the associated rule doesn't work). Good to know that the problem is solved and the plugin works as expected. Nonetheless, I'd like to lessen some rigidities of the css parser, but cssutils is a convoluted package, so it's not a simple matter. One issue, for example, is that it doesn't recognize Amazon's media queries for kindlegen ("@media amzn-mobi" and "@media amzn-kf8"): the accepted values are those of MEDIA_TYPE list in cssutils/stylesheets/mediaquery.py. I added the two values directly there and everything seems to work fine, but I don't know how to integrate that little change in my plugin without subclassing a large number of cssutils's classes and copying probably hundreds of lines of code. KevinH and DiapDealer, is there a chance to have a sigil-cssutils (as you already did with sigil-bs4) in some future release of Sigil? |
Please try up streaming the change first.
If they aren't interested and if cssutils is pure Python, we can include it. Sigil itself needs a good css parser and if a C or C++ one can not be found, we can use the embedded Python interface with our own cssutils version. |
Ok, but first I'd like to better understand the package functioning. So, as soon as I'll find some spare time to further study it, I'll try to put together a decent proposal.
|
I just uploaded a new version of the plugin with support for Amazon's proprietary values for @media rules. It needed a hack of 2 (literally) lines of code, I was stopped by an awkward amnesia on how class attributes work in Python...
In the meantime I opened an issue in cssutils's issue tracker. |
Quote:
Using this plugin pretty regularly, by the way. :) |
Quote:
Quote:
|
Just updated the plugin to version 0.2.0 with a graphical interface to set and save preferences on css formatting options.
EDIT: quick bug fix update to v0.2.1 |
Hi, I tried the plug-in on a css with too many stile but I get a error message, I use win 7 and Sigil 0.9.6, the message is:
Stato: failed Traceback (most recent call last): File "C:\Program Files\Sigil\plugin_launchers\python\launcher.py", line 135, in launch self.exitcode = target_script.run(container) File "C:\Users\IS\AppData\Local\sigil-ebook\sigil\plugins\cssRemoveUnusedSelectors\plugi n.py", line 501, in run css_to_change[sel_data[0]] = sel_data[4].cssText File "site-packages\cssutils\css\cssstylesheet.py", line 124, in _getCssText File "C:\Users\IS\AppData\Local\sigil-ebook\sigil\plugins\cssRemoveUnusedSelectors\custo mCssutils.py", line 28, in do_CSSStyleSheet out.append(cssText+self.prefs.blankLinesAfterRules ) TypeError: Can't convert 'bytes' object to str implicitly Error: Can't convert 'bytes' object to str implicitly |
| All times are GMT -4. The time now is 08:39 PM. |
Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.