From 70ac7517e433e711b6cd4b81c52c452540d7b9be Mon Sep 17 00:00:00 2001 From: "david.woods" Date: Sat, 9 Jan 2010 20:26:03 +0000 Subject: [PATCH] Gives option to disable Table of Contents for Markdown text input --- src/calibre/ebooks/markdown/mdx_toc.py | 31 ++++++++------ src/calibre/ebooks/txt/input.py | 4 +- src/calibre/ebooks/txt/processor.py | 15 +++++-- src/calibre/gui2/convert/txt_input.py | 2 +- src/calibre/gui2/convert/txt_input.ui | 58 ++++++++++++++++++-------- 5 files changed, 75 insertions(+), 35 deletions(-) diff --git a/src/calibre/ebooks/markdown/mdx_toc.py b/src/calibre/ebooks/markdown/mdx_toc.py index a8c1db267d..3ce25754e2 100644 --- a/src/calibre/ebooks/markdown/mdx_toc.py +++ b/src/calibre/ebooks/markdown/mdx_toc.py @@ -44,13 +44,13 @@ class TocExtension (markdown.Extension): replaces first string occurence of "///Table of Contents Goes Here///" """ - def __init__ (self) : + def __init__ (self, configs) : #maybe add these as parameters to the class init? self.TOC_INCLUDE_MARKER = "///Table of Contents///" self.TOC_TITLE = "Table Of Contents" self.auto_toc_heading_type=2 self.toc_heading_type=3 - + self.configs = configs def extendMarkdown(self, md, md_globals) : # Just insert in the end @@ -148,16 +148,23 @@ class TocPostprocessor (markdown.Postprocessor): def run(self, doc): tocPlaceholder = self.toc.findTocPlaceholder(doc) - tocDiv = self.toc.createTocDiv(doc) - if tocDiv: - if tocPlaceholder : - # Replace "magic" pattern with toc - tocPlaceholder.parent.replaceChild(tocPlaceholder, tocDiv) - else : - # Dump at the end of the DOM - # Probably want to use CSS to position div - doc.documentElement.appendChild(tocDiv) + if self.toc.configs["disable_toc"]: + if tocPlaceholder: + tocPlaceholder.parent.replaceChild(tocPlaceholder, "") + + else: + + tocDiv = self.toc.createTocDiv(doc) + + if tocDiv: + if tocPlaceholder : + # Replace "magic" pattern with toc + tocPlaceholder.parent.replaceChild(tocPlaceholder, tocDiv) + else : + # Dump at the end of the DOM + # Probably want to use CSS to position div + doc.documentElement.appendChild(tocDiv) def makeExtension(configs=None) : - return TocExtension() + return TocExtension(configs) diff --git a/src/calibre/ebooks/txt/input.py b/src/calibre/ebooks/txt/input.py index 278efd4847..a31fad43ee 100644 --- a/src/calibre/ebooks/txt/input.py +++ b/src/calibre/ebooks/txt/input.py @@ -31,6 +31,8 @@ class TXTInput(InputFormatPlugin): OptionRecommendation(name='markdown', recommended_value=False, help=_('Run the text input through the markdown pre-processor. To ' 'learn more about markdown see')+' http://daringfireball.net/projects/markdown/'), + OptionRecommendation(name="markdown_disable_toc", recommended_value=False, + help=_('Do not insert a Table of Contents.')), ]) def convert(self, stream, options, file_ext, log, @@ -50,7 +52,7 @@ class TXTInput(InputFormatPlugin): if options.markdown: log.debug('Running text though markdown conversion...') try: - html = convert_markdown(txt) + html = convert_markdown(txt, options.markdown_disable_toc) except RuntimeError: raise ValueError('This txt file has malformed markup, it cannot be' 'converted by calibre. See http://daringfireball.net/projects/markdown/syntax') diff --git a/src/calibre/ebooks/txt/processor.py b/src/calibre/ebooks/txt/processor.py index 9d0e1283c1..da840d3534 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -39,10 +39,17 @@ def convert_basic(txt, title=''): return HTML_TEMPLATE % (title, '\n'.join(lines)) -def convert_markdown(txt, title=''): - md = markdown.Markdown( - extensions=['footnotes', 'tables', 'toc'], - safe_mode=False,) +def convert_markdown(txt, disable_toc, title=''): + if disable_toc: + md = markdown.Markdown( + extensions=['footnotes', 'tables', 'toc'], + extension_configs={"toc": {"disable_toc": True}}, + safe_mode=False) + else: + md = markdown.Markdown( + extensions=['footnotes', 'tables', 'toc'], + extension_configs={"toc": {"disable_toc": False}}, + safe_mode=False) return HTML_TEMPLATE % (title, md.convert(txt)) def separate_paragraphs_single_line(txt): diff --git a/src/calibre/gui2/convert/txt_input.py b/src/calibre/gui2/convert/txt_input.py index 505a916f81..7fe6f6274c 100644 --- a/src/calibre/gui2/convert/txt_input.py +++ b/src/calibre/gui2/convert/txt_input.py @@ -14,6 +14,6 @@ class PluginWidget(Widget, Ui_Form): def __init__(self, parent, get_option, get_help, db=None, book_id=None): Widget.__init__(self, parent, 'txt_input', - ['single_line_paras', 'print_formatted_paras', 'markdown']) + ['single_line_paras', 'print_formatted_paras', 'markdown', 'markdown_disable_toc']) self.db, self.book_id = db, book_id self.initialize_options(get_option, get_help, db, book_id) diff --git a/src/calibre/gui2/convert/txt_input.ui b/src/calibre/gui2/convert/txt_input.ui index 94cc1ad25f..69a7fe06aa 100644 --- a/src/calibre/gui2/convert/txt_input.ui +++ b/src/calibre/gui2/convert/txt_input.ui @@ -14,19 +14,6 @@ Form - - - - Qt::Vertical - - - - 20 - 213 - - - - @@ -34,6 +21,13 @@ + + + + Assume print formatting + + + @@ -51,15 +45,45 @@ - - + + - Assume print formatting + Disable Table of Contents + + + + Qt::Vertical + + + + 20 + 213 + + + + - + + + opt_markdown + toggled(bool) + opt_markdown_disable_toc + setEnabled(bool) + + + 76 + 80 + + + 418 + 105 + + + +