mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Pull from driver-dev
This commit is contained in:
commit
10a95db60a
@ -246,7 +246,7 @@ class CurrentDir(object):
|
||||
os.chdir(self.cwd)
|
||||
|
||||
|
||||
class FileWrapper(object):
|
||||
class StreamReadWrapper(object):
|
||||
'''
|
||||
Used primarily with pyPdf to ensure the stream is properly closed.
|
||||
'''
|
||||
|
@ -23,6 +23,14 @@ def sanitize_head(match):
|
||||
x = _span_pat.sub('', x)
|
||||
return '<head>\n'+x+'\n</head>'
|
||||
|
||||
def chap_head(match):
|
||||
chap = match.group('chap')
|
||||
title = match.group('title')
|
||||
if not title:
|
||||
return '<h1>'+chap+'</h1><br/>'
|
||||
else:
|
||||
return '<h1>'+chap+'<br/>'+title+'</h1><br/>'
|
||||
|
||||
|
||||
class CSSPreProcessor(object):
|
||||
|
||||
@ -54,8 +62,9 @@ class HTMLPreProcessor(object):
|
||||
(re.compile(r'<hr.*?>', re.IGNORECASE), lambda match: '<br />'),
|
||||
# Remove page numbers
|
||||
(re.compile(r'\d+<br>', re.IGNORECASE), lambda match: ''),
|
||||
# Remove <br> and replace <br><br> with <p>
|
||||
# Replace <br><br> with <p>
|
||||
(re.compile(r'<br.*?>\s*<br.*?>', re.IGNORECASE), lambda match: '<p>'),
|
||||
# Remove <br>
|
||||
(re.compile(r'(.*)<br.*?>', re.IGNORECASE),
|
||||
lambda match: match.group() if \
|
||||
re.match('<', match.group(1).lstrip()) or \
|
||||
@ -69,15 +78,22 @@ 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[^>]*>)?s*(?P<chap>(Chapter|Epilogue|Prologue|Book|Part)\s*(\d+|\w+)?)(</?p[^>]*>|<br[^>]*>)\n?((?=(<i>)?\s*\w+(\s+\w+)?(</i>)?(<br[^>]*>|</?p[^>]*>))((?P<title>.*)(<br[^>]*>|</?p[^>]*>)))?', re.IGNORECASE), chap_head),
|
||||
(re.compile(r'(<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>'),
|
||||
|
||||
# Un wrap lines
|
||||
(re.compile(r'(?<=\w)\s*</(i|b|u)>\s*<p.*?>\s*<(i|b|u)>\s*(?=\w)'), lambda match: ' '),
|
||||
(re.compile(r'(?<=\w)\s*<p.*?>\s*(?=\w)', re.UNICODE), lambda match: ' '),
|
||||
(re.compile(r'(?<=[^\.^\^?^!^"^”])\s*(</(i|b|u)>)*\s*<p.*?>\s*(<(i|b|u)>)*\s*(?=[a-z0-9I])', re.UNICODE), lambda match: ' '),
|
||||
|
||||
# Clean up spaces
|
||||
(re.compile(u'(?<=\.|,|:|;|\?|!|”|"|\')[\s^ ]*(?=<)'), lambda match: ' '),
|
||||
]
|
||||
(re.compile(u'(?<=[\.,:;\?!”"\'])[\s^ ]*(?=<)'), lambda match: ' '),
|
||||
# Add space before and after italics
|
||||
(re.compile(r'(?<!“)<i>'), lambda match: ' <i>'),
|
||||
(re.compile(r'</i>(?=\w)'), lambda match: '</i> '),
|
||||
]
|
||||
|
||||
# Fix Book Designer markup
|
||||
BOOK_DESIGNER = [
|
||||
|
@ -6,7 +6,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
import sys, os, cStringIO
|
||||
from threading import Thread
|
||||
|
||||
from calibre import FileWrapper
|
||||
from calibre import StreamReadWrapper
|
||||
from calibre.ebooks.metadata import MetaInformation, authors_to_string
|
||||
from calibre.ptempfile import TemporaryDirectory
|
||||
from pyPdf import PdfFileReader, PdfFileWriter
|
||||
@ -33,7 +33,7 @@ def get_metadata(stream, extract_cover=True):
|
||||
traceback.print_exc()
|
||||
|
||||
try:
|
||||
with FileWrapper(stream) as stream:
|
||||
with StreamReadWrapper(stream) as stream:
|
||||
info = PdfFileReader(stream).getDocumentInfo()
|
||||
if info.title:
|
||||
mi.title = info.title
|
||||
@ -94,29 +94,27 @@ def set_metadata(stream, mi):
|
||||
stream.seek(0)
|
||||
|
||||
def get_cover(stream):
|
||||
|
||||
try:
|
||||
pdf = PdfFileReader(stream)
|
||||
output = PdfFileWriter()
|
||||
|
||||
if len(pdf.pages) >= 1:
|
||||
output.addPage(pdf.getPage(0))
|
||||
|
||||
with TemporaryDirectory('_pdfmeta') as tdir:
|
||||
cover_path = os.path.join(tdir, 'cover.pdf')
|
||||
|
||||
with open(cover_path, "wb") as outputStream:
|
||||
output.write(outputStream)
|
||||
with ImageMagick():
|
||||
wand = NewMagickWand()
|
||||
MagickReadImage(wand, cover_path)
|
||||
MagickSetImageFormat(wand, 'JPEG')
|
||||
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
||||
return open('%s.jpg' % cover_path, 'rb').read()
|
||||
|
||||
with StreamReadWrapper(stream) as stream:
|
||||
pdf = PdfFileReader(stream)
|
||||
output = PdfFileWriter()
|
||||
|
||||
if len(pdf.pages) >= 1:
|
||||
output.addPage(pdf.getPage(0))
|
||||
|
||||
with TemporaryDirectory('_pdfmeta') as tdir:
|
||||
cover_path = os.path.join(tdir, 'cover.pdf')
|
||||
|
||||
with open(cover_path, "wb") as outputStream:
|
||||
output.write(outputStream)
|
||||
with ImageMagick():
|
||||
wand = NewMagickWand()
|
||||
MagickReadImage(wand, cover_path)
|
||||
MagickSetImageFormat(wand, 'JPEG')
|
||||
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
||||
return open('%s.jpg' % cover_path, 'rb').read()
|
||||
except:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
return ''
|
||||
|
||||
|
@ -126,7 +126,7 @@ class PDFWriter(QObject):
|
||||
try:
|
||||
outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author)
|
||||
for item in self.combine_queue:
|
||||
inputPDF = PdfFileReader(file(item, 'rb'))
|
||||
inputPDF = PdfFileReader(open(item, 'rb'))
|
||||
for page in inputPDF.pages:
|
||||
outPDF.addPage(page)
|
||||
outPDF.write(self.out_stream)
|
||||
|
@ -346,10 +346,25 @@ class DeviceMenu(QMenu):
|
||||
self.action_triggered(action)
|
||||
break
|
||||
|
||||
def enable_device_actions(self, enable):
|
||||
def enable_device_actions(self, enable, card_prefix=(None, None)):
|
||||
for action in self.actions:
|
||||
if action.dest in ('main:', 'carda:0', 'cardb:0'):
|
||||
action.setEnabled(enable)
|
||||
if not enable:
|
||||
action.setEnabled(False)
|
||||
else:
|
||||
if action.dest == 'main:':
|
||||
action.setEnabled(True)
|
||||
elif action.dest == 'carda:0':
|
||||
if card_prefix[0] != None:
|
||||
action.setEnabled(True)
|
||||
else:
|
||||
action.setEnabled(False)
|
||||
elif action.dest == 'cardb:0':
|
||||
if card_prefix[1] != None:
|
||||
action.setEnabled(True)
|
||||
else:
|
||||
action.setEnabled(False)
|
||||
|
||||
|
||||
class Emailer(Thread):
|
||||
|
||||
|
@ -608,7 +608,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
|
||||
self.device_manager.device.__class__.__name__+\
|
||||
_(' detected.'), 3000)
|
||||
self.device_connected = True
|
||||
self._sync_menu.enable_device_actions(True)
|
||||
self._sync_menu.enable_device_actions(True, self.device_manager.device.card_prefix())
|
||||
else:
|
||||
self.device_connected = False
|
||||
self._sync_menu.enable_device_actions(False)
|
||||
|
Loading…
x
Reference in New Issue
Block a user