mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-05-08 20:35:18 +02:00
FB2 Output: Add option to create FB2 sections based on internal file structure of input file (useful for EPUB files that have been split on chapter boundaries). Also add options to mark h1/h2/h3 tags as section titles in the FB2 file. Fixes #7738 (FB2 Output option to create section per HTML file). PMLZ Output: Various bug fixes [7742, 7745]
This commit is contained in:
commit
532ecccf84
5 changed files with 64 additions and 6 deletions
|
|
@ -91,6 +91,10 @@ def fb2mlize_spine(self):
|
|||
return u'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True)
|
||||
|
||||
def clean_text(self, text):
|
||||
text = re.sub(r'(?miu)<section>\s*</section>', '', text)
|
||||
text = re.sub(r'(?miu)\s+</section>', '</section>', text)
|
||||
text = re.sub(r'(?miu)</section><section>', '</section>\n\n<section>', text)
|
||||
|
||||
text = re.sub(r'(?miu)<p>\s*</p>', '', text)
|
||||
text = re.sub(r'(?miu)\s+</p>', '</p>', text)
|
||||
text = re.sub(r'(?miu)</p><p>', '</p>\n\n<p>', text)
|
||||
|
|
@ -166,11 +170,15 @@ def remove_p(t):
|
|||
|
||||
def get_text(self):
|
||||
text = []
|
||||
for item in self.oeb_book.spine:
|
||||
for i, item in enumerate(self.oeb_book.spine):
|
||||
if self.opts.sectionize_chapters_using_file_structure and i is not 0:
|
||||
text.append('<section>')
|
||||
self.log.debug('Converting %s to FictionBook2 XML' % item.href)
|
||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile)
|
||||
text.append(self.add_page_anchor(item))
|
||||
text += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||
if self.opts.sectionize_chapters_using_file_structure and i is not len(self.oeb_book.spine) - 1:
|
||||
text.append('</section>')
|
||||
return ''.join(text)
|
||||
|
||||
def fb2_body_footer(self):
|
||||
|
|
@ -258,6 +266,10 @@ def dump_text(self, elem, stylizer, page, tag_stack=[]):
|
|||
if id_name:
|
||||
fb2_text.append(self.get_anchor(page, id_name))
|
||||
|
||||
if tag == 'h1' and self.opts.h1_to_title or tag == 'h2' and self.opts.h2_to_title or tag == 'h3' and self.opts.h3_to_title:
|
||||
fb2_text.append('<title>')
|
||||
tags.append('title')
|
||||
|
||||
fb2_tag = TAG_MAP.get(tag, None)
|
||||
if fb2_tag == 'p':
|
||||
if 'p' in tag_stack+tags:
|
||||
|
|
|
|||
|
|
@ -25,6 +25,20 @@ class FB2Output(OutputFormatPlugin):
|
|||
'WARNING: ' \
|
||||
'This option is experimental. It can cause conversion ' \
|
||||
'to fail. It can also produce unexpected output.')),
|
||||
OptionRecommendation(name='sectionize_chapters_using_file_structure',
|
||||
recommended_value=False, level=OptionRecommendation.LOW,
|
||||
help=_('Try to turn chapters into individual sections using the ' \
|
||||
'internal structure of the ebook. This works well for EPUB ' \
|
||||
'books that have been internally split by chapter.')),
|
||||
OptionRecommendation(name='h1_to_title',
|
||||
recommended_value=False, level=OptionRecommendation.LOW,
|
||||
help=_('Wrap all h1 tags with fb2 title elements.')),
|
||||
OptionRecommendation(name='h2_to_title',
|
||||
recommended_value=False, level=OptionRecommendation.LOW,
|
||||
help=_('Wrap all h2 tags with fb2 title elements.')),
|
||||
OptionRecommendation(name='h3_to_title',
|
||||
recommended_value=False, level=OptionRecommendation.LOW,
|
||||
help=_('Wrap all h3 tags with fb2 title elements.')),
|
||||
])
|
||||
|
||||
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
||||
|
|
|
|||
|
|
@ -216,7 +216,9 @@ def dump_text(self, elem, stylizer, page, tag_stack=[]):
|
|||
w = '\\w'
|
||||
width = elem.get('width')
|
||||
if width:
|
||||
w += '="%s%%"' % width
|
||||
if not width.endswith('%'):
|
||||
width += '%'
|
||||
w += '="%s"' % width
|
||||
else:
|
||||
w += '="50%"'
|
||||
text.append(w)
|
||||
|
|
@ -252,8 +254,8 @@ def dump_text(self, elem, stylizer, page, tag_stack=[]):
|
|||
if href not in self.link_hrefs.keys():
|
||||
self.link_hrefs[href] = 'calibre_link-%s' % len(self.link_hrefs.keys())
|
||||
href = '#%s' % self.link_hrefs[href]
|
||||
text.append('\\q="%s"' % href)
|
||||
tags.append('q')
|
||||
text.append('\\q="%s"' % href)
|
||||
tags.append('q')
|
||||
|
||||
# Anchor ids
|
||||
id_name = elem.get('id')
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ class PluginWidget(Widget, Ui_Form):
|
|||
ICON = I('mimetypes/fb2.png')
|
||||
|
||||
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
|
||||
Widget.__init__(self, parent, ['inline_toc', 'sectionize_chapters'])
|
||||
Widget.__init__(self, parent, ['inline_toc', 'sectionize_chapters',
|
||||
'sectionize_chapters_using_file_structure', 'h1_to_title',
|
||||
'h2_to_title', 'h3_to_title'])
|
||||
self.db, self.book_id = db, book_id
|
||||
self.initialize_options(get_option, get_help, db, book_id)
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="2" column="0">
|
||||
<item row="6" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
|
|
@ -41,6 +41,34 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QCheckBox" name="opt_sectionize_chapters_using_file_structure">
|
||||
<property name="text">
|
||||
<string>Sectionize Chapters using file structure</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QCheckBox" name="opt_h1_to_title">
|
||||
<property name="text">
|
||||
<string>Wrap h1 tags with <title> elements</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QCheckBox" name="opt_h2_to_title">
|
||||
<property name="text">
|
||||
<string>Wrap h2 tags with <title> elements</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0">
|
||||
<widget class="QCheckBox" name="opt_h3_to_title">
|
||||
<property name="text">
|
||||
<string>Wrap h3 tags with <title> elements</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue