From 241ef0b6e1135c33e2b61c6a75d4a65a6eaeb5a4 Mon Sep 17 00:00:00 2001 From: John Schember Date: Mon, 31 Jan 2011 19:57:43 -0500 Subject: [PATCH 1/2] Heuristics: Add replace soft scene break option. --- src/calibre/ebooks/conversion/cli.py | 3 +- src/calibre/ebooks/conversion/plumber.py | 4 +++ src/calibre/gui2/convert/heuristics.py | 45 +++++++++++++++++++++++- src/calibre/gui2/convert/heuristics.ui | 36 +++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index 33ae61f16a..25179d48a7 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -45,7 +45,8 @@ For full documentation of the conversion system see HEURISTIC_OPTIONS = ['markup_chapter_headings', 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', - 'delete_blank_paragraphs', 'format_scene_breaks', + 'delete_blank_paragraphs', + 'format_scene_breaks', 'replace_soft_scene_breaks', 'dehyphenate', 'renumber_headings'] def print_help(parser, log): diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 5807ba5f8f..2c37053759 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -530,6 +530,10 @@ OptionRecommendation(name='format_scene_breaks', help=_('Left aligned scene break markers are center aligned. ' 'Replace soft scene breaks that use multiple blank lines with' 'horizontal rules.')), + +OptionRecommendation(name='replace_soft_scene_breaks', + recommended_value='', level=OptionRecommendation.LOW, + help=_('Replace soft scene breaks with the specified text.')), OptionRecommendation(name='dehyphenate', recommended_value=True, level=OptionRecommendation.LOW, diff --git a/src/calibre/gui2/convert/heuristics.py b/src/calibre/gui2/convert/heuristics.py index e788888257..73b4622246 100644 --- a/src/calibre/gui2/convert/heuristics.py +++ b/src/calibre/gui2/convert/heuristics.py @@ -6,6 +6,7 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import Qt +from calibre.gui2 import gprefs from calibre.gui2.convert.heuristics_ui import Ui_Form from calibre.gui2.convert import Widget @@ -21,17 +22,35 @@ class HeuristicsWidget(Widget, Ui_Form): ['enable_heuristics', 'markup_chapter_headings', 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', - 'delete_blank_paragraphs', 'format_scene_breaks', + 'delete_blank_paragraphs', + 'format_scene_breaks', 'replace_soft_scene_breaks', 'dehyphenate', 'renumber_headings'] ) self.db, self.book_id = db, book_id + self.rssb_defaults = ['', '
', '* * *'] self.initialize_options(get_option, get_help, db, book_id) + self.load_histories() + self.opt_enable_heuristics.stateChanged.connect(self.enable_heuristics) self.opt_unwrap_lines.stateChanged.connect(self.enable_unwrap) self.enable_heuristics(self.opt_enable_heuristics.checkState()) + def restore_defaults(self, get_option): + Widget.restore_defaults(self, get_option) + + rssb_hist = gprefs['replace_soft_scene_breaks_history'] + for x in self.rssb_defaults: + if x in rssb_hist: + del rssb_hist[rssb_hist.index(x)] + gprefs['replace_soft_scene_breaks_history'] = self.rssb_defaults + gprefs['replace_soft_scene_breaks_history'] + + def commit_options(self, save_defaults=False): + Widget.commit_options(self, save_defaults) + + self.save_histories() + def break_cycles(self): Widget.break_cycles(self) @@ -45,6 +64,30 @@ class HeuristicsWidget(Widget, Ui_Form): if val is None and g is self.opt_html_unwrap_factor: g.setValue(0.0) return True + if not val and g is self.opt_replace_soft_scene_breaks: + g.lineEdit().setText('') + return True + + def load_histories(self): + val = unicode(self.opt_replace_soft_scene_breaks.currentText()) + rssb_hist = gprefs.get('replace_soft_scene_breaks_history', self.rssb_defaults) + if val in rssb_hist: + del rssb_hist[rssb_hist.index(val)] + rssb_hist.insert(0, val) + for v in rssb_hist: + # Ensure we don't have duplicate items. + if self.opt_replace_soft_scene_breaks.findText(v) == -1: + self.opt_replace_soft_scene_breaks.addItem(v) + self.opt_replace_soft_scene_breaks.setCurrentIndex(0) + + def save_histories(self): + rssb_history = [] + history_pats = [unicode(self.opt_replace_soft_scene_breaks.lineEdit().text())] + [unicode(self.opt_replace_soft_scene_breaks.itemText(i)) for i in xrange(self.opt_replace_soft_scene_breaks.count())] + for p in history_pats[:10]: + # Ensure we don't have duplicate items. + if p not in rssb_history: + rssb_history.append(p) + gprefs['replace_soft_scene_breaks_history'] = rssb_history def enable_heuristics(self, state): state = state == Qt.Checked diff --git a/src/calibre/gui2/convert/heuristics.ui b/src/calibre/gui2/convert/heuristics.ui index 6863fcf8e6..c047957d4d 100644 --- a/src/calibre/gui2/convert/heuristics.ui +++ b/src/calibre/gui2/convert/heuristics.ui @@ -150,6 +150,42 @@ + + + + QLayout::SetDefaultConstraint + + + + + + 0 + 0 + + + + Replace soft scene breaks: + + + + + + + + 0 + 0 + + + + true + + + QComboBox::InsertAtTop + + + + + From c4f74eb182eab41b749ddb814791c55dc260f1bd Mon Sep 17 00:00:00 2001 From: John Schember Date: Tue, 1 Feb 2011 07:29:01 -0500 Subject: [PATCH 2/2] Heuristics: Rename scene break option. Fix bug preventing saving settings. --- src/calibre/ebooks/conversion/cli.py | 5 ++--- src/calibre/ebooks/conversion/plumber.py | 4 ++-- src/calibre/gui2/convert/heuristics.py | 26 ++++++++++++------------ src/calibre/gui2/convert/heuristics.ui | 2 +- src/calibre/gui2/convert/single.py | 1 + 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index 25179d48a7..b3d2f8cac5 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -45,8 +45,7 @@ For full documentation of the conversion system see HEURISTIC_OPTIONS = ['markup_chapter_headings', 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', - 'delete_blank_paragraphs', - 'format_scene_breaks', 'replace_soft_scene_breaks', + 'delete_blank_paragraphs', 'format_scene_breaks', 'dehyphenate', 'renumber_headings'] def print_help(parser, log): @@ -144,7 +143,7 @@ def add_pipeline_options(parser, plumber): ' patterns. Disabled by default. Use %s to enable. ' ' Individual actions can be disabled with the %s options.') % ('--enable-heuristics', '--disable-*'), - ['enable_heuristics'] + HEURISTIC_OPTIONS + ['enable_heuristics', 'replace_scene_breaks'] + HEURISTIC_OPTIONS ), 'SEARCH AND REPLACE' : ( diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 2c37053759..a4708d398c 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -531,9 +531,9 @@ OptionRecommendation(name='format_scene_breaks', 'Replace soft scene breaks that use multiple blank lines with' 'horizontal rules.')), -OptionRecommendation(name='replace_soft_scene_breaks', +OptionRecommendation(name='replace_scene_breaks', recommended_value='', level=OptionRecommendation.LOW, - help=_('Replace soft scene breaks with the specified text.')), + help=_('Replace scene breaks with the specified text.')), OptionRecommendation(name='dehyphenate', recommended_value=True, level=OptionRecommendation.LOW, diff --git a/src/calibre/gui2/convert/heuristics.py b/src/calibre/gui2/convert/heuristics.py index 73b4622246..8ca4cab455 100644 --- a/src/calibre/gui2/convert/heuristics.py +++ b/src/calibre/gui2/convert/heuristics.py @@ -23,7 +23,7 @@ class HeuristicsWidget(Widget, Ui_Form): 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', 'delete_blank_paragraphs', - 'format_scene_breaks', 'replace_soft_scene_breaks', + 'format_scene_breaks', 'replace_scene_breaks', 'dehyphenate', 'renumber_headings'] ) self.db, self.book_id = db, book_id @@ -40,16 +40,16 @@ class HeuristicsWidget(Widget, Ui_Form): def restore_defaults(self, get_option): Widget.restore_defaults(self, get_option) - rssb_hist = gprefs['replace_soft_scene_breaks_history'] + rssb_hist = gprefs['replace_scene_breaks_history'] for x in self.rssb_defaults: if x in rssb_hist: del rssb_hist[rssb_hist.index(x)] - gprefs['replace_soft_scene_breaks_history'] = self.rssb_defaults + gprefs['replace_soft_scene_breaks_history'] + gprefs['replace_scene_breaks_history'] = self.rssb_defaults + gprefs['replace_scene_breaks_history'] def commit_options(self, save_defaults=False): - Widget.commit_options(self, save_defaults) - self.save_histories() + + return Widget.commit_options(self, save_defaults) def break_cycles(self): Widget.break_cycles(self) @@ -64,30 +64,30 @@ class HeuristicsWidget(Widget, Ui_Form): if val is None and g is self.opt_html_unwrap_factor: g.setValue(0.0) return True - if not val and g is self.opt_replace_soft_scene_breaks: + if not val and g is self.opt_replace_scene_breaks: g.lineEdit().setText('') return True def load_histories(self): - val = unicode(self.opt_replace_soft_scene_breaks.currentText()) - rssb_hist = gprefs.get('replace_soft_scene_breaks_history', self.rssb_defaults) + val = unicode(self.opt_replace_scene_breaks.currentText()) + rssb_hist = gprefs.get('replace_scene_breaks_history', self.rssb_defaults) if val in rssb_hist: del rssb_hist[rssb_hist.index(val)] rssb_hist.insert(0, val) for v in rssb_hist: # Ensure we don't have duplicate items. - if self.opt_replace_soft_scene_breaks.findText(v) == -1: - self.opt_replace_soft_scene_breaks.addItem(v) - self.opt_replace_soft_scene_breaks.setCurrentIndex(0) + if self.opt_replace_scene_breaks.findText(v) == -1: + self.opt_replace_scene_breaks.addItem(v) + self.opt_replace_scene_breaks.setCurrentIndex(0) def save_histories(self): rssb_history = [] - history_pats = [unicode(self.opt_replace_soft_scene_breaks.lineEdit().text())] + [unicode(self.opt_replace_soft_scene_breaks.itemText(i)) for i in xrange(self.opt_replace_soft_scene_breaks.count())] + history_pats = [unicode(self.opt_replace_scene_breaks.lineEdit().text())] + [unicode(self.opt_replace_scene_breaks.itemText(i)) for i in xrange(self.opt_replace_scene_breaks.count())] for p in history_pats[:10]: # Ensure we don't have duplicate items. if p not in rssb_history: rssb_history.append(p) - gprefs['replace_soft_scene_breaks_history'] = rssb_history + gprefs['replace_scene_breaks_history'] = rssb_history def enable_heuristics(self, state): state = state == Qt.Checked diff --git a/src/calibre/gui2/convert/heuristics.ui b/src/calibre/gui2/convert/heuristics.ui index c047957d4d..4f7cf5ea6e 100644 --- a/src/calibre/gui2/convert/heuristics.ui +++ b/src/calibre/gui2/convert/heuristics.ui @@ -169,7 +169,7 @@ - + 0 diff --git a/src/calibre/gui2/convert/single.py b/src/calibre/gui2/convert/single.py index 59fcbb65ad..6540383229 100644 --- a/src/calibre/gui2/convert/single.py +++ b/src/calibre/gui2/convert/single.py @@ -258,6 +258,7 @@ class Config(ResizableDialog, Ui_Dialog): if not w.pre_commit_check(): return x = w.commit(save_defaults=False) + print x recs.update(x) self.opf_file, self.cover_file = self.mw.opf_file, self.mw.cover_file self._recommendations = recs