Gives option to disable Table of Contents for Markdown text input

This commit is contained in:
david.woods 2010-01-09 20:26:03 +00:00
parent 536e629356
commit 70ac7517e4
5 changed files with 75 additions and 35 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,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)

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.')),
])
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')

View File

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

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>Disable Table of Contents</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>