Beautify CSS as well when beautify before diff option is used

This commit is contained in:
Kovid Goyal 2014-01-30 12:58:36 +05:30
parent efbab707db
commit cee4b97e2e
4 changed files with 24 additions and 8 deletions

View File

@ -14,6 +14,13 @@ from calibre import guess_type as _guess_type
def guess_type(x):
return _guess_type(x)[0] or 'application/octet-stream'
def setup_cssutils_serialization(tab_width=2):
import cssutils
prefs = cssutils.ser.prefs
prefs.indent = tab_width * ' '
prefs.indentClosingBrace = False
prefs.omitLastSemicolon = False
class PositionFinder(object):
def __init__(self, raw):

View File

@ -24,7 +24,7 @@ from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all
from calibre.ebooks.oeb.polish.replace import rename_files, replace_file, get_recommended_folders, rationalize_folders
from calibre.ebooks.oeb.polish.split import split, merge, AbortError, multisplit
from calibre.ebooks.oeb.polish.toc import remove_names_from_toc, find_existing_toc
from calibre.ebooks.oeb.polish.utils import link_stylesheets
from calibre.ebooks.oeb.polish.utils import link_stylesheets, setup_cssutils_serialization as scs
from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file
from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.tweak_book import set_current_container, current_container, tprefs, actions, editors
@ -54,11 +54,7 @@ def get_container(*args, **kwargs):
return container
def setup_cssutils_serialization():
import cssutils
prefs = cssutils.ser.prefs
prefs.indent = tprefs['editor_tab_stop_width'] * ' '
prefs.indentClosingBrace = False
prefs.omitLastSemicolon = False
scs(tprefs['editor_tab_stop_width'])
class BusyCursor(object):

View File

@ -257,7 +257,7 @@ class Diff(Dialog):
for i in (3, 5, 10, 50):
cm.addAction(_('Show %d lines of context') % i, partial(self.change_context, i))
cm.addAction(_('Show all text'), partial(self.change_context, None))
m.addAction(_('Beautify HTML/XML files before comparing them'), partial(self.change_beautify, True))
m.addAction(_('Beautify files before comparing them'), partial(self.change_beautify, True))
m.addMenu(cm)
l.addWidget(b, l.rowCount() - 1, l.columnCount(), 1, 1)

View File

@ -53,6 +53,19 @@ def beautify_text(raw, syntax):
if syntax == 'xml':
root = etree.fromstring(strip_encoding_declarations(raw))
pretty_xml_tree(root)
elif syntax == 'css':
import logging
from calibre.ebooks.oeb.base import serialize, _css_logger
from calibre.ebooks.oeb.polish.utils import setup_cssutils_serialization
from cssutils import CSSParser, log
setup_cssutils_serialization(tprefs['editor_tab_stop_width'])
log.setLevel(logging.WARN)
log.raiseExceptions = False
parser = CSSParser(loglevel=logging.WARNING,
# We dont care about @import rules
fetcher=lambda x: (None, None), log=_css_logger)
data = parser.parseString(raw, href='<string>', validate=False)
return serialize(data, 'text/css')
else:
root = parse(raw, line_numbers=False)
pretty_html_tree(None, root)
@ -668,7 +681,7 @@ class DiffSplit(QSplitter): # {{{
def add_text_diff(self, left_text, right_text, context, syntax, beautify=False):
left_text = unicodedata.normalize('NFC', left_text)
right_text = unicodedata.normalize('NFC', right_text)
if beautify and syntax in {'xml', 'html'}:
if beautify and syntax in {'xml', 'html', 'css'}:
left_text, right_text = beautify_text(left_text, syntax), beautify_text(right_text, syntax)
left_lines = self.left_lines = left_text.splitlines()
right_lines = self.right_lines = right_text.splitlines()