06-05-2016, 05:02 PM | #1 |
Enthusiast
Posts: 38
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. [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.). 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 Code:
"quiet": true 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 } Current version: 0.3.4. Requires Python 3.4 or later (the interpreter bundled in Sigil will work fine). Changes: Spoiler:
Last edited by wrCisco; 09-22-2024 at 11:00 AM. Reason: Release 0.3.4 |
06-05-2016, 05:56 PM | #2 |
Sigil Developer
Posts: 8,110
Karma: 5450184
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,968
Karma: 198535232
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,968
Karma: 198535232
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,968
Karma: 198535232
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: 38
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: 8,110
Karma: 5450184
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: 38
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: 38
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,968
Karma: 198535232
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: 38
Karma: 467802
Join Date: Apr 2016
Device: none
|
Quote:
Quote:
|
||
06-18-2016, 02:40 PM | #14 |
Enthusiast
Posts: 38
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 |
[GUI Plugin] KindleUnpack - The Plugin | DiapDealer | Plugins | 494 | 05-18-2024 02:34 AM |
[FileType Plugin] YVES Bible Plugin | ClashTheBunny | Plugins | 27 | 01-16-2023 01:25 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 |