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