TXT Input: Add option to disable insertion of Table of Contents into output text. Implement #4506 (Option to disable generation of Table of Contents on Markdown input)

This commit is contained in:
Kovid Goyal 2010-01-09 14:03:48 -07:00
commit 5b4b130468
6 changed files with 69 additions and 36 deletions

View File

@ -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,22 @@ 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.get("disable_toc", False):
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()
def makeExtension(configs={}):
return TocExtension(configs=configs)

View File

@ -960,7 +960,7 @@ class Manifest(object):
else:
title = _('Unknown')
return self._parse_xhtml(convert_markdown(data, title))
return self._parse_xhtml(convert_markdown(data, title=title))
def _parse_css(self, data):

View File

@ -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 into the output text.')),
])
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, disable_toc=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')

View File

@ -39,10 +39,11 @@ def convert_basic(txt, title=''):
return HTML_TEMPLATE % (title, '\n'.join(lines))
def convert_markdown(txt, title=''):
def convert_markdown(txt, title='', disable_toc=False):
md = markdown.Markdown(
extensions=['footnotes', 'tables', 'toc'],
safe_mode=False,)
extensions=['footnotes', 'tables', 'toc'],
extension_configs={"toc": {"disable_toc": disable_toc}},
safe_mode=False)
return HTML_TEMPLATE % (title, md.convert(txt))
def separate_paragraphs_single_line(txt):

View File

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

View File

@ -14,19 +14,6 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>213</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="opt_single_line_paras">
<property name="text">
@ -34,6 +21,13 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="opt_print_formatted_paras">
<property name="text">
<string>Assume print formatting</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="opt_markdown">
<property name="text">
@ -51,15 +45,45 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="opt_print_formatted_paras">
<item row="4" column="0">
<widget class="QCheckBox" name="opt_markdown_disable_toc">
<property name="text">
<string>Assume print formatting</string>
<string>Do not insert Table of Contents into output text when using markdown</string>
</property>
</widget>
</item>
<item row="5" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>213</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
<connections>
<connection>
<sender>opt_markdown</sender>
<signal>toggled(bool)</signal>
<receiver>opt_markdown_disable_toc</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>76</x>
<y>80</y>
</hint>
<hint type="destinationlabel">
<x>418</x>
<y>105</y>
</hint>
</hints>
</connection>
</connections>
</ui>