mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Edit Book: Allow customizing the templates used when creating new, blank HTML/CSS files via Preferences->Editor settings. Fixes #1348986 [Footnote pop-up - new issue](https://bugs.launchpad.net/calibre/+bug/1348986)
This commit is contained in:
parent
979eb5c86c
commit
bc5ab8eb13
@ -63,6 +63,7 @@ d['editor_format_toolbar'] = [('format-text-' + x) if x else x for x in (
|
|||||||
'justify-right', 'justify-fill')]
|
'justify-right', 'justify-fill')]
|
||||||
d['spell_check_case_sensitive_search'] = False
|
d['spell_check_case_sensitive_search'] = False
|
||||||
d['add_cover_preserve_aspect_ratio'] = False
|
d['add_cover_preserve_aspect_ratio'] = False
|
||||||
|
d['templates'] = {}
|
||||||
del d
|
del d
|
||||||
|
|
||||||
ucase_map = {l:string.ascii_uppercase[i] for i, l in enumerate(string.ascii_lowercase)}
|
ucase_map = {l:string.ascii_uppercase[i] for i, l in enumerate(string.ascii_lowercase)}
|
||||||
|
@ -18,13 +18,14 @@ from PyQt4.Qt import (
|
|||||||
QListWidgetItem, QIcon, QWidget, QSize, QFormLayout, Qt, QSpinBox,
|
QListWidgetItem, QIcon, QWidget, QSize, QFormLayout, Qt, QSpinBox,
|
||||||
QCheckBox, pyqtSignal, QDoubleSpinBox, QComboBox, QLabel, QFont,
|
QCheckBox, pyqtSignal, QDoubleSpinBox, QComboBox, QLabel, QFont,
|
||||||
QFontComboBox, QPushButton, QSizePolicy, QHBoxLayout, QGroupBox,
|
QFontComboBox, QPushButton, QSizePolicy, QHBoxLayout, QGroupBox,
|
||||||
QToolButton, QVBoxLayout, QSpacerItem)
|
QToolButton, QVBoxLayout, QSpacerItem, QTimer)
|
||||||
|
|
||||||
from calibre.gui2.keyboard import ShortcutConfig
|
from calibre.gui2.keyboard import ShortcutConfig
|
||||||
from calibre.gui2.tweak_book import tprefs, toolbar_actions, editor_toolbar_actions, actions
|
from calibre.gui2.tweak_book import tprefs, toolbar_actions, editor_toolbar_actions, actions
|
||||||
from calibre.gui2.tweak_book.editor.themes import default_theme, all_theme_names, ThemeEditor
|
from calibre.gui2.tweak_book.editor.themes import default_theme, all_theme_names, ThemeEditor
|
||||||
from calibre.gui2.tweak_book.spell import ManageDictionaries
|
from calibre.gui2.tweak_book.spell import ManageDictionaries
|
||||||
from calibre.gui2.font_family_chooser import FontFamilyChooser
|
from calibre.gui2.font_family_chooser import FontFamilyChooser
|
||||||
|
from calibre.gui2.tweak_book.widgets import Dialog
|
||||||
|
|
||||||
class BasicSettings(QWidget): # {{{
|
class BasicSettings(QWidget): # {{{
|
||||||
|
|
||||||
@ -200,6 +201,10 @@ class EditorSettings(BasicSettings):
|
|||||||
' time you open a HTML/CSS/etc. file for editing.'))
|
' time you open a HTML/CSS/etc. file for editing.'))
|
||||||
l.addRow(lw)
|
l.addRow(lw)
|
||||||
|
|
||||||
|
self.tb = b = QPushButton(_('Change &templates'))
|
||||||
|
l.addRow(_('Change the templates for creating new files:'), b)
|
||||||
|
b.clicked.connect(lambda : TemplatesDialog(self).exec_())
|
||||||
|
|
||||||
lw = self('inline_spell_check')
|
lw = self('inline_spell_check')
|
||||||
lw.setText(_('Show misspelled words underlined in the code view'))
|
lw.setText(_('Show misspelled words underlined in the code view'))
|
||||||
lw.setToolTip('<p>' + _(
|
lw.setToolTip('<p>' + _(
|
||||||
@ -513,6 +518,77 @@ class ToolbarSettings(QWidget):
|
|||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
class TemplatesDialog(Dialog): # {{{
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
self.ignore_changes = False
|
||||||
|
Dialog.__init__(self, _('Customize templates'), 'customize-templates', parent=parent)
|
||||||
|
|
||||||
|
def setup_ui(self):
|
||||||
|
from calibre.gui2.tweak_book.templates import DEFAULT_TEMPLATES
|
||||||
|
from calibre.gui2.tweak_book.editor.text import TextEdit
|
||||||
|
self.l = l = QFormLayout(self)
|
||||||
|
self.setLayout(l)
|
||||||
|
|
||||||
|
self.syntaxes = s = QComboBox(self)
|
||||||
|
s.addItems(sorted(DEFAULT_TEMPLATES.iterkeys()))
|
||||||
|
s.setCurrentIndex(s.findText('html'))
|
||||||
|
l.addRow(_('Choose the &type of template to edit:'), s)
|
||||||
|
s.currentIndexChanged.connect(self.show_template)
|
||||||
|
|
||||||
|
self.helpl = la = QLabel(_(
|
||||||
|
'The variables {0} and {1} will be replaced with the title and author of the book. {2}'
|
||||||
|
' is where the cursor will be positioned.').format('{TITLE}', '{AUTHOR}', '%CURSOR%'))
|
||||||
|
la.setWordWrap(True)
|
||||||
|
l.addRow(la)
|
||||||
|
|
||||||
|
self.save_timer = t = QTimer(self)
|
||||||
|
t.setSingleShot(True), t.setInterval(100)
|
||||||
|
t.timeout.connect(self._save_syntax)
|
||||||
|
|
||||||
|
self.editor = e = TextEdit(self)
|
||||||
|
l.addRow(e)
|
||||||
|
e.textChanged.connect(self.save_syntax)
|
||||||
|
|
||||||
|
self.show_template()
|
||||||
|
|
||||||
|
self.bb.clear()
|
||||||
|
self.bb.addButton(self.bb.Close)
|
||||||
|
self.rd = b = self.bb.addButton(self.bb.RestoreDefaults)
|
||||||
|
b.clicked.connect(self.restore_defaults)
|
||||||
|
l.addRow(self.bb)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_syntax(self):
|
||||||
|
return unicode(self.syntaxes.currentText())
|
||||||
|
|
||||||
|
def show_template(self):
|
||||||
|
from calibre.gui2.tweak_book.templates import raw_template_for
|
||||||
|
syntax = self.current_syntax
|
||||||
|
self.ignore_changes = True
|
||||||
|
try:
|
||||||
|
self.editor.load_text(raw_template_for(syntax), syntax=syntax)
|
||||||
|
finally:
|
||||||
|
self.ignore_changes = False
|
||||||
|
|
||||||
|
def save_syntax(self):
|
||||||
|
if self.ignore_changes:
|
||||||
|
return
|
||||||
|
self.save_timer.start()
|
||||||
|
|
||||||
|
def _save_syntax(self):
|
||||||
|
custom = tprefs['templates']
|
||||||
|
custom[self.current_syntax] = unicode(self.editor.toPlainText())
|
||||||
|
tprefs['templates'] = custom
|
||||||
|
|
||||||
|
def restore_defaults(self):
|
||||||
|
custom = tprefs['templates']
|
||||||
|
custom.pop(self.current_syntax, None)
|
||||||
|
tprefs['templates'] = custom
|
||||||
|
self.show_template()
|
||||||
|
self._save_syntax()
|
||||||
|
# }}}
|
||||||
|
|
||||||
class Preferences(QDialog):
|
class Preferences(QDialog):
|
||||||
|
|
||||||
def __init__(self, gui, initial_panel=None):
|
def __init__(self, gui, initial_panel=None):
|
||||||
|
@ -7,7 +7,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
from calibre import prepare_string_for_xml
|
from calibre import prepare_string_for_xml
|
||||||
from calibre.gui2.tweak_book import current_container
|
from calibre.gui2.tweak_book import current_container, tprefs
|
||||||
|
|
||||||
DEFAULT_TEMPLATES = {
|
DEFAULT_TEMPLATES = {
|
||||||
'html':
|
'html':
|
||||||
@ -33,12 +33,15 @@ DEFAULT_TEMPLATES = {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def raw_template_for(syntax):
|
||||||
|
return tprefs['templates'].get(syntax, DEFAULT_TEMPLATES.get(syntax, ''))
|
||||||
|
|
||||||
def template_for(syntax):
|
def template_for(syntax):
|
||||||
mi = current_container().mi
|
mi = current_container().mi
|
||||||
data = {
|
data = {
|
||||||
'TITLE':mi.title,
|
'TITLE':mi.title,
|
||||||
'AUTHOR': ' & '.join(mi.authors),
|
'AUTHOR': ' & '.join(mi.authors),
|
||||||
}
|
}
|
||||||
template = DEFAULT_TEMPLATES.get(syntax, '')
|
return raw_template_for(syntax).format(
|
||||||
return template.format(**{k:prepare_string_for_xml(v, True) for k, v in data.iteritems()})
|
**{k:prepare_string_for_xml(v, True) for k, v in data.iteritems()})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user