mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DOCX: Performance improvement
This commit is contained in:
parent
02a28056de
commit
a210f681a7
@ -83,11 +83,10 @@ def get(x, attr, default=None):
|
|||||||
return x.attrib.get(expand(attr), default)
|
return x.attrib.get(expand(attr), default)
|
||||||
|
|
||||||
def ancestor(elem, name):
|
def ancestor(elem, name):
|
||||||
tag = expand(name)
|
try:
|
||||||
while elem is not None:
|
return XPath('ancestor::%s[1]' % name)(elem)[0]
|
||||||
elem = elem.getparent()
|
except IndexError:
|
||||||
if getattr(elem, 'tag', None) == tag:
|
return None
|
||||||
return elem
|
|
||||||
|
|
||||||
def generate_anchor(name, existing):
|
def generate_anchor(name, existing):
|
||||||
x = y = 'id_' + re.sub(r'[^0-9a-zA-Z_]', '', ascii_text(name)).lstrip('_')
|
x = y = 'id_' + re.sub(r'[^0-9a-zA-Z_]', '', ascii_text(name)).lstrip('_')
|
||||||
|
@ -16,7 +16,7 @@ from lxml.html.builder import (
|
|||||||
from calibre.ebooks.docx.container import DOCX, fromstring
|
from calibre.ebooks.docx.container import DOCX, fromstring
|
||||||
from calibre.ebooks.docx.names import (
|
from calibre.ebooks.docx.names import (
|
||||||
XPath, is_tag, XML, STYLES, NUMBERING, FONTS, get, generate_anchor,
|
XPath, is_tag, XML, STYLES, NUMBERING, FONTS, get, generate_anchor,
|
||||||
descendants, ancestor, FOOTNOTES, ENDNOTES, children)
|
descendants, FOOTNOTES, ENDNOTES, children)
|
||||||
from calibre.ebooks.docx.styles import Styles, inherit, PageProperties
|
from calibre.ebooks.docx.styles import Styles, inherit, PageProperties
|
||||||
from calibre.ebooks.docx.numbering import Numbering
|
from calibre.ebooks.docx.numbering import Numbering
|
||||||
from calibre.ebooks.docx.fonts import Fonts
|
from calibre.ebooks.docx.fonts import Fonts
|
||||||
@ -308,6 +308,7 @@ class Convert(object):
|
|||||||
|
|
||||||
current_anchor = None
|
current_anchor = None
|
||||||
current_hyperlink = None
|
current_hyperlink = None
|
||||||
|
hl_xpath = XPath('ancestor::w:hyperlink[1]')
|
||||||
|
|
||||||
for x in descendants(p, 'w:r', 'w:bookmarkStart', 'w:hyperlink'):
|
for x in descendants(p, 'w:r', 'w:bookmarkStart', 'w:hyperlink'):
|
||||||
if x.tag.endswith('}r'):
|
if x.tag.endswith('}r'):
|
||||||
@ -316,10 +317,10 @@ class Convert(object):
|
|||||||
(dest if len(dest) == 0 else span).set('id', current_anchor)
|
(dest if len(dest) == 0 else span).set('id', current_anchor)
|
||||||
current_anchor = None
|
current_anchor = None
|
||||||
if current_hyperlink is not None:
|
if current_hyperlink is not None:
|
||||||
hl = ancestor(x, 'w:hyperlink')
|
try:
|
||||||
if hl is not None:
|
hl = hl_xpath(x)[0]
|
||||||
self.link_map[hl].append(span)
|
self.link_map[hl].append(span)
|
||||||
else:
|
except IndexError:
|
||||||
current_hyperlink = None
|
current_hyperlink = None
|
||||||
dest.append(span)
|
dest.append(span)
|
||||||
self.layers[p].append(x)
|
self.layers[p].append(x)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user