mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
TXT Input: Allow shipped markdown extensions to be selected and used.
This commit is contained in:
parent
ca0ee594f9
commit
a97c86bfe3
@ -47,8 +47,19 @@ class TXTInput(InputFormatPlugin):
|
|||||||
OptionRecommendation(name='txt_in_remove_indents', recommended_value=False,
|
OptionRecommendation(name='txt_in_remove_indents', recommended_value=False,
|
||||||
help=_('Normally extra space at the beginning of lines is retained. '
|
help=_('Normally extra space at the beginning of lines is retained. '
|
||||||
'With this option they will be removed.')),
|
'With this option they will be removed.')),
|
||||||
OptionRecommendation(name="markdown_disable_toc", recommended_value=False,
|
OptionRecommendation(name="markdown_extensions", recommended_value='footnotes, tables, toc',
|
||||||
help=_('Do not insert a Table of Contents into the output text.')),
|
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,
|
def convert(self, stream, options, file_ext, log,
|
||||||
@ -178,7 +189,7 @@ class TXTInput(InputFormatPlugin):
|
|||||||
if options.formatting_type == 'markdown':
|
if options.formatting_type == 'markdown':
|
||||||
log.debug('Running text through markdown conversion...')
|
log.debug('Running text through markdown conversion...')
|
||||||
try:
|
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:
|
except RuntimeError:
|
||||||
raise ValueError('This txt file has malformed markup, it cannot be'
|
raise ValueError('This txt file has malformed markup, it cannot be'
|
||||||
' converted by calibre. See http://daringfireball.net/projects/markdown/syntax')
|
' converted by calibre. See http://daringfireball.net/projects/markdown/syntax')
|
||||||
|
@ -17,6 +17,7 @@ from calibre.ebooks.conversion.preprocess import DocAnalysis
|
|||||||
from calibre.utils.cleantext import clean_ascii_chars
|
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>'
|
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):
|
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))
|
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
|
from calibre.ebooks.markdown import markdown
|
||||||
extensions=['footnotes', 'tables']
|
extensions = [x for x in extensions if x.lower() in VALID_MARKDOWN_EXTENSIONS]
|
||||||
if not disable_toc:
|
|
||||||
extensions.append('toc')
|
|
||||||
md = markdown.Markdown(
|
md = markdown.Markdown(
|
||||||
extensions,
|
extensions,
|
||||||
safe_mode=False)
|
safe_mode=False)
|
||||||
|
@ -16,7 +16,7 @@ class PluginWidget(Widget, Ui_Form):
|
|||||||
|
|
||||||
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
|
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
|
||||||
Widget.__init__(self, parent,
|
Widget.__init__(self, parent,
|
||||||
['paragraph_type', 'formatting_type', 'markdown_disable_toc',
|
['paragraph_type', 'formatting_type', 'markdown_extensions',
|
||||||
'preserve_spaces', 'txt_in_remove_indents'])
|
'preserve_spaces', 'txt_in_remove_indents'])
|
||||||
self.db, self.book_id = db, book_id
|
self.db, self.book_id = db, book_id
|
||||||
for x in get_option('paragraph_type').option.choices:
|
for x in get_option('paragraph_type').option.choices:
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>518</width>
|
<width>588</width>
|
||||||
<height>353</height>
|
<height>330</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
@ -97,8 +97,21 @@
|
|||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Markdown</string>
|
<string>Markdown</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QGridLayout" name="gridLayout_2">
|
||||||
<item>
|
<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">
|
<widget class="QLabel" name="label">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string><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>.</string>
|
<string><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>.</string>
|
||||||
@ -111,13 +124,6 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user