mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Nicer prefix for index anchors
This commit is contained in:
parent
7ea8e2abc5
commit
a761e39317
@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import re, os, uuid
|
import re, os
|
||||||
|
|
||||||
from calibre.ebooks.docx.names import XPath, get, namespaces
|
from calibre.ebooks.docx.names import XPath, get, namespaces
|
||||||
TEST_INDEX = 'CALIBRE_TEST_INDEX' in os.environ
|
TEST_INDEX = 'CALIBRE_TEST_INDEX' in os.environ
|
||||||
@ -88,10 +88,15 @@ class Fields(object):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.fields = []
|
self.fields = []
|
||||||
self.bookmark_counter = 0
|
self.index_bookmark_counter = 0
|
||||||
self.bookmark_prefix = str(uuid.uuid4())
|
self.index_bookmark_prefix = 'index-'
|
||||||
|
|
||||||
def __call__(self, doc, log):
|
def __call__(self, doc, log):
|
||||||
|
all_ids = frozenset(XPath('//*/@w:id')(doc))
|
||||||
|
c = 0
|
||||||
|
while self.index_bookmark_prefix in all_ids:
|
||||||
|
c += 1
|
||||||
|
self.index_bookmark_prefix = self.index_bookmark_prefix.replace('-', '%d-' % c)
|
||||||
stack = []
|
stack = []
|
||||||
for elem in XPath(
|
for elem in XPath(
|
||||||
'//*[name()="w:p" or name()="w:r" or name()="w:instrText" or (name()="w:fldChar" and (@w:fldCharType="begin" or @w:fldCharType="end"))]')(doc):
|
'//*[name()="w:p" or name()="w:r" or name()="w:instrText" or (name()="w:fldChar" and (@w:fldCharType="begin" or @w:fldCharType="end"))]')(doc):
|
||||||
@ -158,8 +163,8 @@ class Fields(object):
|
|||||||
if xe:
|
if xe:
|
||||||
# We insert a synthetic bookmark around this index item so that we
|
# We insert a synthetic bookmark around this index item so that we
|
||||||
# can link to it later
|
# can link to it later
|
||||||
self.bookmark_counter += 1
|
self.index_bookmark_counter += 1
|
||||||
bmark = xe['anchor'] = '%s-%d' % (self.bookmark_prefix, self.bookmark_counter)
|
bmark = xe['anchor'] = '%s%d' % (self.index_bookmark_prefix, self.index_bookmark_counter)
|
||||||
p = field.start.getparent()
|
p = field.start.getparent()
|
||||||
bm = p.makeelement(WORD('bookmarkStart'))
|
bm = p.makeelement(WORD('bookmarkStart'))
|
||||||
bm.set(WORD('id'), bmark), bm.set(WORD('name'), bmark)
|
bm.set(WORD('id'), bmark), bm.set(WORD('name'), bmark)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user