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 05-18-2016, 01:40 PM   #1
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
[Plugin] RemoveInLineStyles Plugin

Hi All,

This "edit" plugin will search for and remove all inline styles (style attributes in xhtml tags) and replace them with the equivalent class stored in an external stylesheet.

Updated: May 31, 2021
Current Version: "0.3.1"

Requires: Python 3.4 or Later and Sigil 1.0 or later

See the attached: RemoveInLineStyles_v031.zip

For Sigil Versions before Sigil-1.0
please see the attached older version RemoveInLineStyles_v021.zip

How it works:
It will parse all existing stylesheets using cssutils (part of the official internal Python 3 requirements included with Sigil) to create a list of all stylesheet file names and class names currently being used to prevent name collisions.

Then it will parse each and every xhtml file, examining each tag and if a style attribute is present, it will remove it, look in the current list of newly created rules to see if an exact match exists and if not add a new class name with that new rule. The correct class attribute is then added to replace the removed style attribute. Note that for safety, inline styles used inside svg or mathml elements/regions are not touched.

If inline styles are found, an external link to the newly created external stylesheet will be inserted into the head element just before the closing head tag.

No changes will be made if no inline styles are actually found in the xhtml file and no new css stylesheet will be created unless at least one inline style is found someplace in the ebook.

This code should be safe to use if no external stylesheets already exist or if there is no mixing of existing css rules and inline styles for the exact same element.

Warnings
1. Please note, any styles inside the style tag in the head tag will be ignored.
2. Should not be used on code that mixes css rules with inline styles at the same time as the authors intent with the inline style may be to overrule anything previous because inline styles have higher specificity.


Problems / Bug Reports / Suggestions for Improvements
If you do run into problems with this plugin, please report them here including a small sample of the problem xhtml/css code, along with information on your platform, Sigil version, etc. And I will try to quickly fix any bugs or issues you run into.

Hope this helps!

KevinH
Attached Files
File Type: zip RemoveInLineStyles_v021.zip (2.7 KB, 1783 views)
File Type: zip RemoveInLineStyles_v031.zip (2.8 KB, 260 views)

Last edited by KevinH; 05-31-2021 at 11:07 PM.
KevinH is online now   Reply With Quote
Old 05-18-2016, 01:47 PM   #2
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: 24,682
Karma: 169420718
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Oh, this will come in extremely handy (speaking for myself, anyway). Thanks!
DiapDealer is offline   Reply With Quote
Advert
Old 05-19-2016, 02:25 PM   #3
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
Update to version v021 in first post of this thread. RemoveInLineStyle will now simply ignore inline styles used inside svg elements/regions and math elements/regions for increased safety.

Kevin
KevinH is online now   Reply With Quote
Old 05-21-2016, 12:32 AM   #4
odamizu
just an egg
odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.odamizu ought to be getting tired of karma fortunes by now.
 
odamizu's Avatar
 
Posts: 1,386
Karma: 2688282
Join Date: Mar 2015
Device: Kindle Oasis1 & Voyage, iOS
Quote:
Originally Posted by KevinH View Post
This "edit" plugin will search for and remove all inline styles (style attributes in xhtml tags) and replace them with the equivalent class stored in an external stylesheet.
odamizu is offline   Reply With Quote
Old 05-21-2016, 12:53 AM   #5
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 40,020
Karma: 17765146
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
@KevinH: Perhaps you should add a note that this plugin can destroy your styles, since there is no way to robustly convert an inline style to a class based style. The CSS selector specificity will always be wrong. For example

Code:
#test {
color: red
}


<span id="test" style="color:green">test</test>
will render green before this plugin runs and red after it runs.

This is because in the CSS cascading spec the specificity of an inline style declaration is always higher than the specificity of any selector based rule.
kovidgoyal is offline   Reply With Quote
Advert
Old 05-21-2016, 01:15 AM   #6
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,279
Karma: 19618507
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by kovidgoyal View Post
Perhaps you should add a note that this plugin can destroy your styles, since there is no way to robustly convert an inline style to a class based style. The CSS selector specificity will always be wrong.
You're certainly raising a valid point, however, the ePubs for which this plugin was primarily intended are not very likely to contain id selectors and many web designers don't recommend using id selectors.
Doitsu is offline   Reply With Quote
Old 05-21-2016, 02:03 AM   #7
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 40,020
Karma: 17765146
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
It has nothing to do with id selectors, here's an example without id selectors

Code:
p span.test {
color: red
}

<p>
<span class="test" style="color:green">test</span>
</p>
The point is that there is no way to map the specificity of an inline style to a selector based style robustly. Which means that the CSS cascade before and after the plugin runs is different.
kovidgoyal is offline   Reply With Quote
Old 05-21-2016, 09:22 AM   #8
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
Good point! In both cases there are existing style sheets and are mixing in inline styles. Even if I add the new general class to a style sheet so it is found second, it won't have higher specificity.

