mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Clean up the handling of anchors and page breaks.
This commit is contained in:
parent
4346c7f66c
commit
c4bef79096
@ -44,6 +44,7 @@ class BlockState(object):
|
||||
self.vmargin = 0.
|
||||
self.pbreak = False
|
||||
self.istate = None
|
||||
self.content = False
|
||||
|
||||
class FormatState(object):
|
||||
def __init__(self):
|
||||
@ -120,17 +121,20 @@ class MobiMLizer(object):
|
||||
return result
|
||||
|
||||
def mobimlize_content(self, tag, text, bstate, istates):
|
||||
bstate.content = True
|
||||
istate = istates[-1]
|
||||
if istate.ids:
|
||||
body = bstate.body
|
||||
index = max((0, len(body) - 1))
|
||||
for id in istate.ids:
|
||||
body.insert(index, etree.Element('a', attrib={'id': id}))
|
||||
istate.ids.clear()
|
||||
para = bstate.para
|
||||
if tag in SPECIAL_TAGS and not text:
|
||||
para = para if para is not None else bstate.body
|
||||
elif para is None:
|
||||
body = bstate.body
|
||||
if bstate.pbreak:
|
||||
etree.SubElement(body, MBP('pagebreak'))
|
||||
bstate.pbreak = False
|
||||
if istate.ids:
|
||||
for id in istate.ids:
|
||||
etree.SubElement(body, 'a', attrib={'id': id})
|
||||
istate.ids.clear()
|
||||
bstate.istate = None
|
||||
parent = bstate.nested[-1] if bstate.nested else bstate.body
|
||||
indent = istate.indent
|
||||
@ -140,9 +144,6 @@ class MobiMLizer(object):
|
||||
indent = 0
|
||||
elif indent != 0 and abs(indent) < self.profile.fbase:
|
||||
indent = (indent / abs(indent)) * self.profile.fbase
|
||||
if bstate.pbreak:
|
||||
etree.SubElement(parent, MBP('pagebreak'))
|
||||
bstate.pbreak = False
|
||||
if tag in NESTABLE_TAGS:
|
||||
para = wrapper = etree.SubElement(parent, tag)
|
||||
bstate.nested.append(para)
|
||||
@ -270,7 +271,7 @@ class MobiMLizer(object):
|
||||
else:
|
||||
last = elem[-1]
|
||||
last.text = (last.text or '') + (u'\xa0' * spaces)
|
||||
if style['page-break-before'] in PAGE_BREAKS:
|
||||
if bstate.content and style['page-break-before'] in PAGE_BREAKS:
|
||||
bstate.pbreak = True
|
||||
istate.fsize = self.mobimlize_font(style['font-size'])
|
||||
istate.italic = True if style['font-style'] == 'italic' else False
|
||||
@ -340,7 +341,7 @@ class MobiMLizer(object):
|
||||
tail = COLLAPSE.sub(' ', child.tail)
|
||||
if tail:
|
||||
self.mobimlize_content(tag, tail, bstate, istates)
|
||||
if style['page-break-after'] in PAGE_BREAKS:
|
||||
if bstate.content and style['page-break-after'] in PAGE_BREAKS:
|
||||
bstate.pbreak = True
|
||||
if isblock:
|
||||
para = bstate.para
|
||||
|
@ -118,8 +118,7 @@ class Serializer(object):
|
||||
if item and item.spine_position is None:
|
||||
return False
|
||||
id = item.id if item else base.id
|
||||
frag = frag if frag else 'calibre_top'
|
||||
href = '#'.join((id, frag))
|
||||
href = '#'.join((id, frag)) if frag else id
|
||||
buffer.write('filepos=')
|
||||
self.href_offsets[href].append(buffer.tell())
|
||||
buffer.write('0000000000')
|
||||
@ -138,7 +137,7 @@ class Serializer(object):
|
||||
buffer = self.buffer
|
||||
if not item.linear:
|
||||
self.breaks.append(buffer.tell() - 1)
|
||||
self.id_offsets[item.id + '#calibre_top'] = buffer.tell()
|
||||
self.id_offsets[item.id] = buffer.tell()
|
||||
for elem in item.data.find(XHTML('body')):
|
||||
self.serialize_elem(elem, item)
|
||||
buffer.write('<mbp:pagebreak/>')
|
||||
@ -155,6 +154,9 @@ class Serializer(object):
|
||||
id = '#'.join((item.id, elem.attrib[attr]))
|
||||
self.id_offsets[id] = buffer.tell()
|
||||
del elem.attrib[attr]
|
||||
if tag == 'a' and not elem.attrib \
|
||||
and not len(elem) and not elem.text:
|
||||
return
|
||||
buffer.write('<')
|
||||
buffer.write(tag)
|
||||
if elem.attrib:
|
||||
|
@ -249,10 +249,10 @@ class Metadata(object):
|
||||
self.oeb = oeb
|
||||
self.items = defaultdict(list)
|
||||
|
||||
def add(self, term, value, attrib={}, index=-1, **kwargs):
|
||||
def add(self, term, value, attrib={}, **kwargs):
|
||||
item = self.Item(term, value, attrib, **kwargs)
|
||||
items = self.items[barename(item.term)]
|
||||
items.insert(index, item)
|
||||
items.append(item)
|
||||
return item
|
||||
|
||||
def iterkeys(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user