Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 06-05-2016, 05:02 PM   #1
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
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://pythonhosted.org/cssselect/#supported-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 this plugin successfully with Sigil v0.9.7 on Ubuntu 16.04 and Windows 7 and 10.

Current version: 0.3.0.

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.
Attached Files
File Type: zip cssRemoveUnusedSelectors_v0.2.3.zip (9.3 KB, 174 views)
File Type: zip cssRemoveUnusedSelectors_v0.3.0.zip (20.7 KB, 307 views)

Last edited by wrCisco; 12-05-2016 at 09:34 AM. Reason: Release 0.3.0
wrCisco is offline   Reply With Quote
Advert
Old 06-05-2016, 05:56 PM   #2
KevinH
Wizard
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 2,416
Karma: 769772
Join Date: Nov 2009
Device: many
Wow, very nice work indeed!
Thank you for your work improving Sigil.

KevinH
KevinH is offline   Reply With Quote
Old 06-05-2016, 05:58 PM   #3
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 16,030
Karma: 83624236
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.
DiapDealer is offline   Reply With Quote
Old 06-06-2016, 01:31 AM   #4
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 2,075
Karma: 2150557
Join Date: Jan 2009
Device: Kobo Glo - 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;
}
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.
Attached Thumbnails
Click image for larger version

Name:	Sélection_073.png
Views:	239
Size:	14.0 KB
ID:	149250   Click image for larger version

Name:	css.png
Views:	265
Size:	18.5 KB
ID:	149251  

Last edited by roger64; 06-06-2016 at 01:49 AM.
roger64 is offline   Reply With Quote
Old 06-06-2016, 05:49 AM   #5
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 16,030
Karma: 83624236
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by roger64 View Post
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.
DiapDealer is offline   Reply With Quote
Advert
Old 06-06-2016, 06:18 AM   #6
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 2,075
Karma: 2150557
Join Date: Jan 2009
Device: Kobo Glo - 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
roger64 is offline   Reply With Quote
Old 06-06-2016, 07:14 AM   #7
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 16,030
Karma: 83624236
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.
DiapDealer is offline   Reply With Quote
Old 06-06-2016, 04:40 PM   #8
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
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?
wrCisco is offline   Reply With Quote
Old 06-06-2016, 07:24 PM   #9
KevinH
Wizard
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 2,416
Karma: 769772
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.
KevinH is offline   Reply With Quote
Old 06-07-2016, 02:03 PM   #10
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
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.
wrCisco is offline   Reply With Quote
Old 06-07-2016, 06:18 PM   #11
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
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.
wrCisco is offline   Reply With Quote
Old 06-12-2016, 10:46 AM   #12
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 16,030
Karma: 83624236
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by wrCisco View Post
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.
DiapDealer is offline   Reply With Quote
Old 06-12-2016, 05:50 PM   #13
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
Join Date: Apr 2016
Device: none
Quote:
Originally Posted by DiapDealer View Post
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 is offline   Reply With Quote
Old 06-18-2016, 02:40 PM   #14
wrCisco
Member
wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.wrCisco ought to be getting tired of karma fortunes by now.
 
Posts: 13
Karma: 282196
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.
wrCisco is offline   Reply With Quote
Old 06-27-2016, 07:06 PM   #15
Auramazda
Member
Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.Auramazda is no ebook tyro.
 
Posts: 13
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
Auramazda is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[GUI Plugin] KindleUnpack - The Plugin DiapDealer Plugins 402 03-05-2017 10:31 AM
[FileType Plugin] YVES Bible Plugin ClashTheBunny Plugins 23 08-16-2016 06:53 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


All times are GMT -4. The time now is 08:01 PM.


MobileRead.com is a privately owned, operated and funded community.