Sync to pluginize

This commit is contained in:
John Schember 2009-04-25 17:46:23 -04:00
commit 7ce53a941e
6 changed files with 40 additions and 10 deletions

View File

@ -128,7 +128,7 @@ def add_pipeline_options(parser, plumber):
[
'dont_split_on_page_breaks', 'chapter', 'chapter_mark',
'prefer_metadata_cover', 'remove_first_image',
'insert_comments',
'insert_comments', 'page_breaks_before',
]
),

View File

@ -227,6 +227,14 @@ OptionRecommendation(name='extra_css',
'rules.')
),
OptionRecommendation(name='page_breaks_before',
recommended_value="//*[name()='h1' or name()='h2']",
level=OptionRecommendation.LOW,
help=_('An XPath expression. Page breaks are inserted '
'before the specified elements.')
),
OptionRecommendation(name='margin_top',
recommended_value=5.0, level=OptionRecommendation.LOW,
help=_('Set the top margin in pts. Default is %default')),

View File

@ -26,10 +26,17 @@ def sanitize_head(match):
def chap_head(match):
chap = match.group('chap')
title = match.group('title')
<<<<<<< TREE
if not title:
return '<h1>'+chap+'</h1><br/>\n'
else:
return '<h1>'+chap+'<br/>\n'+title+'</h1><br/>\n'
=======
if not title:
return '<h1>'+chap+'</h1><br/>'
else:
return '<h1>'+chap+'<br/>'+title+'</h1><br/>'
>>>>>>> MERGE-SOURCE
def wrap_lines(match):
ital = match.group('ital')
@ -55,19 +62,19 @@ def line_length(raw, percent):
total = sum(lengths)
avg = total / len(lengths)
max_line = avg * 2
lengths = sorted(lengths)
for i in range(len(lengths) - 1, -1, -1):
if lengths[i] > max_line:
del lengths[i]
if percent > 1:
percent = 1
if percent < 0:
percent = 0
index = int(len(lengths) * percent) - 1
return lengths[index]
@ -110,14 +117,13 @@ class HTMLPreProcessor(object):
# Remove non breaking spaces
(re.compile(ur'\u00a0'), lambda match : ' '),
# Detect Chapters to match default XPATH in GUI
(re.compile(r'(?=<(/?br|p))(<(/?br|p)[^>]*)?>\s*(?P<chap>(<i><b>|<i>|<b>)?(Chapter|Epilogue|Prologue|Book|Part)\s*(\d+|\w+)?(</i></b>|</i>|</b>)?)(</?p[^>]*>|<br[^>]*>)\n?((?=(<i>)?\s*\w+(\s+\w+)?(</i>)?(<br[^>]*>|</?p[^>]*>))((?P<title>(<i>)?\s*\w+(\s+\w+)?(</i>)?)(<br[^>]*>|</?p[^>]*>)))?', re.IGNORECASE), chap_head),
(re.compile(r'(?=<(/?br|p))(<(/?br|p)[^>]*)?>\s*(?P<chap>([A-Z \'"!]{5,})\s*(\d+|\w+)?)(</?p[^>]*>|<br[^>]*>)\n?((?=(<i>)?\s*\w+(\s+\w+)?(</i>)?(<br[^>]*>|</?p[^>]*>))((?P<title>.*)(<br[^>]*>|</?p[^>]*>)))?'), chap_head),
# Have paragraphs show better
(re.compile(r'<br.*?>'), lambda match : '<p>'),
# Clean up spaces
(re.compile(u'(?<=[\.,:;\?!”"\'])[\s^ ]*(?=<)'), lambda match: ' '),
# Add space before and after italics
@ -154,6 +160,7 @@ class HTMLPreProcessor(object):
def __call__(self, html, remove_special_chars=None):
if remove_special_chars is not None:
html = remove_special_chars.sub('', html)
html = html.replace('\0', '')
if self.is_baen(html):
rules = []
elif self.is_book_designer(html):

View File

@ -941,7 +941,10 @@ class Manifest(object):
href = urlunparse(purl)
path, frag = urldefrag(href)
if not path:
return '#'.join((self.href, frag))
if frag:
return '#'.join((self.href, frag))
else:
return self.href
if '/' not in self.href:
return href
dirname = os.path.dirname(self.href)

View File

@ -45,6 +45,14 @@ class DetectStructure(object):
if not node.title or regexp.search(node.title) is not None:
self.oeb.toc.remove(node)
if opts.page_breaks_before is not None:
pb_xpath = XPath(opts.page_breaks_before)
for item in oeb.spine:
for elem in pb_xpath(item.data):
style = elem.get('style', '')
if style:
style += '; '
elem.set('style', style+'page-break-before:always')
def detect_chapters(self):
self.detected_chapters = []
@ -102,6 +110,7 @@ class DetectStructure(object):
play_order=self.oeb.toc.next_play_order())
def elem_to_link(self, item, elem, counter):
text = u' '.join([t.strip() for t in elem.xpath('descendant::text()')])
text = text[:100].strip()

View File

@ -159,9 +159,12 @@ class MetadataSingleDialog(ResizableDialog, Ui_MetadataSingleDialog):
row = self.formats.currentRow()
fmt = self.formats.item(row)
if fmt is None:
error_dialog(self, _('No format selected'),
if self.formats.count() == 1:
fmt = self.formats.item(0)
if fmt is None:
error_dialog(self, _('No format selected'),
_('No format selected')).exec_()
return
return
ext = fmt.ext.lower()
if fmt.path is None:
stream = self.db.format(self.row, ext, as_file=True)