mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Implement bug #7738: FB2 Output option to create section per HTML file. Implement FB2 options to wrap h1-3 tags with title elments.
This commit is contained in:
parent
95ade95c60
commit
acac5a479b
@ -91,6 +91,10 @@ class FB2MLizer(object):
|
|||||||
return u'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True)
|
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):
|
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)<p>\s*</p>', '', text)
|
||||||
text = re.sub(r'(?miu)\s+</p>', '</p>', text)
|
text = re.sub(r'(?miu)\s+</p>', '</p>', text)
|
||||||
text = re.sub(r'(?miu)</p><p>', '</p>\n\n<p>', text)
|
text = re.sub(r'(?miu)</p><p>', '</p>\n\n<p>', text)
|
||||||
@ -166,11 +170,15 @@ class FB2MLizer(object):
|
|||||||
|
|
||||||
def get_text(self):
|
def get_text(self):
|
||||||
text = []
|
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)
|
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)
|
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile)
|
||||||
text.append(self.add_page_anchor(item))
|
text.append(self.add_page_anchor(item))
|
||||||
text += self.dump_text(item.data.find(XHTML('body')), stylizer, 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)
|
return ''.join(text)
|
||||||
|
|
||||||
def fb2_body_footer(self):
|
def fb2_body_footer(self):
|
||||||
@ -258,6 +266,10 @@ class FB2MLizer(object):
|
|||||||
if id_name:
|
if id_name:
|
||||||
fb2_text.append(self.get_anchor(page, 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)
|
fb2_tag = TAG_MAP.get(tag, None)
|
||||||
if fb2_tag == 'p':
|
if fb2_tag == 'p':
|
||||||
if 'p' in tag_stack+tags:
|
if 'p' in tag_stack+tags:
|
||||||
|
@ -25,6 +25,20 @@ class FB2Output(OutputFormatPlugin):
|
|||||||
'WARNING: ' \
|
'WARNING: ' \
|
||||||
'This option is experimental. It can cause conversion ' \
|
'This option is experimental. It can cause conversion ' \
|
||||||
'to fail. It can also produce unexpected output.')),
|
'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):
|
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
||||||
|
@ -17,6 +17,8 @@ class PluginWidget(Widget, Ui_Form):
|
|||||||
ICON = I('mimetypes/fb2.png')
|
ICON = I('mimetypes/fb2.png')
|
||||||
|
|
||||||
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, ['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.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,7 +14,7 @@
|
|||||||
<string>Form</string>
|
<string>Form</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="2" column="0">
|
<item row="6" column="0">
|
||||||
<spacer name="verticalSpacer">
|
<spacer name="verticalSpacer">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Vertical</enum>
|
<enum>Qt::Vertical</enum>
|
||||||
@ -41,6 +41,34 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
<resources/>
|
<resources/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user