mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DOCX Output: Handle invisible HTML elements
This commit is contained in:
parent
51a08167db
commit
a403ec4351
@ -12,7 +12,7 @@ from lxml import etree
|
|||||||
from lxml.builder import ElementMaker
|
from lxml.builder import ElementMaker
|
||||||
|
|
||||||
from calibre.ebooks.docx.names import namespaces
|
from calibre.ebooks.docx.names import namespaces
|
||||||
from calibre.ebooks.docx.styles import w, BlockStyle, TextStyle
|
from calibre.ebooks.docx.writer.styles import w, BlockStyle, TextStyle
|
||||||
from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
|
from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
|
||||||
from calibre.ebooks.oeb.base import XPath, barename
|
from calibre.ebooks.oeb.base import XPath, barename
|
||||||
|
|
||||||
@ -138,8 +138,11 @@ class Convert(object):
|
|||||||
self.process_block(body, b, stylizer, ignore_tail=True)
|
self.process_block(body, b, stylizer, ignore_tail=True)
|
||||||
|
|
||||||
def process_block(self, html_block, docx_block, stylizer, ignore_tail=False):
|
def process_block(self, html_block, docx_block, stylizer, ignore_tail=False):
|
||||||
|
block_style = stylizer.style(html_block)
|
||||||
|
if block_style.is_hidden:
|
||||||
|
return
|
||||||
if html_block.text:
|
if html_block.text:
|
||||||
docx_block.add_text(html_block.text, stylizer.style(html_block), ignore_leading_whitespace=True)
|
docx_block.add_text(html_block.text, block_style, ignore_leading_whitespace=True)
|
||||||
|
|
||||||
for child in html_block.iterchildren(etree.Element):
|
for child in html_block.iterchildren(etree.Element):
|
||||||
tag = barename(child.tag)
|
tag = barename(child.tag)
|
||||||
@ -157,15 +160,17 @@ class Convert(object):
|
|||||||
if ignore_tail is False and html_block.tail and html_block.tail.strip():
|
if ignore_tail is False and html_block.tail and html_block.tail.strip():
|
||||||
b = docx_block
|
b = docx_block
|
||||||
if b is not self.blocks[-1]:
|
if b is not self.blocks[-1]:
|
||||||
b = Block(html_block, stylizer.style(html_block))
|
b = Block(html_block, block_style)
|
||||||
self.blocks.append(b)
|
self.blocks.append(b)
|
||||||
b.add_text(html_block.tail, stylizer.style(html_block.getparent()))
|
b.add_text(html_block.tail, stylizer.style(html_block.getparent()))
|
||||||
|
|
||||||
def process_inline(self, html_child, docx_block, stylizer):
|
def process_inline(self, html_child, docx_block, stylizer):
|
||||||
tag = barename(html_child.tag)
|
tag = barename(html_child.tag)
|
||||||
|
style = stylizer.style(html_child)
|
||||||
|
if style.is_hidden:
|
||||||
|
return
|
||||||
if tag == 'img':
|
if tag == 'img':
|
||||||
return # TODO: Handle images
|
return # TODO: Handle images
|
||||||
style = stylizer.style(html_child)
|
|
||||||
if html_child.text:
|
if html_child.text:
|
||||||
docx_block.add_text(html_child.text, style, html_parent=html_child)
|
docx_block.add_text(html_child.text, style, html_parent=html_child)
|
||||||
for child in html_child.iterchildren(etree.Element):
|
for child in html_child.iterchildren(etree.Element):
|
||||||
|
@ -737,3 +737,7 @@ class Style(object):
|
|||||||
css = self._pseudo_classes
|
css = self._pseudo_classes
|
||||||
return {k:v for k, v in css.iteritems() if v}
|
return {k:v for k, v in css.iteritems() if v}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_hidden(self):
|
||||||
|
return self._style.get('display') == 'none' or self._style.get('visibility') == 'hidden'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user