From faf9fc0e1beabcc19c81d4d3498cae66dc742717 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 10 Dec 2013 22:30:55 +0530 Subject: [PATCH] DRY --- src/calibre/ebooks/oeb/polish/check/parsing.py | 11 +---------- src/calibre/ebooks/oeb/polish/pretty.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/check/parsing.py b/src/calibre/ebooks/oeb/polish/check/parsing.py index c7b627119a..303c6237c6 100644 --- a/src/calibre/ebooks/oeb/polish/check/parsing.py +++ b/src/calibre/ebooks/oeb/polish/check/parsing.py @@ -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 diff --git a/src/calibre/ebooks/oeb/polish/pretty.py b/src/calibre/ebooks/oeb/polish/pretty.py index 94db83571d..4cf4e4e4c9 100644 --- a/src/calibre/ebooks/oeb/polish/pretty.py +++ b/src/calibre/ebooks/oeb/polish/pretty.py @@ -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