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

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 06-06-2014, 05:27 PM   #16
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
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;
For this reason, pingouinux suggests to change this line:
Code:
unit=re.compile('([^\d.]+:\s*)([\d.]+)(cm)?(;\s*)')
with this line (which also simplifies the code)

Code:
unit=re.compile('(.+:\s*)(-?[\d.]+)(cm)?(;\s*)')
roger64 is online now   Reply With Quote
Old 06-08-2014, 04:47 PM   #17
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
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"
Do you think the script is limited to Python 2.x users? Which version of Python did you use to write your script?
roger64 is online now   Reply With Quote
 
Advertisement
Old 06-08-2014, 04:58 PM   #18
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: 9,530
Karma: 43837842
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by roger64 View Post
Do you think the script is limited to Python 2.x users? Which version of Python did you use to write your script?
I certainly didn't have Python 3 in mind when I wrote it, but it's simple enough that I can't imagine it couldn't be tweaked to run successfully in both 2.x and 3.x environments.

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 05:03 PM.
DiapDealer is online now   Reply With Quote
Old 06-08-2014, 07:43 PM   #19
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: 9,530
Karma: 43837842
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by roger64 View Post
Do you think the script is limited to Python 2.x users? Which version of Python did you use to write your script?
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.
Attached Files
File Type: txt cm2em_v3.txt (2.2 KB, 17 views)
DiapDealer is online now   Reply With Quote
Old 06-09-2014, 01:26 AM   #20
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: 26,433
Karma: 5383257
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()
And you run this like this

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.
kovidgoyal is offline   Reply With Quote
Old 06-09-2014, 01:59 AM   #21
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
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. It's when the CSS is written in a condensed form, not in a "beautified" form with a clean vertical layout. (more of it lowerdown).

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.
Attached Files
File Type: zip diap.zip (4.3 KB, 14 views)

Last edited by roger64; 06-09-2014 at 02:21 AM.
roger64 is online now   Reply With Quote
Old 06-09-2014, 07:17 AM   #22
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: 9,530
Karma: 43837842
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.
DiapDealer is online now   Reply With Quote
Old 06-09-2014, 07:21 AM   #23
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
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.
roger64 is online now   Reply With Quote
Old 06-09-2014, 09:25 AM   #24
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: 9,530
Karma: 43837842
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}
becomes:
Code:
body {font-size:1em;margin-bottom:0.00em;margin-left:1.19em;margin-right:1.19em;margin-top:0cm}
Note the margin-top property at the end. It obviously won't matter when the value is zero, but it will when it's not.

If you get a basic conversion algorithm/regex that will cover all contingencies, integrating it into the cm2em script should be a snap.
DiapDealer is online now   Reply With Quote
Old 06-09-2014, 09:28 AM   #25
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: 9,530
Karma: 43837842
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by kovidgoyal View Post
Just FYI, you can write python scripts to process epub and azw3 files using the same infrastructure the calibre editor uses, like this:


And you run this like this

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.
Very cool! Thanks for the tip (and the functionality).

Last edited by DiapDealer; 06-09-2014 at 09:42 AM.
DiapDealer is online now   Reply With Quote
Old 06-09-2014, 11:07 AM   #26
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Quote:
Originally Posted by DiapDealer View Post
If you get a basic conversion algorithm/regex that will cover all contingencies, integrating it into the cm2em script should be a snap.
First, I am hunting them. Thanks for spotting this one.

I contact pingouinux about this damned last unforced
Code:
;
roger64 is online now   Reply With Quote
Old 06-09-2014, 04:17 PM   #27
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
@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
With the new release, I will make another round of testing involving some other people. I suppose this release will also be working for both Python 2.x and 3.x. If you take Kovid Goyal's advice, please provide some comments as this is way above my head.

Last edited by roger64; 06-09-2014 at 04:26 PM.
roger64 is online now   Reply With Quote
Old 06-09-2014, 05:21 PM   #28
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: 9,530
Karma: 43837842
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by roger64 View Post
@DiapDealer

Crossing the fingers, here is pingouin2 which takes into account your previous remark.


With the new release, I will make another round of testing involving some other people. I suppose this release will also be working for both Python 2.x and 3.x.
OK. Try version 3 on for size. Should work with python 2.x or 3.x.

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.
Attached Files
File Type: txt cm2em_v3.txt (2.3 KB, 20 views)
DiapDealer is online now   Reply With Quote
Old 06-09-2014, 05:33 PM   #29
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: 1,488
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Thank you! Testing resumes NOW
roger64 is online now   Reply With Quote
Old 06-09-2014, 06:23 PM   #30
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: 9,530
Karma: 43837842
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)
Attached Files
File Type: txt cm2em_calibre.txt (1.2 KB, 24 views)
DiapDealer 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
Question: Find/Replace in css stylesheet ElMiko Sigil 12 12-25-2011 07:29 AM
ePub margins : @page vs stylesheet.css Agama Calibre 1 08-16-2010 05:37 PM
How to maintain a global CSS stylesheet amoroso Sigil 7 07-18-2010 09:37 PM
EPub conversion stylesheet.css problem nycaleksey Calibre 1 03-26-2010 12:42 AM
Where is the stylesheet.css? roger64 Sigil 4 03-23-2010 03:12 PM


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


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