06-05-2016, 05:02 PM | #1 |
Enthusiast
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:
Last edited by wrCisco; 08-22-2020 at 12:42 PM. Reason: Release 0.3.2 |
06-05-2016, 05:56 PM | #2 |
Sigil Developer
Posts: 7,675
Karma: 5433388
Join Date: Nov 2009
Device: many
|
Wow, very nice work indeed!
Thank you for your work improving Sigil. KevinH |
Advert | |
|
06-05-2016, 05:58 PM | #3 |
Grand Sorcerer
Posts: 27,572
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Very cool. Thanks for the contribution! I was hoping someone would see the potential of the cssselect module that is included with Sigil.
|
06-06-2016, 01:31 AM | #4 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Thanks you for your nice plugin.
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,{ text-indent: 0; } 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. Last edited by roger64; 06-06-2016 at 01:49 AM. |
06-06-2016, 05:49 AM | #5 | |
Grand Sorcerer
Posts: 27,572
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
|
|
Advert | |
|
06-06-2016, 06:18 AM | #6 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Good to know. I remove it immediately. It had never been signalled by Epubcheck or CSSLint but indeed it was the reason. Sorry for this. Last edited by roger64; 06-06-2016 at 06:27 AM. Reason: sorry |
06-06-2016, 07:14 AM | #7 |
Grand Sorcerer
Posts: 27,572
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
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. |
06-06-2016, 04:40 PM | #8 |
Enthusiast
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
|
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? |
06-06-2016, 07:24 PM | #9 |
Sigil Developer
Posts: 7,675
Karma: 5433388
Join Date: Nov 2009
Device: many
|
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. |
06-07-2016, 02:03 PM | #10 |
Enthusiast
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
|
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.
|
06-07-2016, 06:18 PM | #11 |
Enthusiast
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
|
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. |
06-12-2016, 10:46 AM | #12 |
Grand Sorcerer
Posts: 27,572
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Just a heads-up that the latest download still indicates that version 0.1.0 is being installed. Not sure if the zip contents are actually new (I don't have the original to diff), or if the version attribute of the plugin.xml file just didn't get advanced to 0.1.1.
Using this plugin pretty regularly, by the way. |
06-12-2016, 05:50 PM | #13 | ||
Enthusiast
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
|
Quote:
Quote:
|
||
06-18-2016, 02:40 PM | #14 |
Enthusiast
Posts: 34
Karma: 467802
Join Date: Apr 2016
Device: none
|
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 Last edited by wrCisco; 06-18-2016 at 04:29 PM. |
06-27-2016, 07:06 PM | #15 |
Member
Posts: 16
Karma: 1422
Join Date: Mar 2016
Device: none
|
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[FileType Plugin] YVES Bible Plugin | ClashTheBunny | Plugins | 27 | 01-16-2023 01:25 AM |
[GUI Plugin] KindleUnpack - The Plugin | DiapDealer | Plugins | 492 | 10-25-2022 08:13 AM |
Plugin not customizable: Plugin: HTML Output does not need customization | flyingfoxlee | Conversion | 2 | 02-24-2012 02:24 AM |
[GUI Plugin] Plugin Updater **Deprecated** | kiwidude | Plugins | 159 | 06-19-2011 12:27 PM |
New Plugin Type Idea: Library Plugin | cgranade | Plugins | 3 | 09-15-2010 12:11 PM |