Edit book: Add an option to automatically beautify individual files whenever they are opened for editing. Look under "Editor Settings" in the Edit Book Preferences.

This commit is contained in:
Kovid Goyal 2014-02-25 09:30:02 +05:30
parent 37fcd4093b
commit 22d59bac3c
5 changed files with 17 additions and 1 deletions

View File

@ -39,6 +39,7 @@ d['preview_minimum_font_size'] = 8
d['remove_existing_links_when_linking_sheets'] = True
d['charmap_favorites'] = list(map(ord, '\xa0\u2002\u2003\u2009\xad' '‘’“”‹›«»‚„' '—–§¶†‡©®™' '→⇒•·°±−×÷¼½½¾' '…µ¢£€¿¡¨´¸ˆ˜' 'ÀÁÂÃÄÅÆÇÈÉÊË' 'ÌÍÎÏÐÑÒÓÔÕÖØ' 'ŒŠÙÚÛÜÝŸÞßàá' 'âãäåæçèéêëìí' 'îïðñòóôõöøœš' 'ùúûüýÿþªºαΩ∞')) # noqa
d['folders_for_types'] = {'style':'styles', 'image':'images', 'font':'fonts', 'audio':'audio', 'video':'video'}
d['pretty_print_on_open'] = False
del d

View File

@ -1086,6 +1086,8 @@ class Boss(QObject):
data = use_template
editor = editors[name] = editor_from_syntax(syntax, self.gui.editor_tabs)
self.init_editor(name, editor, data, use_template=bool(use_template))
if tprefs['pretty_print_on_open']:
editor.pretty_print(name)
self.show_editor(name)
return editor

View File

@ -136,6 +136,9 @@ class Editor(QMainWindow):
def number_of_lines(self):
return 0
def pretty_print(self, name):
return False
def get_raw_data(self):
return self.canvas.get_image_data(quality=self.quality)

View File

@ -283,7 +283,10 @@ class Editor(QMainWindow):
from calibre.ebooks.oeb.polish.pretty import pretty_html, pretty_css, pretty_xml
if self.syntax in {'css', 'html', 'xml'}:
func = {'css':pretty_css, 'xml':pretty_xml}.get(self.syntax, pretty_html)
self.editor.replace_text(func(current_container(), name, unicode(self.editor.toPlainText())).decode('utf-8'))
original_text = unicode(self.editor.toPlainText())
prettied_text = func(current_container(), name, original_text).decode('utf-8')
if original_text != prettied_text:
self.editor.replace_text(prettied_text)
return True
return False

View File

@ -184,6 +184,13 @@ class EditorSettings(BasicSettings):
lw.setText(_('Show the name of the current character before the cursor along with the line and column number'))
l.addRow(lw)
lw = self('pretty_print_on_open')
lw.setText(_('Beautify individual files automatically when they are opened'))
lw.setToolTip('<p>' + _(
'This will cause the beautify current file action to be performed automatically every'
' time you open a HTML/CSS/etc. file for editing.'))
l.addRow(lw)
class IntegrationSettings(BasicSettings):
def __init__(self, parent=None):