mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
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:
commit
5b4b130468
@ -44,13 +44,13 @@ class TocExtension (markdown.Extension):
|
|||||||
replaces first string occurence of "///Table of Contents Goes Here///"
|
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?
|
#maybe add these as parameters to the class init?
|
||||||
self.TOC_INCLUDE_MARKER = "///Table of Contents///"
|
self.TOC_INCLUDE_MARKER = "///Table of Contents///"
|
||||||
self.TOC_TITLE = "Table Of Contents"
|
self.TOC_TITLE = "Table Of Contents"
|
||||||
self.auto_toc_heading_type=2
|
self.auto_toc_heading_type=2
|
||||||
self.toc_heading_type=3
|
self.toc_heading_type=3
|
||||||
|
self.configs = configs
|
||||||
|
|
||||||
def extendMarkdown(self, md, md_globals) :
|
def extendMarkdown(self, md, md_globals) :
|
||||||
# Just insert in the end
|
# Just insert in the end
|
||||||
@ -148,16 +148,22 @@ class TocPostprocessor (markdown.Postprocessor):
|
|||||||
def run(self, doc):
|
def run(self, doc):
|
||||||
tocPlaceholder = self.toc.findTocPlaceholder(doc)
|
tocPlaceholder = self.toc.findTocPlaceholder(doc)
|
||||||
|
|
||||||
tocDiv = self.toc.createTocDiv(doc)
|
if self.toc.configs.get("disable_toc", False):
|
||||||
if tocDiv:
|
if tocPlaceholder:
|
||||||
if tocPlaceholder :
|
tocPlaceholder.parent.replaceChild(tocPlaceholder, "")
|
||||||
# Replace "magic" pattern with toc
|
else:
|
||||||
tocPlaceholder.parent.replaceChild(tocPlaceholder, tocDiv)
|
|
||||||
else :
|
tocDiv = self.toc.createTocDiv(doc)
|
||||||
# Dump at the end of the DOM
|
|
||||||
# Probably want to use CSS to position div
|
if tocDiv:
|
||||||
doc.documentElement.appendChild(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) :
|
def makeExtension(configs={}):
|
||||||
return TocExtension()
|
return TocExtension(configs=configs)
|
||||||
|
@ -960,7 +960,7 @@ class Manifest(object):
|
|||||||
else:
|
else:
|
||||||
title = _('Unknown')
|
title = _('Unknown')
|
||||||
|
|
||||||
return self._parse_xhtml(convert_markdown(data, title))
|
return self._parse_xhtml(convert_markdown(data, title=title))
|
||||||
|
|
||||||
|
|
||||||
def _parse_css(self, data):
|
def _parse_css(self, data):
|
||||||
|
@ -31,6 +31,8 @@ class TXTInput(InputFormatPlugin):
|
|||||||
OptionRecommendation(name='markdown', recommended_value=False,
|
OptionRecommendation(name='markdown', recommended_value=False,
|
||||||
help=_('Run the text input through the markdown pre-processor. To '
|
help=_('Run the text input through the markdown pre-processor. To '
|
||||||
'learn more about markdown see')+' http://daringfireball.net/projects/markdown/'),
|
'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,
|
def convert(self, stream, options, file_ext, log,
|
||||||
@ -50,7 +52,7 @@ class TXTInput(InputFormatPlugin):
|
|||||||
if options.markdown:
|
if options.markdown:
|
||||||
log.debug('Running text though markdown conversion...')
|
log.debug('Running text though markdown conversion...')
|
||||||
try:
|
try:
|
||||||
html = convert_markdown(txt)
|
html = convert_markdown(txt, disable_toc=options.markdown_disable_toc)
|
||||||
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')
|
||||||
|
@ -39,10 +39,11 @@ def convert_basic(txt, title=''):
|
|||||||
|
|
||||||
return HTML_TEMPLATE % (title, '\n'.join(lines))
|
return HTML_TEMPLATE % (title, '\n'.join(lines))
|
||||||
|
|
||||||
def convert_markdown(txt, title=''):
|
def convert_markdown(txt, title='', disable_toc=False):
|
||||||
md = markdown.Markdown(
|
md = markdown.Markdown(
|
||||||
extensions=['footnotes', 'tables', 'toc'],
|
extensions=['footnotes', 'tables', 'toc'],
|
||||||
safe_mode=False,)
|
extension_configs={"toc": {"disable_toc": disable_toc}},
|
||||||
|
safe_mode=False)
|
||||||
return HTML_TEMPLATE % (title, md.convert(txt))
|
return HTML_TEMPLATE % (title, md.convert(txt))
|
||||||
|
|
||||||
def separate_paragraphs_single_line(txt):
|
def separate_paragraphs_single_line(txt):
|
||||||
|
@ -14,6 +14,6 @@ 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, 'txt_input',
|
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.db, self.book_id = db, book_id
|
||||||
self.initialize_options(get_option, get_help, db, book_id)
|
self.initialize_options(get_option, get_help, db, book_id)
|
||||||
|
@ -14,19 +14,6 @@
|
|||||||
<string>Form</string>
|
<string>Form</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<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">
|
<item row="0" column="0">
|
||||||
<widget class="QCheckBox" name="opt_single_line_paras">
|
<widget class="QCheckBox" name="opt_single_line_paras">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@ -34,6 +21,13 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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">
|
<item row="2" column="0">
|
||||||
<widget class="QCheckBox" name="opt_markdown">
|
<widget class="QCheckBox" name="opt_markdown">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@ -51,15 +45,45 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="4" column="0">
|
||||||
<widget class="QCheckBox" name="opt_print_formatted_paras">
|
<widget class="QCheckBox" name="opt_markdown_disable_toc">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Assume print formatting</string>
|
<string>Do not insert Table of Contents into output text when using markdown</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
<resources/>
|
<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>
|
</ui>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user