From a97c86bfe3f0d6096444f435ee0f9340d58e3f85 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sun, 4 Aug 2013 16:33:24 -0400 Subject: [PATCH] TXT Input: Allow shipped markdown extensions to be selected and used. --- .../ebooks/conversion/plugins/txt_input.py | 17 +++++++++-- src/calibre/ebooks/txt/processor.py | 7 ++--- src/calibre/gui2/convert/txt_input.py | 2 +- src/calibre/gui2/convert/txt_input.ui | 28 +++++++++++-------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/txt_input.py b/src/calibre/ebooks/conversion/plugins/txt_input.py index a8e18aad11..876e2c381e 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_input.py +++ b/src/calibre/ebooks/conversion/plugins/txt_input.py @@ -47,8 +47,19 @@ class TXTInput(InputFormatPlugin): OptionRecommendation(name='txt_in_remove_indents', recommended_value=False, help=_('Normally extra space at the beginning of lines is retained. ' 'With this option they will be removed.')), - OptionRecommendation(name="markdown_disable_toc", recommended_value=False, - help=_('Do not insert a Table of Contents into the output text.')), + OptionRecommendation(name="markdown_extensions", recommended_value='footnotes, tables, toc', + help=_('Enable extensions to markdown syntax. Extensions are formatting that is not part ' + 'of the standard format.\n' + 'This should be a comma separated list of exentensions to enable:\n' + '* abbr: Abbreviations.\n' + '* def_list: Definition lists.\n' + '* fenced_code: Alternative code block syntax.\n' + '* footnotes: Footnotes\n.' + '* headerid: Allow ids as part of a header.\n' + '* meta: Metadata in the document.\n' + '* tables: Support tables.\n' + '* toc: Generate a table of contents.\n' + '* wikilinks: Wiki style links.\n')), ]) def convert(self, stream, options, file_ext, log, @@ -178,7 +189,7 @@ class TXTInput(InputFormatPlugin): if options.formatting_type == 'markdown': log.debug('Running text through markdown conversion...') try: - html = convert_markdown(txt, disable_toc=options.markdown_disable_toc) + html = convert_markdown(txt, extensions=[x for x in options.markdown_extensions.split(',') if x.strip()]) 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 0880eca4ca..cb132d374a 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -17,6 +17,7 @@ from calibre.ebooks.conversion.preprocess import DocAnalysis from calibre.utils.cleantext import clean_ascii_chars HTML_TEMPLATE = u'%s \n%s\n' +VALID_MARKDOWN_EXTENSIONS = ['abbr', 'def_list', 'fenced_code', 'footnotes', 'headerid', 'meta', 'tables', 'toc', 'wikilinks'] def clean_txt(txt): ''' @@ -95,11 +96,9 @@ def convert_basic(txt, title='', epub_split_size_kb=0): return HTML_TEMPLATE % (title, u'\n'.join(lines)) -def convert_markdown(txt, title='', disable_toc=False): +def convert_markdown(txt, title='', extensions=['footnotes', 'tables', 'toc']): from calibre.ebooks.markdown import markdown - extensions=['footnotes', 'tables'] - if not disable_toc: - extensions.append('toc') + extensions = [x for x in extensions if x.lower() in VALID_MARKDOWN_EXTENSIONS] md = markdown.Markdown( extensions, safe_mode=False) diff --git a/src/calibre/gui2/convert/txt_input.py b/src/calibre/gui2/convert/txt_input.py index acdf5f43c0..c4ef46a7e2 100644 --- a/src/calibre/gui2/convert/txt_input.py +++ b/src/calibre/gui2/convert/txt_input.py @@ -16,7 +16,7 @@ class PluginWidget(Widget, Ui_Form): def __init__(self, parent, get_option, get_help, db=None, book_id=None): Widget.__init__(self, parent, - ['paragraph_type', 'formatting_type', 'markdown_disable_toc', + ['paragraph_type', 'formatting_type', 'markdown_extensions', 'preserve_spaces', 'txt_in_remove_indents']) self.db, self.book_id = db, book_id for x in get_option('paragraph_type').option.choices: diff --git a/src/calibre/gui2/convert/txt_input.ui b/src/calibre/gui2/convert/txt_input.ui index 211b03294a..1d98eaccfb 100644 --- a/src/calibre/gui2/convert/txt_input.ui +++ b/src/calibre/gui2/convert/txt_input.ui @@ -6,8 +6,8 @@ 0 0 - 518 - 353 + 588 + 330 @@ -97,8 +97,21 @@ Markdown - - + + + + + + + + Extensions (comma separated): + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + <p>Markdown is a simple markup language for text files, that allows for advanced formatting. To learn more visit <a href="http://daringfireball.net/projects/markdown">markdown</a>. @@ -111,13 +124,6 @@ - - - - Do not insert Table of Contents into output text when using markdown - - -