![]() |
#16 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Hi
EDIT I tested the script on several books and it's OK. One small thing, I realized that it does not convert negative indents like this one (a rare occurrence). Code:
text-indent: -0.5cm; Code:
unit=re.compile('([^\d.]+:\s*)([\d.]+)(cm)?(;\s*)') Code:
unit=re.compile('(.+:\s*)(-?[\d.]+)(cm)?(;\s*)') |
![]() |
![]() |
![]() |
#17 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Hi
@DiapDealer Your script is still working very well for me on Linux 64 bits with Python 2.7.5.5. (not a surprise) ![]() One friend (using Python 3.2 with Win7) told me he could not run successfully a command line and got this error message: Code:
"can't use a string pattern on a bytes-like object" |
![]() |
![]() |
![]() |
#18 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
I'll take a peek when I get a minute. Your friend had to do something to the script already, however, because the print statements aren't 3.x compliant. I would expect that to be the first complaint of a Python3 interpreter. Last edited by DiapDealer; 06-08-2014 at 04:03 PM. |
|
![]() |
![]() |
![]() |
#19 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Give version 3 a shot (in both python 2.x and 3.x). It works with my testing, but things can get weird with unicode filenames and zipfiles. Also if there's any special characters in the css file that can't be decoded with the utf-8 codec, there could be issues.
|
![]() |
![]() |
![]() |
#20 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,410
Karma: 27757236
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Just FYI, you can write python scripts to process epub and azw3 files using the same infrastructure the calibre editor uses, like this:
Code:
import sys from calibre.ebooks.oeb.polish.container import get_container, OEB_STYLES c = get_container(sys.argv[-1], tweak_mode=True) # now for example to make changes to css files for name, mt in c.mime_map.iteritems(): if mt in OEB_STYLES: raw = c.open(name).read() # do some processing on raw c.open(name, 'wb').write(raw) # Alternately if you wish to work witha parsed representation of the CSS stylesheet = c.parsed(name) # do something with stylesheet c.dirty(name) # indicates the stylesheet has been modified, needed only if you work with the parsed representation # Now save your changes to the epub/azw3 file (you can pass an alternate file path tocommit() to save a copy instead c.commit() calibre-debug myscript.py file.epub Look at polish/container.py for the various API methods of the container object as well as the other files in the polish directory to see how you can achieve various things the editor does. |
![]() |
![]() |
![]() |
#21 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Hi
@kovidgoyal Thank you very much for your interest and your input. @DiapDealer Following more extensive testing, I've found a resilient kind of stylesheet. ![]() So for now, your _v2 and _v3 scripts work only with EPUB which have this beautified kind of stylesheet. I reported yesterday night this finding to pingouinux who wrote a brand new version of his conversion tool. He tells me he tested it with python 2.7.3. and python 3.2.3. I checked this new tool with both a condensed and a beautified stylesheet and it works for both. I've enclosed a zip file containing: - pingouin, the new conversion tool for these two kinds of stylesheets - style.css, a "condensed" kind of style-sheet - newstyle.css, the processed with pingouin style.css - styles1.css, a "beautified" kind of stylesheet - newstyles1.css, the processed with pingouin styles1.css I hope this makes some sense to you and I apologize if I have not been clear. Maybe, integrating this new more efficient tool named "pingouin" and taking into account the comments of kovidgoyal above, there could be possible to build a version of the script with a broader field of use. Last edited by roger64; 06-09-2014 at 01:21 AM. |
![]() |
![]() |
![]() |
#22 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
To be fair, it's the original routine that you posted (which I integrated pretty-much verbatim) that doesn't handle condensed css.
![]() |
![]() |
![]() |
![]() |
#23 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
I agree. I never said otherwise and that's why this part has been rewritten. And I am the first responsible for this because I provided only a "beautified" CSS to begin with.
|
![]() |
![]() |
![]() |
#24 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Note that pingouin's regex in the new conversion routine will miss the last property/value pair in the class (when it doesn't end with a ";").
Code:
body {font-size:1em;margin-bottom:0cm;margin-left:0.5cm;margin-right:0.5cm;margin-top:0cm} Code:
body {font-size:1em;margin-bottom:0.00em;margin-left:1.19em;margin-right:1.19em;margin-top:0cm} If you get a basic conversion algorithm/regex that will cover all contingencies, integrating it into the cm2em script should be a snap. |
![]() |
![]() |
![]() |
#25 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
![]() Last edited by DiapDealer; 06-09-2014 at 08:42 AM. |
|
![]() |
![]() |
![]() |
#26 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
|
![]() |
![]() |
![]() |
#27 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
@DiapDealer
Crossing the fingers, here is pingouin2 which takes into account your previous remark. Code:
#!/usr/bin/python import sys, re conv=2.37106301584 unit=re.compile('(:\s*)(-?[\d.]+)(cm)?(;|})') with open(sys.argv[1],'r') as f, open(sys.argv[2],'w') as g : fic=f.read() while True : k=unit.search(fic) if(k) : g.write(fic[:k.start(1)]) g.write(k.group(1)) val=k.group(2) unite=k.group(3) if unite=='cm' : val="%.2f"%(conv*float(k.group(2))) unite='em' else : unite='' g.write(val+unite) g.write(k.group(4)) fic=fic[k.end(4):] else : g.write(fic) break Last edited by roger64; 06-09-2014 at 03:26 PM. |
![]() |
![]() |
![]() |
#28 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
The only real change I make to the original algorithm is to build the entire new css file before writing it to the new file. That's because I can't write incrementally/append to a file (like the original does) with python's ZipFile module. I've got to write the whole thing at once. |
|
![]() |
![]() |
![]() |
#29 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,625
Karma: 3120635
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Thank you! Testing resumes NOW
![]() |
![]() |
![]() |
![]() |
#30 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,647
Karma: 204624552
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
For those who already have calibre installed and want to try a version that utilizes the method(s) Kovid mentioned, the attached script has the advantage(?) of updating the existing epub, instead of creating a new epub that contains the changes.
** WARNING!! Make sure you have good backups of any epubs you try it with, because it WILL modify the epub you feed it!! ** As Kovid mentioned earlier, run the script like this: calibre-debug cm2em_calibre.py file.epub (assuming, of course, that 'cm2em_calibre.py' and 'file.epub' are in the current directory) |
![]() |
![]() |
![]() |
|
![]() |
||||
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 |