mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix handling of images and anchors for RB, FB2 and eReader output
This commit is contained in:
commit
56acf8fb56
@ -38,6 +38,14 @@ TAG_SPACE = [
|
|||||||
'br',
|
'br',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
TAG_IMAGES = [
|
||||||
|
'img',
|
||||||
|
]
|
||||||
|
|
||||||
|
TAG_LINKS = [
|
||||||
|
'a',
|
||||||
|
]
|
||||||
|
|
||||||
STYLES = [
|
STYLES = [
|
||||||
('font-weight', {'bold' : 'strong', 'bolder' : 'strong'}),
|
('font-weight', {'bold' : 'strong', 'bolder' : 'strong'}),
|
||||||
('font-style', {'italic' : 'emphasis'}),
|
('font-style', {'italic' : 'emphasis'}),
|
||||||
@ -48,6 +56,7 @@ class FB2MLizer(object):
|
|||||||
def __init__(self, log):
|
def __init__(self, log):
|
||||||
self.log = log
|
self.log = log
|
||||||
self.image_hrefs = {}
|
self.image_hrefs = {}
|
||||||
|
self.link_hrefs = {}
|
||||||
|
|
||||||
def extract_content(self, oeb_book, opts):
|
def extract_content(self, oeb_book, opts):
|
||||||
self.log.info('Converting XHTML to FB2 markup...')
|
self.log.info('Converting XHTML to FB2 markup...')
|
||||||
@ -57,6 +66,7 @@ class FB2MLizer(object):
|
|||||||
|
|
||||||
def fb2mlize_spine(self):
|
def fb2mlize_spine(self):
|
||||||
self.image_hrefs = {}
|
self.image_hrefs = {}
|
||||||
|
self.link_hrefs = {}
|
||||||
output = self.fb2_header()
|
output = self.fb2_header()
|
||||||
if 'titlepage' in self.oeb_book.guide:
|
if 'titlepage' in self.oeb_book.guide:
|
||||||
self.log.debug('Generating cover page...')
|
self.log.debug('Generating cover page...')
|
||||||
@ -68,6 +78,7 @@ class FB2MLizer(object):
|
|||||||
for item in self.oeb_book.spine:
|
for item in self.oeb_book.spine:
|
||||||
self.log.debug('Converting %s to FictionBook2 XML' % item.href)
|
self.log.debug('Converting %s to FictionBook2 XML' % item.href)
|
||||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||||
|
output += self.add_page_anchor(item)
|
||||||
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||||
output += self.fb2_body_footer()
|
output += self.fb2_body_footer()
|
||||||
output += self.fb2mlize_images()
|
output += self.fb2mlize_images()
|
||||||
@ -82,7 +93,7 @@ class FB2MLizer(object):
|
|||||||
|
|
||||||
if len(author_parts) == 1:
|
if len(author_parts) == 1:
|
||||||
author_last = author_parts[0]
|
author_last = author_parts[0]
|
||||||
elif len(author_parts == 2):
|
elif len(author_parts) == 2:
|
||||||
author_first = author_parts[0]
|
author_first = author_parts[0]
|
||||||
author_last = author_parts[1]
|
author_last = author_parts[1]
|
||||||
else:
|
else:
|
||||||
@ -108,6 +119,17 @@ class FB2MLizer(object):
|
|||||||
def fb2_footer(self):
|
def fb2_footer(self):
|
||||||
return u'</FictionBook>'
|
return u'</FictionBook>'
|
||||||
|
|
||||||
|
def add_page_anchor(self, page):
|
||||||
|
return self.get_anchor(page, '')
|
||||||
|
|
||||||
|
def get_anchor(self, page, aid):
|
||||||
|
aid = prepare_string_for_xml(aid)
|
||||||
|
aid = '%s#%s' % (page.href, aid)
|
||||||
|
if aid not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[aid] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
aid = self.link_hrefs[aid]
|
||||||
|
return '<v id="%s"></v>' % aid
|
||||||
|
|
||||||
def fb2mlize_images(self):
|
def fb2mlize_images(self):
|
||||||
images = u''
|
images = u''
|
||||||
for item in self.oeb_book.manifest:
|
for item in self.oeb_book.manifest:
|
||||||
@ -149,10 +171,32 @@ class FB2MLizer(object):
|
|||||||
tag = barename(elem.tag)
|
tag = barename(elem.tag)
|
||||||
tag_count = 0
|
tag_count = 0
|
||||||
|
|
||||||
if tag == 'img':
|
if tag in TAG_IMAGES:
|
||||||
if page.abshref(elem.attrib['src']) not in self.image_hrefs.keys():
|
if elem.attrib.get('src', None):
|
||||||
self.image_hrefs[page.abshref(elem.attrib['src'])] = '%s.jpg' % len(self.image_hrefs.keys())
|
if page.abshref(elem.attrib['src']) not in self.image_hrefs.keys():
|
||||||
fb2_text += '<image xlink:href="#%s" />' % self.image_hrefs[page.abshref(elem.attrib['src'])]
|
self.image_hrefs[page.abshref(elem.attrib['src'])] = '%s.jpg' % len(self.image_hrefs.keys())
|
||||||
|
fb2_text += '<image xlink:href="#%s" />' % self.image_hrefs[page.abshref(elem.attrib['src'])]
|
||||||
|
|
||||||
|
if tag in TAG_LINKS:
|
||||||
|
href = elem.get('href')
|
||||||
|
if href:
|
||||||
|
href = prepare_string_for_xml(page.abshref(href))
|
||||||
|
if '://' in href:
|
||||||
|
fb2_text += '<a xlink:href="%s">' % href
|
||||||
|
else:
|
||||||
|
if '#' not in href:
|
||||||
|
href += '#'
|
||||||
|
if href not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[href] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
href = self.link_hrefs[href]
|
||||||
|
fb2_text += '<a xlink:href="#%s">' % href
|
||||||
|
tag_count += 1
|
||||||
|
tag_stack.append('a')
|
||||||
|
|
||||||
|
# Anchor ids
|
||||||
|
id_name = elem.get('id')
|
||||||
|
if id_name:
|
||||||
|
fb2_text += self.get_anchor(page, id_name)
|
||||||
|
|
||||||
fb2_tag = TAG_MAP.get(tag, None)
|
fb2_tag = TAG_MAP.get(tag, None)
|
||||||
if fb2_tag and fb2_tag not in tag_stack:
|
if fb2_tag and fb2_tag not in tag_stack:
|
||||||
|
@ -9,7 +9,7 @@ import os
|
|||||||
class EreaderError(Exception):
|
class EreaderError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def image_name(name):
|
def image_name(name, taken_names=[]):
|
||||||
name = os.path.basename(name)
|
name = os.path.basename(name)
|
||||||
|
|
||||||
if len(name) > 32:
|
if len(name) > 32:
|
||||||
@ -18,6 +18,10 @@ def image_name(name):
|
|||||||
namee = name[10+cut:]
|
namee = name[10+cut:]
|
||||||
name = names + namee
|
name = names + namee
|
||||||
|
|
||||||
|
while name in taken_names:
|
||||||
|
for i in xrange(9999999999999999999999999999999):
|
||||||
|
name = '%s%s' % (name[:-len('%s' % i)], i)
|
||||||
|
|
||||||
name = name.ljust(32, '\x00')[:32]
|
name = name.ljust(32, '\x00')[:32]
|
||||||
|
|
||||||
return name
|
return name
|
||||||
|
@ -22,7 +22,6 @@ import cStringIO
|
|||||||
from calibre.ebooks.pdb.formatwriter import FormatWriter
|
from calibre.ebooks.pdb.formatwriter import FormatWriter
|
||||||
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
|
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
|
||||||
from calibre.ebooks.pdb.header import PdbHeaderBuilder
|
from calibre.ebooks.pdb.header import PdbHeaderBuilder
|
||||||
from calibre.ebooks.pdb.ereader import image_name
|
|
||||||
from calibre.ebooks.pml.pmlml import PMLMLizer
|
from calibre.ebooks.pml.pmlml import PMLMLizer
|
||||||
|
|
||||||
IDENTITY = 'PNRdPPrs'
|
IDENTITY = 'PNRdPPrs'
|
||||||
@ -38,8 +37,8 @@ class Writer(FormatWriter):
|
|||||||
self.log = log
|
self.log = log
|
||||||
|
|
||||||
def write_content(self, oeb_book, out_stream, metadata=None):
|
def write_content(self, oeb_book, out_stream, metadata=None):
|
||||||
text = self._text(oeb_book)
|
text, image_hrefs = self._text(oeb_book)
|
||||||
images = self._images(oeb_book.manifest)
|
images = self._images(oeb_book.manifest, image_hrefs)
|
||||||
metadata = [self._metadata(metadata)]
|
metadata = [self._metadata(metadata)]
|
||||||
|
|
||||||
hr = [self._header_record(len(text), len(images))]
|
hr = [self._header_record(len(text), len(images))]
|
||||||
@ -66,13 +65,13 @@ class Writer(FormatWriter):
|
|||||||
for i in range(0, (len(pml) / MAX_RECORD_SIZE) + 1):
|
for i in range(0, (len(pml) / MAX_RECORD_SIZE) + 1):
|
||||||
pml_pages.append(zlib.compress(pml[i * MAX_RECORD_SIZE : (i * MAX_RECORD_SIZE) + MAX_RECORD_SIZE]))
|
pml_pages.append(zlib.compress(pml[i * MAX_RECORD_SIZE : (i * MAX_RECORD_SIZE) + MAX_RECORD_SIZE]))
|
||||||
|
|
||||||
return pml_pages
|
return pml_pages, pmlmlizer.image_hrefs
|
||||||
|
|
||||||
def _images(self, manifest):
|
def _images(self, manifest, image_hrefs):
|
||||||
images = []
|
images = []
|
||||||
|
|
||||||
for item in manifest:
|
for item in manifest:
|
||||||
if item.media_type in OEB_RASTER_IMAGES:
|
if item.media_type in OEB_RASTER_IMAGES and item.href in image_hrefs.keys():
|
||||||
try:
|
try:
|
||||||
im = Image.open(cStringIO.StringIO(item.data)).convert('P')
|
im = Image.open(cStringIO.StringIO(item.data)).convert('P')
|
||||||
im.thumbnail((300,300), Image.ANTIALIAS)
|
im.thumbnail((300,300), Image.ANTIALIAS)
|
||||||
@ -82,7 +81,7 @@ class Writer(FormatWriter):
|
|||||||
data = data.getvalue()
|
data = data.getvalue()
|
||||||
|
|
||||||
header = 'PNG '
|
header = 'PNG '
|
||||||
header += image_name(item.href)
|
header += image_hrefs[item.href].ljust(32, '\x00')[:32]
|
||||||
header = header.ljust(62, '\x00')
|
header = header.ljust(62, '\x00')
|
||||||
|
|
||||||
if len(data) + len(header) < 65505:
|
if len(data) + len(header) < 65505:
|
||||||
|
@ -53,6 +53,10 @@ LINK_TAGS = [
|
|||||||
'a',
|
'a',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
IMAGE_TAGS = [
|
||||||
|
'img',
|
||||||
|
]
|
||||||
|
|
||||||
SEPARATE_TAGS = [
|
SEPARATE_TAGS = [
|
||||||
'h1',
|
'h1',
|
||||||
'h2',
|
'h2',
|
||||||
@ -69,6 +73,8 @@ SEPARATE_TAGS = [
|
|||||||
class PMLMLizer(object):
|
class PMLMLizer(object):
|
||||||
def __init__(self, log):
|
def __init__(self, log):
|
||||||
self.log = log
|
self.log = log
|
||||||
|
self.image_hrefs = {}
|
||||||
|
self.link_hrefs = {}
|
||||||
|
|
||||||
def extract_content(self, oeb_book, opts):
|
def extract_content(self, oeb_book, opts):
|
||||||
self.log.info('Converting XHTML to PML markup...')
|
self.log.info('Converting XHTML to PML markup...')
|
||||||
@ -77,6 +83,8 @@ class PMLMLizer(object):
|
|||||||
return self.pmlmlize_spine()
|
return self.pmlmlize_spine()
|
||||||
|
|
||||||
def pmlmlize_spine(self):
|
def pmlmlize_spine(self):
|
||||||
|
self.image_hrefs = {}
|
||||||
|
self.link_hrefs = {}
|
||||||
output = u''
|
output = u''
|
||||||
if 'titlepage' in self.oeb_book.guide:
|
if 'titlepage' in self.oeb_book.guide:
|
||||||
self.log.debug('Generating title page...')
|
self.log.debug('Generating title page...')
|
||||||
@ -84,19 +92,25 @@ class PMLMLizer(object):
|
|||||||
item = self.oeb_book.manifest.hrefs[href]
|
item = self.oeb_book.manifest.hrefs[href]
|
||||||
if item.spine_position is None:
|
if item.spine_position is None:
|
||||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||||
output += self.dump_text(item.data.find(XHTML('body')), stylizer)
|
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||||
for item in self.oeb_book.spine:
|
for item in self.oeb_book.spine:
|
||||||
self.log.debug('Converting %s to PML markup...' % item.href)
|
self.log.debug('Converting %s to PML markup...' % item.href)
|
||||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||||
output += self.add_page_anchor(item.href)
|
output += self.add_page_anchor(item)
|
||||||
output += self.dump_text(item.data.find(XHTML('body')), stylizer)
|
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||||
output = self.clean_text(output)
|
output = self.clean_text(output)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def add_page_anchor(self, href):
|
def add_page_anchor(self, page):
|
||||||
href = os.path.splitext(os.path.basename(href))[0]
|
return self.get_anchor(page, '')
|
||||||
return u'\\Q="%s"' % href
|
|
||||||
|
def get_anchor(self, page, aid):
|
||||||
|
aid = '%s#%s' % (page.href, aid)
|
||||||
|
if aid not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[aid] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
aid = self.link_hrefs[aid]
|
||||||
|
return u'\\Q="%s"' % aid
|
||||||
|
|
||||||
def clean_text(self, text):
|
def clean_text(self, text):
|
||||||
# Remove excess spaces at beginning and end of lines
|
# Remove excess spaces at beginning and end of lines
|
||||||
@ -123,7 +137,7 @@ class PMLMLizer(object):
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def dump_text(self, elem, stylizer, tag_stack=[]):
|
def dump_text(self, elem, stylizer, page, tag_stack=[]):
|
||||||
if not isinstance(elem.tag, basestring) \
|
if not isinstance(elem.tag, basestring) \
|
||||||
or namespace(elem.tag) != XHTML_NS:
|
or namespace(elem.tag) != XHTML_NS:
|
||||||
return u''
|
return u''
|
||||||
@ -146,8 +160,11 @@ class PMLMLizer(object):
|
|||||||
|
|
||||||
# Process tags that need special processing and that do not have inner
|
# Process tags that need special processing and that do not have inner
|
||||||
# text. Usually these require an argument
|
# text. Usually these require an argument
|
||||||
if tag == 'img':
|
if tag in IMAGE_TAGS:
|
||||||
text += '\\m="%s"' % image_name(os.path.basename(elem.get('src'))).strip('\x00')
|
if elem.attrib.get('src', None):
|
||||||
|
if page.abshref(elem.attrib['src']) not in self.image_hrefs.keys():
|
||||||
|
self.image_hrefs[page.abshref(elem.attrib['src'])] = image_name('%s' % len(self.image_hrefs.keys()), self.image_hrefs.keys()).strip('\x00')
|
||||||
|
text += '\\m="%s"' % self.image_hrefs[page.abshref(elem.attrib['src'])]
|
||||||
if tag == 'hr':
|
if tag == 'hr':
|
||||||
text += '\\w'
|
text += '\\w'
|
||||||
width = elem.get('width')
|
width = elem.get('width')
|
||||||
@ -171,17 +188,22 @@ class PMLMLizer(object):
|
|||||||
# Anchors links
|
# Anchors links
|
||||||
if tag in LINK_TAGS and 'q' not in tag_stack:
|
if tag in LINK_TAGS and 'q' not in tag_stack:
|
||||||
href = elem.get('href')
|
href = elem.get('href')
|
||||||
if href and '://' not in href:
|
if href:
|
||||||
if '#' in href:
|
href = page.abshref(href)
|
||||||
href = href.partition('#')[2]
|
if '://' not in href:
|
||||||
href = os.path.splitext(os.path.basename(href))[0]
|
if '#' not in href:
|
||||||
|
href += '#'
|
||||||
|
if href not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[href] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
href = self.link_hrefs[href]
|
||||||
|
text += '\\q="#%s"' % href
|
||||||
tag_count += 1
|
tag_count += 1
|
||||||
text += '\\q="#%s"' % href
|
|
||||||
tag_stack.append('q')
|
tag_stack.append('q')
|
||||||
|
|
||||||
# Anchor ids
|
# Anchor ids
|
||||||
id_name = elem.get('id')
|
id_name = elem.get('id')
|
||||||
if id_name:
|
if id_name:
|
||||||
text += '\\Q="%s"' % os.path.splitext(id_name)[0]
|
text += self.get_anchor(page, id_name)
|
||||||
|
|
||||||
# Processes style information
|
# Processes style information
|
||||||
for s in STYLES:
|
for s in STYLES:
|
||||||
@ -197,7 +219,7 @@ class PMLMLizer(object):
|
|||||||
text += self.elem_text(elem, tag_stack)
|
text += self.elem_text(elem, tag_stack)
|
||||||
|
|
||||||
for item in elem:
|
for item in elem:
|
||||||
text += self.dump_text(item, stylizer, tag_stack)
|
text += self.dump_text(item, stylizer, page, tag_stack)
|
||||||
|
|
||||||
close_tag_list = []
|
close_tag_list = []
|
||||||
for i in range(0, tag_count):
|
for i in range(0, tag_count):
|
||||||
|
@ -45,6 +45,10 @@ LINK_TAGS = [
|
|||||||
'a',
|
'a',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
IMAGE_TAGS = [
|
||||||
|
'img',
|
||||||
|
]
|
||||||
|
|
||||||
STYLES = [
|
STYLES = [
|
||||||
('font-weight', {'bold' : 'b', 'bolder' : 'b'}),
|
('font-weight', {'bold' : 'b', 'bolder' : 'b'}),
|
||||||
('font-style', {'italic' : 'i'}),
|
('font-style', {'italic' : 'i'}),
|
||||||
@ -56,6 +60,7 @@ class RBMLizer(object):
|
|||||||
def __init__(self, log, name_map={}):
|
def __init__(self, log, name_map={}):
|
||||||
self.log = log
|
self.log = log
|
||||||
self.name_map = name_map
|
self.name_map = name_map
|
||||||
|
self.link_hrefs = {}
|
||||||
|
|
||||||
def extract_content(self, oeb_book, opts):
|
def extract_content(self, oeb_book, opts):
|
||||||
self.log.info('Converting XHTML to RB markup...')
|
self.log.info('Converting XHTML to RB markup...')
|
||||||
@ -65,6 +70,7 @@ class RBMLizer(object):
|
|||||||
|
|
||||||
|
|
||||||
def mlize_spine(self):
|
def mlize_spine(self):
|
||||||
|
self.link_hrefs = {}
|
||||||
output = u'<HTML><HEAD><TITLE></TITLE></HEAD><BODY>'
|
output = u'<HTML><HEAD><TITLE></TITLE></HEAD><BODY>'
|
||||||
if 'titlepage' in self.oeb_book.guide:
|
if 'titlepage' in self.oeb_book.guide:
|
||||||
self.log.debug('Generating cover page...')
|
self.log.debug('Generating cover page...')
|
||||||
@ -72,19 +78,25 @@ class RBMLizer(object):
|
|||||||
item = self.oeb_book.manifest.hrefs[href]
|
item = self.oeb_book.manifest.hrefs[href]
|
||||||
if item.spine_position is None:
|
if item.spine_position is None:
|
||||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||||
output += self.dump_text(item.data.find(XHTML('body')), stylizer)
|
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||||
for item in self.oeb_book.spine:
|
for item in self.oeb_book.spine:
|
||||||
self.log.debug('Converting %s to RocketBook HTML...' % item.href)
|
self.log.debug('Converting %s to RocketBook HTML...' % item.href)
|
||||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||||
output += self.add_page_anchor(item.href)
|
output += self.add_page_anchor(item)
|
||||||
output += self.dump_text(item.data.find(XHTML('body')), stylizer)
|
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||||
output += u'</BODY></HTML>'
|
output += u'</BODY></HTML>'
|
||||||
output = self.clean_text(output)
|
output = self.clean_text(output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def add_page_anchor(self, href):
|
def add_page_anchor(self, page):
|
||||||
href = os.path.splitext(os.path.basename(href))[0]
|
return self.get_anchor(page, '')
|
||||||
return u'<A NAME="%s"></A>' % href
|
|
||||||
|
def get_anchor(self, page, aid):
|
||||||
|
aid = '%s#%s' % (page.href, aid)
|
||||||
|
if aid not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[aid] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
aid = self.link_hrefs[aid]
|
||||||
|
return u'<A NAME="%s"></A>' % aid
|
||||||
|
|
||||||
def clean_text(self, text):
|
def clean_text(self, text):
|
||||||
# Remove anchors that do not have links
|
# Remove anchors that do not have links
|
||||||
@ -95,7 +107,7 @@ class RBMLizer(object):
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def dump_text(self, elem, stylizer, tag_stack=[]):
|
def dump_text(self, elem, stylizer, page, tag_stack=[]):
|
||||||
if not isinstance(elem.tag, basestring) \
|
if not isinstance(elem.tag, basestring) \
|
||||||
or namespace(elem.tag) != XHTML_NS:
|
or namespace(elem.tag) != XHTML_NS:
|
||||||
return u''
|
return u''
|
||||||
@ -112,10 +124,11 @@ class RBMLizer(object):
|
|||||||
|
|
||||||
# Process tags that need special processing and that do not have inner
|
# Process tags that need special processing and that do not have inner
|
||||||
# text. Usually these require an argument
|
# text. Usually these require an argument
|
||||||
if tag == 'img':
|
if tag in IMAGE_TAGS:
|
||||||
src = os.path.basename(elem.get('src'))
|
if elem.attrib.get('src', None):
|
||||||
name = self.name_map.get(src, src)
|
if page.abshref(elem.attrib['src']) not in self.name_map.keys():
|
||||||
text += '<IMG SRC="%s">' % name
|
self.name_map[page.abshref(elem.attrib['src'])] = unique_name('%s' % len(self.image_hrefs.keys()), self.image_hrefs.keys(), self.name_map.keys())
|
||||||
|
text += '<IMG SRC="%s">' % self.name_map[page.abshref(elem.attrib['src'])]
|
||||||
|
|
||||||
rb_tag = tag.upper() if tag in TAGS else None
|
rb_tag = tag.upper() if tag in TAGS else None
|
||||||
if rb_tag:
|
if rb_tag:
|
||||||
@ -123,21 +136,25 @@ class RBMLizer(object):
|
|||||||
text += '<%s>' % rb_tag
|
text += '<%s>' % rb_tag
|
||||||
tag_stack.append(rb_tag)
|
tag_stack.append(rb_tag)
|
||||||
|
|
||||||
|
# Anchors links
|
||||||
if tag in LINK_TAGS:
|
if tag in LINK_TAGS:
|
||||||
href = elem.get('href')
|
href = elem.get('href')
|
||||||
if href:
|
if href:
|
||||||
|
href = page.abshref(href)
|
||||||
if '://' not in href:
|
if '://' not in href:
|
||||||
if '#' in href:
|
if '#' not in href:
|
||||||
href = href.partition('#')[2]
|
href += '#'
|
||||||
href = os.path.splitext(os.path.basename(href))[0]
|
if href not in self.link_hrefs.keys():
|
||||||
|
self.link_hrefs[href] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||||
|
href = self.link_hrefs[href]
|
||||||
|
text += '<A HREF="#%s">' % href
|
||||||
tag_count += 1
|
tag_count += 1
|
||||||
text += '<A HREF="#%s">' % href
|
|
||||||
tag_stack.append('A')
|
tag_stack.append('A')
|
||||||
|
|
||||||
# Anchor ids
|
# Anchor ids
|
||||||
id_name = elem.get('id')
|
id_name = elem.get('id')
|
||||||
if id_name:
|
if id_name:
|
||||||
text += '<A NAME="%s"></A>' % os.path.splitext(id_name)[0]
|
text += self.get_anchor(page, id_name)
|
||||||
|
|
||||||
# Processes style information
|
# Processes style information
|
||||||
for s in STYLES:
|
for s in STYLES:
|
||||||
@ -153,7 +170,7 @@ class RBMLizer(object):
|
|||||||
text += prepare_string_for_xml(elem.text)
|
text += prepare_string_for_xml(elem.text)
|
||||||
|
|
||||||
for item in elem:
|
for item in elem:
|
||||||
text += self.dump_text(item, stylizer, tag_stack)
|
text += self.dump_text(item, stylizer, page, tag_stack)
|
||||||
|
|
||||||
close_tag_list = []
|
close_tag_list = []
|
||||||
for i in range(0, tag_count):
|
for i in range(0, tag_count):
|
||||||
|
@ -125,10 +125,10 @@ class RBWriter(object):
|
|||||||
im.save(data, 'PNG')
|
im.save(data, 'PNG')
|
||||||
data = data.getvalue()
|
data = data.getvalue()
|
||||||
|
|
||||||
name = '%s.png' % os.path.splitext(os.path.basename(item.href))[0]
|
name = '%s.png' % len(used_names)
|
||||||
name = unique_name(name, used_names)
|
name = unique_name(name, used_names)
|
||||||
used_names.append(name)
|
used_names.append(name)
|
||||||
self.name_map[os.path.basename(item.href)] = name
|
self.name_map[item.href] = name
|
||||||
|
|
||||||
images.append((name, data))
|
images.append((name, data))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user