diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index ea73def343..6d269efd21 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -404,7 +404,7 @@ class Convert(object): self.svg_rasterizer(self.oeb, self.opts) self.styles_manager = StylesManager(self.docx.namespace, self.log, self.mi.language) - self.links_manager = LinksManager(self.docx.namespace, self.docx.document_relationships) + self.links_manager = LinksManager(self.docx.namespace, self.docx.document_relationships, self.log) self.images_manager = ImagesManager(self.oeb, self.docx.document_relationships) self.lists_manager = ListsManager(self.docx) self.fonts_manager = FontsManager(self.docx.namespace, self.oeb, self.opts) @@ -454,7 +454,7 @@ class Convert(object): self.current_lang = lang_for_tag(item.data) or self.styles_manager.document_lang for i, body in enumerate(XPath('//h:body')(item.data)): with self.blocks: - body.set('id', body.get('id', None) or self.links_manager.top_anchor) + self.links_manager.bookmark_for_anchor(self.links_manager.top_anchor, self.current_item, body) self.process_tag(body, stylizer, is_first_tag=i == 0) def process_tag(self, html_tag, stylizer, is_first_tag=False, float_spec=None): diff --git a/src/calibre/ebooks/docx/writer/links.py b/src/calibre/ebooks/docx/writer/links.py index d41c3edeb5..afaa15b8ee 100644 --- a/src/calibre/ebooks/docx/writer/links.py +++ b/src/calibre/ebooks/docx/writer/links.py @@ -61,8 +61,9 @@ def sanitize_bookmark_name(base): class LinksManager(object): - def __init__(self, namespace, document_relationships): + def __init__(self, namespace, document_relationships, log): self.namespace = namespace + self.log = log self.document_relationships = document_relationships self.top_anchor = type('')(uuid4().hex) self.anchor_map = {} @@ -107,6 +108,8 @@ class LinksManager(object): else: bmark = self.anchor_map[(href, self.top_anchor)] return self.namespace.makeelement(parent, 'w:hyperlink', w_anchor=bmark, w_tooltip=tooltip or '') + else: + self.log.warn('Ignoring internal hyperlink with href (%s) pointing to unknown destination' % url) if purl.scheme in {'http', 'https', 'ftp'}: if url not in self.external_links: self.external_links[url] = self.document_relationships.add_relationship(url, self.namespace.names['LINKS'], target_mode='External')