TXT Input: Allow shipped markdown extensions to be selected and used.

This commit is contained in:
John Schember 2013-08-04 16:33:24 -04:00
parent ca0ee594f9
commit a97c86bfe3
4 changed files with 35 additions and 19 deletions

View File

@ -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')

View File

@ -17,6 +17,7 @@ from calibre.ebooks.conversion.preprocess import DocAnalysis
from calibre.utils.cleantext import clean_ascii_chars
HTML_TEMPLATE = u'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s </title></head><body>\n%s\n</body></html>'
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)

View File

@ -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:

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<height>353</height>
<width>588</width>
<height>330</height>
</rect>
</property>
<property name="windowTitle">
@ -97,8 +97,21 @@
<property name="title">
<string>Markdown</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<widget class="QLineEdit" name="opt_markdown_extensions"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Extensions (comma separated):</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;p&gt;Markdown is a simple markup language for text files, that allows for advanced formatting. To learn more visit &lt;a href=&quot;http://daringfireball.net/projects/markdown&quot;&gt;markdown&lt;/a&gt;.</string>
@ -111,13 +124,6 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="opt_markdown_disable_toc">
<property name="text">
<string>Do not insert Table of Contents into output text when using markdown</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>