MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   [Plugin] cssRemoveUnusedSelectors (https://www.mobileread.com/forums/showthread.php?t=274926)

wrCisco 06-05-2016 06:02 PM

[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:
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

KevinH 06-05-2016 06:56 PM

Wow, very nice work indeed!
Thank you for your work improving Sigil.

KevinH

DiapDealer 06-05-2016 06:58 PM

Very cool. Thanks for the contribution! I was hoping someone would see the potential of the cssselect module that is included with Sigil. :)

roger64 06-06-2016 02:31 AM

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,{
text-indent: 0;
}

The plugin stopped parsing this style-sheet because of this code (see screenshot). It's very nice to know precisely what happens instead of getting a terse and cryptic "failed".

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.

DiapDealer 06-06-2016 06:49 AM

Quote:

Originally Posted by roger64 (Post 3330348)
Code:

.Title, .Heading, .Subtitle,{
text-indent: 0;
}

The plugin stopped parsing this style-sheet because of this code (see screenshot). It's very nice to know precisely what happens instead of getting a terse and cryptic "failed".

I would like to know if the above code is wrong and if I should break it in three parts.

More than likely it's just a matter of the extraneous comma after the .Subtitle selector.

roger64 06-06-2016 07:18 AM

: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.

DiapDealer 06-06-2016 08:14 AM

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. :)

wrCisco 06-06-2016 05:40 PM

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?

KevinH 06-06-2016 08:24 PM

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.

wrCisco 06-07-2016 03:03 PM

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.

wrCisco 06-07-2016 07:18 PM

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.

DiapDealer 06-12-2016 11:46 AM

Quote:

Originally Posted by wrCisco (Post 3330203)
Current version: 0.1.1.

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. :)

wrCisco 06-12-2016 06:50 PM

Quote:

Originally Posted by DiapDealer (Post 3333374)
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.

You're right, I forgot to update plugin.xml. I just uploaded version 0.1.2 of the plugin with minor improvements, now version number should be correct.

Quote:

Using this plugin pretty regularly, by the way. :)
Glad to hear that. :)

wrCisco 06-18-2016 03:40 PM

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

Auramazda 06-27-2016 08:06 PM

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.