I will add that to the warnings (now added).

I will modify the code to allow the user to abort if inline styles are used with existing style sheets already included. Or I guess I could map styles sheet classes to xhtml lines and if a rule already exists for that element, leave it alone.

Thanks for the heads up!

Last edited by KevinH; 05-21-2016 at 11:00 AM.
KevinH is online now   Reply With Quote
Old 05-21-2016, 10:50 AM   #9
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
Kovid,

As I walk the xhtml, if I can detect if a rule already exists for an element that *also* specifies an inline style, could I not add an "id" attribute and then remove the inline style and add it as a new rule to the new stylesheet but with that id selector. That should match the highest specificity rule but be come after the original definition. That should work for both your examples.

Or are there other cases that would still make this approach unsound.

KevinH
KevinH is online now   Reply With Quote
Old 05-21-2016, 02:11 PM   #10
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 40,020
Karma: 17765146
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
There is no easy way to solve this (I considered implementing this for the calibre editor a long time ago, and decided not to, because it cannot easily be made robust) -- if you use id selectors, your selector can still be trumped by another id selector that is more specific, for example, if it also has a class or a descendant selector and so on (say #xxx.yyy or p#xxx or p > span#xxx)

See https://css-tricks.com/specifics-on-...le-header-id-0 for a good overview.

What you can do do is go over all rules in all stylesheets, find the highest specificity selector that applies to the element in question, then if:

1) The selector has an id, simply merge the rules from the inline style block into that rule, taking care, in particular of shorthand properties
2) The selector does not have an id -- create a new id based selector by giving the element a random id if it does not already have one

You can refine this process by adding a few more if statements to (2) so that you use class based selectors more often.

And note that even this process is not quite bulletproof there are a few edge cases I have not mentioned -- left as an exercise to the reader.

Last edited by kovidgoyal; 05-21-2016 at 02:15 PM.
kovidgoyal is offline   Reply With Quote
Old 05-21-2016, 02:47 PM   #11
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
I think the only safe thing is to check if any existing class / selector match at all for an element that also has an inline style, we simply leave the inline style untouched.
KevinH is online now   Reply With Quote
Old 11-15-2020, 02:23 PM   #12
FDPuthuff
Avid Learner
FDPuthuff began at the beginning.
 
Posts: 39
Karma: 10
Join Date: Sep 2020
Location: Charleston, SC
Device: Kindle Fire, iPad
Cool

Thank you sir.
I'll play with this and see how it can help me.
FDPuthuff is offline   Reply With Quote
Old 11-19-2020, 04:54 AM   #13
slowsmile
Witchman
slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.
 
Posts: 553
Karma: 788808
Join Date: May 2013
Location: Philippines
Device: Android S5
@KevinH... A way out for you might perhaps be to just rename your new "inline classes" to your own convention e.g. sgc1, sgc2 etc while at the same time moving them to the CSS. I don't know if this is useful to you but you can certainly do this using the Tidy dll(I've used the Tidy dll like this in my HTML2Epub plugin) and just moved the new classes from the top of the html file to the CSS using my own code. I think you have to set the 'css-prefix' Tidy flag for this feature to work. If you created your CSS classes this way there would hopefully be no style name clashes. The only downside perhaps is that the Tidy dll is quite a large file.

Last edited by slowsmile; 11-19-2020 at 05:06 AM.
slowsmile is offline   Reply With Quote
Old 05-13-2021, 06:25 AM   #14
Sarmat89
Addict
Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.Sarmat89 ought to be getting tired of karma fortunes by now.
 
Posts: 311
Karma: 2145408
Join Date: Nov 2015
Device: none
The plugin is not quite compatible with the newer Sigil: it places the resulting file in the root directory, but inserts a "../Styles/" path reference to the HTML files.
Sarmat89 is offline   Reply With Quote
Old 05-13-2021, 10:25 AM   #15
KevinH
Sigil Developer
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: 5,857
Karma: 3571874
Join Date: Nov 2009
Device: many
Thanks! I forgot to update it. I will do that now.
KevinH is online now   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 470 10-26-2021 09:15 AM
[FileType Plugin] YVES Bible Plugin ClashTheBunny Plugins 25 10-13-2019 12:32 AM
Plugin not customizable: Plugin: HTML Output does not need customization flyingfoxlee Conversion 2 02-24-2012 03:24 AM
[GUI Plugin] Plugin Updater **Deprecated** kiwidude Plugins 159 06-19-2011 01:27 PM
New Plugin Type Idea: Library Plugin cgranade Plugins 3 09-15-2010 01:11 PM


All times are GMT -4. The time now is 12:38 AM.


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