DOCX Input: Add a semantic class to footnote links and targets

This commit is contained in:
Kovid Goyal 2017-01-12 13:50:02 +05:30
parent 692022e3b3
commit fad7c991c6

View File

@ -141,6 +141,7 @@ class Convert(object):
self.body.append(dl) self.body.append(dl)
for anchor, text, note in self.footnotes: for anchor, text, note in self.footnotes:
dl.append(DT('[', A('' + text, href='#back_%s' % anchor, title=text), id=anchor)) dl.append(DT('[', A('' + text, href='#back_%s' % anchor, title=text), id=anchor))
dl[-1].set('class', 'calibre-docx-footnote-target')
dl[-1][0].tail = ']' dl[-1][0].tail = ']'
dl.append(DD()) dl.append(DD())
paras = [] paras = []
@ -352,11 +353,11 @@ class Convert(object):
def write(self, doc): def write(self, doc):
toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map, self.log, self.namespace) toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map, self.log, self.namespace)
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>') raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f: with lopen(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw) f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx, self.notes_nopb, self.nosupsub) css = self.styles.generate_css(self.dest_dir, self.docx, self.notes_nopb, self.nosupsub)
if css: if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f: with lopen(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8')) f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi) opf = OPFCreator(self.dest_dir, self.mi)
@ -374,7 +375,7 @@ class Convert(object):
guide.append(E.reference( guide.append(E.reference(
href='index.html#' + self.toc_anchor, title=_('Table of Contents'), type='toc')) href='index.html#' + self.toc_anchor, title=_('Table of Contents'), type='toc'))
toc_file = os.path.join(self.dest_dir, 'toc.ncx') toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx: with lopen(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx', process_guide=process_guide) opf.render(of, ncx, 'toc.ncx', process_guide=process_guide)
if os.path.getsize(toc_file) == 0: if os.path.getsize(toc_file) == 0:
os.remove(toc_file) os.remove(toc_file)
@ -656,7 +657,7 @@ class Convert(object):
anchor, name = self.footnotes.get_ref(child) anchor, name = self.footnotes.get_ref(child)
if anchor and name: if anchor and name:
l = A(SUP(name, id='back_%s' % anchor), href='#' + anchor, title=name) l = A(SUP(name, id='back_%s' % anchor), href='#' + anchor, title=name)
l.set('class', 'noteref') l.set('class', 'noteref calibre-docx-footnote-link')
text.add_elem(l) text.add_elem(l)
ans.append(text.elem) ans.append(text.elem)
elif self.namespace.is_tag(child, 'w:tab'): elif self.namespace.is_tag(child, 'w:tab'):
@ -764,6 +765,7 @@ class Convert(object):
if len(run) > 1: if len(run) > 1:
process_run(run) process_run(run)
if __name__ == '__main__': if __name__ == '__main__':
import shutil import shutil
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
@ -773,4 +775,3 @@ if __name__ == '__main__':
shutil.rmtree(dest_dir) shutil.rmtree(dest_dir)
os.mkdir(dest_dir) os.mkdir(dest_dir)
Convert(sys.argv[-1], dest_dir=dest_dir, log=default_log)() Convert(sys.argv[-1], dest_dir=dest_dir, log=default_log)()