mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRY
This commit is contained in:
parent
ed50bc7ed5
commit
faf9fc0e1b
@ -11,8 +11,8 @@ import re
|
||||
from lxml.etree import XMLParser, fromstring, XMLSyntaxError
|
||||
import cssutils
|
||||
|
||||
from calibre import force_unicode
|
||||
from calibre.ebooks.html_entities import html5_entities
|
||||
from calibre.ebooks.oeb.polish.pretty import pretty_script_or_style as fix_style_tag
|
||||
from calibre.ebooks.oeb.polish.utils import PositionFinder
|
||||
from calibre.ebooks.oeb.polish.check.base import BaseError, WARN, ERROR
|
||||
from calibre.ebooks.oeb.base import OEB_DOCS
|
||||
@ -23,15 +23,6 @@ ALL_ENTITIES = HTML_ENTITTIES | XML_ENTITIES
|
||||
|
||||
replace_pat = re.compile('&(%s);' % '|'.join(re.escape(x) for x in sorted((HTML_ENTITTIES - XML_ENTITIES))))
|
||||
|
||||
def fix_style_tag(container, style):
|
||||
prev = style.getprevious()
|
||||
ws = style.getparent().text if prev is None else prev.tail
|
||||
ws = ws.splitlines()[-1]
|
||||
indent = ws[len(ws.rstrip()):]
|
||||
|
||||
sheet = container.parse_css(style.text)
|
||||
style.text = '\n' + force_unicode(sheet.cssText, 'utf-8') + '\n' + indent
|
||||
|
||||
class XMLParseError(BaseError):
|
||||
|
||||
is_parsing_error = True
|
||||
|
@ -150,6 +150,16 @@ def pretty_block(parent, level=1, indent=' '):
|
||||
child.tail = ''
|
||||
child.tail = child.tail + nn + (indent * l)
|
||||
|
||||
|
||||
def pretty_script_or_style(container, child):
|
||||
if child.text:
|
||||
indent = indent_for_tag(child)
|
||||
if child.tag.endswith('style'):
|
||||
child.text = force_unicode(pretty_css(container, '', child.text), 'utf-8')
|
||||
child.text = textwrap.dedent(child.text)
|
||||
child.text = '\n' + '\n'.join([(indent + x) if x else '' for x in child.text.splitlines()])
|
||||
set_indent(child, 'text', indent)
|
||||
|
||||
def pretty_html_tree(container, root):
|
||||
root.text = '\n\n'
|
||||
for child in root:
|
||||
@ -161,13 +171,7 @@ def pretty_html_tree(container, root):
|
||||
|
||||
# Handle <script> and <style> tags
|
||||
for child in root.xpath('//*[local-name()="script" or local-name()="style"]'):
|
||||
if child.text:
|
||||
indent = indent_for_tag(child)
|
||||
if child.tag.endswith('style'):
|
||||
child.text = force_unicode(pretty_css(container, '', child.text), 'utf-8')
|
||||
child.text = textwrap.dedent(child.text)
|
||||
child.text = '\n' + '\n'.join([(indent + x) if x else '' for x in child.text.splitlines()])
|
||||
set_indent(child, 'text', indent)
|
||||
pretty_script_or_style(container, child)
|
||||
|
||||
def fix_html(container, raw):
|
||||
root = container.parse_xhtml(raw)
|
||||
|
Loading…
x
Reference in New Issue
Block a user