diff --git a/src/calibre/gui2/convert/heuristics.py b/src/calibre/gui2/convert/heuristics.py new file mode 100644 index 0000000000..132652701a --- /dev/null +++ b/src/calibre/gui2/convert/heuristics.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2011, John Schember ' +__docformat__ = 'restructuredtext en' + +import re + +from calibre.gui2.convert.heuristics_ui import Ui_Form +from calibre.gui2.convert import Widget +from calibre.gui2 import error_dialog + +class HeuristicsWidget(Widget, Ui_Form): + + TITLE = _('Heuristics') + HELP = _('') + COMMIT_NAME = 'heuristics' + + def __init__(self, parent, get_option, get_help, db=None, book_id=None): + Widget.__init__(self, parent, + ['enable_heuristics', 'markup_chapter_headings', + 'italicize_common_cases', 'fix_indents', + 'html_unwrap_factor', 'unwrap_lines', + 'delete_blank_paragraphs', 'format_scene_breaks', + 'dehyphenate', + 'sr1_search', 'sr1_replace', + 'sr2_search', 'sr2_replace', + 'sr3_search', 'sr3_replace'] + ) + self.db, self.book_id = db, book_id + self.initialize_options(get_option, get_help, db, book_id) + self.opt_sr1_search.set_msg(_('Search regular expression 1:')) + self.opt_sr1_replace.set_msg(_('Replace regular expression 1:')) + self.opt_sr2_search.set_msg(_('Search regular expression 2:')) + self.opt_sr2_replace.set_msg(_('Replace regular expression 2:')) + self.opt_sr3_search.set_msg(_('Search regular expression 3:')) + self.opt_sr3_replace.set_msg(_('Replace regular expression 3:')) + + def break_cycles(self): + Widget.break_cycles(self) + self.opt_sr1_search.break_cycles() + self.opt_sr1_replace.break_cycles() + self.opt_sr2_search.break_cycles() + self.opt_sr2_replace.break_cycles() + self.opt_sr3_search.break_cycles() + self.opt_sr3_replace.break_cycles() + + def pre_commit_check(self): + for x in ('sr1-search', 'sr1-replace', 'sr2-search', 'sr2-replace', 'sr3-search', 'sr3-replace',): + x = getattr(self, 'opt_'+x) + try: + pat = unicode(x.regex) + re.compile(pat) + except Exception, err: + error_dialog(self, _('Invalid regular expression'), + _('Invalid regular expression: %s')%err).exec_() + return False + + def set_value_handler(self, g, val): + if val is None and g is self.opt_html_unwrap_factor: + g.setValue(0.0) + return True diff --git a/src/calibre/gui2/convert/heuristics.ui b/src/calibre/gui2/convert/heuristics.ui new file mode 100644 index 0000000000..2c103ff5b6 --- /dev/null +++ b/src/calibre/gui2/convert/heuristics.ui @@ -0,0 +1,219 @@ + + + Form + + + + 0 + 0 + 657 + 479 + + + + Form + + + + + + &Preprocess input file to possibly improve structure detection + + + + + + + Heuristics + + + + + + Unwrap lines + + + + + + + Qt::Horizontal + + + + 131 + 22 + + + + + + + + Line &un-wrap factor during preprocess: + + + opt_html_unwrap_factor + + + + + + + + + + 1.000000000000000 + + + 0.050000000000000 + + + 0.400000000000000 + + + + + + + markup_chapter_headings + + + + + + + Delete blank lines between paragraphs + + + + + + + format_scene_breaks + + + + + + + dehyphenate + + + + + + + italicize_common_cases + + + + + + + fix_indents + + + + + + + Qt::Vertical + + + + 131 + 95 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + Search and Replace + + + + + + + + + + + + + + + + + + + + + + + + + + + + RegexEdit + QWidget +
regex_builder.h
+ 1 +
+
+ + + + opt_enable_heuristics + toggled(bool) + opt_html_unwrap_factor + setEnabled(bool) + + + 328 + 87 + + + 481 + 113 + + + + + opt_enable_heuristics + toggled(bool) + huf_label + setEnabled(bool) + + + 295 + 88 + + + 291 + 105 + + + + +
diff --git a/src/calibre/gui2/convert/single.py b/src/calibre/gui2/convert/single.py index 7fa8c29835..0337b779a0 100644 --- a/src/calibre/gui2/convert/single.py +++ b/src/calibre/gui2/convert/single.py @@ -16,6 +16,7 @@ from calibre.ebooks.conversion.config import GuiRecommendations, save_specifics, from calibre.gui2.convert.single_ui import Ui_Dialog from calibre.gui2.convert.metadata import MetadataWidget from calibre.gui2.convert.look_and_feel import LookAndFeelWidget +from calibre.gui2.convert.heuristics import HeuristicsWidget from calibre.gui2.convert.page_setup import PageSetupWidget from calibre.gui2.convert.structure_detection import StructureDetectionWidget from calibre.gui2.convert.toc import TOCWidget @@ -170,6 +171,7 @@ class Config(ResizableDialog, Ui_Dialog): self.mw = widget_factory(MetadataWidget) self.setWindowTitle(_('Convert')+ ' ' + unicode(self.mw.title.text())) lf = widget_factory(LookAndFeelWidget) + hw = widget_factory(HeuristicsWidget) ps = widget_factory(PageSetupWidget) sd = widget_factory(StructureDetectionWidget) toc = widget_factory(TOCWidget) @@ -203,7 +205,7 @@ class Config(ResizableDialog, Ui_Dialog): if not c: break self.stack.removeWidget(c) - widgets = [self.mw, lf, ps, sd, toc] + widgets = [self.mw, lf, hw, ps, sd, toc] if input_widget is not None: widgets.append(input_widget) if output_widget is not None: diff --git a/src/calibre/gui2/convert/structure_detection.py b/src/calibre/gui2/convert/structure_detection.py index 3f350d4508..2c64303ee7 100644 --- a/src/calibre/gui2/convert/structure_detection.py +++ b/src/calibre/gui2/convert/structure_detection.py @@ -6,8 +6,6 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import re - from calibre.gui2.convert.structure_detection_ui import Ui_Form from calibre.gui2.convert import Widget from calibre.gui2 import error_dialog @@ -24,12 +22,8 @@ class StructureDetectionWidget(Widget, Ui_Form): Widget.__init__(self, parent, ['chapter', 'chapter_mark', 'remove_first_image', - 'insert_metadata', 'page_breaks_before', - 'preprocess_html', 'remove_header', 'header_regex', - 'remove_footer', 'footer_regex','html_unwrap_factor'] + 'insert_metadata', 'page_breaks_before'] ) - self.opt_html_unwrap_factor.setEnabled(False) - self.huf_label.setEnabled(False) self.db, self.book_id = db, book_id for x in ('pagebreak', 'rule', 'both', 'none'): self.opt_chapter_mark.addItem(x) @@ -37,28 +31,11 @@ class StructureDetectionWidget(Widget, Ui_Form): self.opt_chapter.set_msg(_('Detect chapters at (XPath expression):')) self.opt_page_breaks_before.set_msg(_('Insert page breaks before ' '(XPath expression):')) - self.opt_header_regex.set_msg(_('Header regular expression:')) - self.opt_header_regex.set_book_id(book_id) - self.opt_header_regex.set_db(db) - self.opt_footer_regex.set_msg(_('Footer regular expression:')) - self.opt_footer_regex.set_book_id(book_id) - self.opt_footer_regex.set_db(db) - + def break_cycles(self): Widget.break_cycles(self) - self.opt_header_regex.break_cycles() - self.opt_footer_regex.break_cycles() def pre_commit_check(self): - for x in ('header_regex', 'footer_regex'): - x = getattr(self, 'opt_'+x) - try: - pat = unicode(x.regex) - re.compile(pat) - except Exception, err: - error_dialog(self, _('Invalid regular expression'), - _('Invalid regular expression: %s')%err).exec_() - return False for x in ('chapter', 'page_breaks_before'): x = getattr(self, 'opt_'+x) if not x.check(): @@ -66,8 +43,3 @@ class StructureDetectionWidget(Widget, Ui_Form): _('The XPath expression %s is invalid.')%x.text).exec_() return False return True - - def set_value_handler(self, g, val): - if val is None and g is self.opt_html_unwrap_factor: - g.setValue(0.0) - return True diff --git a/src/calibre/gui2/convert/structure_detection.ui b/src/calibre/gui2/convert/structure_detection.ui index 21fe365e99..b690a68b0a 100644 --- a/src/calibre/gui2/convert/structure_detection.ui +++ b/src/calibre/gui2/convert/structure_detection.ui @@ -41,17 +41,17 @@ - + Insert &metadata as page at start of book - + - + Qt::Vertical @@ -64,72 +64,6 @@ - - - - Remove F&ooter - - - - - - - Remove H&eader - - - - - - - - - - - - - Line &un-wrap factor during preprocess: - - - opt_html_unwrap_factor - - - - - - - - - - 1.000000000000000 - - - 0.050000000000000 - - - 0.400000000000000 - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - &Preprocess input file to possibly improve structure detection - - - @@ -139,46 +73,7 @@
convert/xpath_wizard.h
1 - - RegexEdit - QWidget -
regex_builder.h
- 1 -
- - - opt_preprocess_html - toggled(bool) - opt_html_unwrap_factor - setEnabled(bool) - - - 328 - 87 - - - 481 - 113 - - - - - opt_preprocess_html - toggled(bool) - huf_label - setEnabled(bool) - - - 295 - 88 - - - 291 - 105 - - - - + diff --git a/src/calibre/gui2/preferences/conversion.py b/src/calibre/gui2/preferences/conversion.py index 0063d4a341..a20872cee0 100644 --- a/src/calibre/gui2/preferences/conversion.py +++ b/src/calibre/gui2/preferences/conversion.py @@ -12,6 +12,7 @@ from calibre.ebooks.conversion.plumber import Plumber from calibre.utils.logging import Log from calibre.gui2.preferences.conversion_ui import Ui_Form from calibre.gui2.convert.look_and_feel import LookAndFeelWidget +from calibre.gui2.convert.heuristics import HeuristicsWidget from calibre.gui2.convert.page_setup import PageSetupWidget from calibre.gui2.convert.structure_detection import StructureDetectionWidget from calibre.gui2.convert.toc import TOCWidget @@ -82,8 +83,8 @@ class Base(ConfigWidgetBase, Ui_Form): class CommonOptions(Base): def load_conversion_widgets(self): - self.conversion_widgets = [LookAndFeelWidget, PageSetupWidget, - StructureDetectionWidget, TOCWidget] + self.conversion_widgets = [LookAndFeelWidget, HeuristicsWidget, + PageSetupWidget, StructureDetectionWidget, TOCWidget] class InputOptions(Base):