mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
AZW3 Input: Handle AZW3 files that contain Amazon specific CSS media queries. Fixes #1406708 [Converting azw3 to epub creates artifacts](https://bugs.launchpad.net/calibre/+bug/1406708)
This commit is contained in:
parent
79c029c15f
commit
acfcc63cfc
@ -306,6 +306,24 @@ def upshift_markup(parts):
|
|||||||
# store away modified version
|
# store away modified version
|
||||||
parts[i] = part
|
parts[i] = part
|
||||||
|
|
||||||
|
def handle_media_queries(raw):
|
||||||
|
# cssutils cannot handle CSS 3 media queries. We look for media queries
|
||||||
|
# that use amzn-mobi or amzn-kf8 and map them to asimple @media screen
|
||||||
|
# rule. See https://bugs.launchpad.net/bugs/1406708 for an example
|
||||||
|
import tinycss
|
||||||
|
parser = tinycss.make_full_parser()
|
||||||
|
def replace(m):
|
||||||
|
sheet = parser.parse_stylesheet(m.group() + '}')
|
||||||
|
for mq in sheet.rules[0].media:
|
||||||
|
# Only accept KF8 media types
|
||||||
|
if (mq.media_type, mq.negated) in {('amzn-mobi', True), ('amzn-kf8', False)}:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return m.group()
|
||||||
|
return '@media screen {'
|
||||||
|
|
||||||
|
return re.sub(r'@media\s[^{]*{', replace, raw)
|
||||||
|
|
||||||
def expand_mobi8_markup(mobi8_reader, resource_map, log):
|
def expand_mobi8_markup(mobi8_reader, resource_map, log):
|
||||||
# First update all internal links that are based on offsets
|
# First update all internal links that are based on offsets
|
||||||
parts = update_internal_links(mobi8_reader, log)
|
parts = update_internal_links(mobi8_reader, log)
|
||||||
@ -347,6 +365,8 @@ def expand_mobi8_markup(mobi8_reader, resource_map, log):
|
|||||||
if not os.path.exists(fi.dir):
|
if not os.path.exists(fi.dir):
|
||||||
os.mkdir(fi.dir)
|
os.mkdir(fi.dir)
|
||||||
with open(os.path.join(fi.dir, fi.fname), 'wb') as f:
|
with open(os.path.join(fi.dir, fi.fname), 'wb') as f:
|
||||||
|
if fi.fname.endswith('.css') and '@media' in flow:
|
||||||
|
flow = handle_media_queries(flow)
|
||||||
f.write(flow.encode('utf-8'))
|
f.write(flow.encode('utf-8'))
|
||||||
|
|
||||||
return spine
|
return spine
|
||||||
|
Loading…
x
Reference in New Issue
Block a user