06-10-2014, 02:53 AM | #31 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Thank you very much for this new try.
|
06-12-2014, 03:50 AM | #32 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Hi
After various tries, it seems to work pretty well for longhand CSS stylesheets... but for shorthand. In this latter case, the script just ignores the shorthand properties and lefts it untouched. Adding shorthand to the script conversion capabilities is probably doable, but it would maybe begin to stretch dangerously the regex... Before coming back - shameful - to pingouinux for this, I thought about another possible solution: I remarked in Calibre/Look and feel that we can use ebook-convert to expand css with the following option: Code:
--expand-css Here is what we get when launching --expand-css alone: After that, the script is working quite well. Spoiler:
At that point also, also, we can wonder if it would not be better to integrate the script as a new option within ebook-convert... Last edited by roger64; 06-12-2014 at 06:22 AM. Reason: integrate |
06-12-2014, 07:28 AM | #33 | |||
Grand Sorcerer
Posts: 27,902
Karma: 198500000
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
Quote:
Quote:
|
|||
06-12-2014, 09:03 AM | #34 |
creator of calibre
Posts: 44,336
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
@DiapDealer: While it is certainly true that using code from the conversion pipeline is not always possible standalone, in this case you can do it with the container pretty easily, like this (insert the following into my previous example:
Code:
# assume we have a parsed stylesheet as I showed before for rule in sheet.cssRules: if rule.type == rule.STYLE_RULE: for property in rule.style.keys(): val = property.propertyValue for x in val: if x.type == 'DIMENSION' and x.unit == 'cm': x.cssText = convert(x.value ) # Here convert should convert a float into a string in em units container.dirty(sheet_name) |
06-12-2014, 09:14 AM | #35 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
I understand.
Using the full program ebook-convert when we just wish to expand the CSS is like using a hammer to kill a fly. We are miles away from your previous "micro-surgery" operation. If you allow me to do it, I'd like to address Kovid Goyal about it. He alone, knows which part of the Calibre code is used specifically to expand a CSS style-sheet, and, who knows?, he would be so kind as to point to it? It can't be that huge. Once a stylesheet is taken out of the water, it seems a not so different job to expand it or to convert some of its values. and both could be done successively. On the other hand, he could see a benefit to this as he could add the cm2em conversion feature to ebook-convert, which strangely, does not have it yet. EDIT: when posting, I realized this was nearly done!! Thank you Kovid Goyal !! Last edited by roger64; 06-12-2014 at 09:19 AM. Reason: posted without reading the previous message |
06-12-2014, 09:18 AM | #36 | |
Grand Sorcerer
Posts: 27,902
Karma: 198500000
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
I definitely need to explore what I can do with this powerful (and highly bash scriptable) calibre plug-out functionality. Thanks so much for the pointers to the relevant code! |
|
06-12-2014, 09:29 AM | #37 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
@DiapDealer
Thanks for trying! |
06-12-2014, 07:34 PM | #38 |
Grand Sorcerer
Posts: 27,902
Karma: 198500000
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
So this is what I have.
This is not a stand-alone python script. You must have calibre installed. You launch the script like so: Code:
calibre-debug cm2em_calibre.py file.epub It's not going to work with any rules inside of @media queries if your css gets that complicated, but it should catch everything else. I included a sample batch file and a bash script if anyone is interested in launching this in a more permanent way. REMEMBER! THIS SCRIPT WILL MODIFY THE EPUB YOU STARTED WITH ... MAKE SURE YOU HAVE BACKUPS BEFORE USING. Last edited by DiapDealer; 06-13-2014 at 12:55 PM. |
06-13-2014, 01:50 AM | #39 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
@DiapDealer
This was amazingly fast. I hurried to test it on three kinds of stylesheet, condensed, beautified and shorthand, and it worked beautifully for all. The code is very elegant and economical. It's a brand new and very effective solution. I could not dream a better one! Congratulations to you and Kovid Goyal and many thanks for that. 1. - For the second script. Forgive me, I am very unfamiliar with it. I understand it gives to calibre-debug the path to the main script (cm2em_calibre.py). I tried to make it work using a right-click, or from the command-line and failed. I will need some additional and basic explanations. If we use this additional script and if we do not look at the terminal window, could it be possible to get some kind of feedback to know when the epub has been processed? (a text file, a window message, whatever) 2. - An innocent question: Suppose we have a folder with some epubs. Could it be possible to batch treat them using *.epub? I tried it but it only processed the first one and stopped. Last edited by roger64; 06-13-2014 at 04:39 AM. |
06-13-2014, 07:02 AM | #40 | |||
Grand Sorcerer
Posts: 27,902
Karma: 198500000
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
Quote:
1) I put the script (cm2em.sh) in /usr/local/bin. 2) rename it to simply cm2em. 3) make it executable. 4) open it in an editor and change SCRIPT_DIR to wherever my cm2em_calibre.py script will be living from now on. After that bit of setup, I can open a terminal and cd to whatever directory the epub I want to work on resides in and simply type "cm2em file.epub" (without the quotes). Quote:
Code:
for i in *.epub; do cm2em "$i"; done Last edited by DiapDealer; 06-13-2014 at 12:55 PM. |
|||
06-13-2014, 09:08 AM | #41 |
Wizard
Posts: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Hi
Thanks again for these crystal clear - and for me necessary - explanations. We can now enjoy a powerful new tool in a very handy way. EDIT I did some tries. Using the cm2em script is working well only when the name of the EPUB has no white space. If there is a white space, or a -, on the terminal, (the first one is replaced by antislash) I observe a failure of the main (cm2em_calibre.py) script. When I was launching the main script directly from the command line, this did not happen. I could use epub file names with any kind of characters, including white spaces and -. Last edited by roger64; 06-13-2014 at 10:38 AM. |
06-13-2014, 01:00 PM | #42 | |
Grand Sorcerer
Posts: 27,902
Karma: 198500000
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
Anyway, I updated the bash script so it won't barf on epub filenames with white-space (as long as you quote those filenames in the terminal command), and modified the command to process entire folders to do the same. I PM'ed you the details. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Question: Find/Replace in css stylesheet | ElMiko | Sigil | 12 | 12-25-2011 06:29 AM |
ePub margins : @page vs stylesheet.css | Agama | Calibre | 1 | 08-16-2010 04:37 PM |
How to maintain a global CSS stylesheet | amoroso | Sigil | 7 | 07-18-2010 08:37 PM |
EPub conversion stylesheet.css problem | nycaleksey | Calibre | 1 | 03-25-2010 11:42 PM |
Where is the stylesheet.css? | roger64 | Sigil | 4 | 03-23-2010 02:12 PM |