Sync to trunk.

This commit is contained in:
John Schember 2009-06-18 19:52:42 -04:00
commit efab7fdcdb
3 changed files with 58 additions and 17 deletions

View File

@ -759,28 +759,33 @@ class Manifest(object):
# Convert to Unicode and normalize line endings
data = self.oeb.decode(data)
data = self.oeb.html_preprocessor(data)
orig_data = data
# Try with more & more drastic measures to parse
try:
data = etree.fromstring(data)
except etree.XMLSyntaxError:
repl = lambda m: ENTITYDEFS.get(m.group(1), m.group(0))
data = ENTITY_RE.sub(repl, data)
def first_pass(data):
try:
data = etree.fromstring(data)
except etree.XMLSyntaxError:
# TODO: Factor out HTML->XML coercion
self.oeb.logger.warn('Parsing file %r as HTML' % self.href)
data = html.fromstring(data)
data.attrib.pop('xmlns', None)
for elem in data.iter(tag=etree.Comment):
if elem.text:
elem.text = elem.text.strip('-')
data = etree.tostring(data, encoding=unicode)
repl = lambda m: ENTITYDEFS.get(m.group(1), m.group(0))
data = ENTITY_RE.sub(repl, data)
try:
data = etree.fromstring(data)
except etree.XMLSyntaxError:
data = etree.fromstring(data, parser=RECOVER_PARSER)
self.oeb.logger.warn('Parsing file %r as HTML' % self.href)
data = html.fromstring(data)
data.attrib.pop('xmlns', None)
for elem in data.iter(tag=etree.Comment):
if elem.text:
elem.text = elem.text.strip('-')
data = etree.tostring(data, encoding=unicode)
try:
data = etree.fromstring(data)
except etree.XMLSyntaxError:
data = etree.fromstring(data, parser=RECOVER_PARSER)
return data
data = first_pass(data)
# Force into the XHTML namespace
if barename(data.tag) != 'html':
data = first_pass('<html>'+data+'</html>')
if barename(data.tag) != 'html':
raise NotHTML(
'File %r does not appear to be (X)HTML' % self.href)

View File

@ -8,7 +8,8 @@ __docformat__ = 'restructuredtext en'
import os, math, re
from PyQt4.Qt import QWidget, QSize, QSizePolicy, QUrl, SIGNAL, Qt, QTimer, \
QPainter, QPalette, QBrush, QFontDatabase, QDialog, \
QByteArray, QColor, QWheelEvent, QPoint, QImage, QRegion, QFont
QByteArray, QColor, QWheelEvent, QPoint, QImage, QRegion, \
QFont, QObject, QApplication, pyqtSignature
from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings
from calibre.utils.config import Config, StringConfig
@ -72,6 +73,20 @@ def config(defaults=None):
return c
class PythonJS(QObject):
def __init__(self, callback):
QObject.__init__(self, QApplication.instance())
self.setObjectName("py_bridge")
self._callback = callback
@pyqtSignature("QString")
def callback(self, msg):
print "callback called"
self._callback(msg)
class ConfigDialog(QDialog, Ui_Dialog):
def __init__(self, *args):
@ -89,6 +104,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
self.css.setPlainText(opts.user_css)
self.css.setToolTip(_('Set the user CSS stylesheet. This can be used to customize the look of all books.'))
def accept(self, *args):
c = config()
c.set('serif_family', unicode(self.serif_family.currentFont().family()))
@ -125,7 +141,10 @@ class Document(QWebPage):
def __init__(self, *args):
QWebPage.__init__(self, *args)
self.setObjectName("py_bridge")
self.debug_javascript = False
#self.js_bridge = PythonJS(self.js_callback)
self.setLinkDelegationPolicy(self.DelegateAllLinks)
self.scroll_marks = []
pal = self.palette()
@ -160,12 +179,17 @@ class Document(QWebPage):
self.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(pt.name))
def load_javascript_libraries(self):
self.mainFrame().addToJavaScriptWindowObject("py_bridge", self)
from calibre.resources import jquery, jquery_scrollTo
self.javascript(jquery)
self.javascript(jquery_scrollTo)
self.javascript(bookmarks)
self.javascript(referencing)
@pyqtSignature("")
def animated_scroll_done(self):
self.emit(SIGNAL('animated_scroll_done()'))
def reference_mode(self, enable):
self.javascript(('enter' if enable else 'leave')+'_reference_mode()')
@ -302,6 +326,12 @@ class DocumentView(QWebView):
self.connect(self.document, SIGNAL('linkClicked(QUrl)'), self.link_clicked)
self.connect(self.document, SIGNAL('linkHovered(QString,QString,QString)'), self.link_hovered)
self.connect(self.document, SIGNAL('selectionChanged()'), self.selection_changed)
self.connect(self.document, SIGNAL('animated_scroll_done()'),
self.animated_scroll_done, Qt.QueuedConnection)
def animated_scroll_done(self):
if self.manager is not None:
self.manager.scrolled(self.document.scroll_fraction)
def reference_mode(self, enable):
self._reference_mode = enable

View File

@ -54,10 +54,15 @@ function calculate_bookmark(y) {
return sel + "|" + ratio;
}
function animated_scrolling_done() {
window.py_bridge.animated_scroll_done();
}
function scroll_to_bookmark(bookmark) {
bm = bookmark.split("|");
var ratio = 0.7 * parseFloat(bm[1]);
$.scrollTo($(bm[0]), 1000, {over:ratio});
$.scrollTo($(bm[0]), 1000,
{over:ratio, onAfter:function(){window.py_bridge.animated_scroll_done()}});
}
'''
@ -112,7 +117,8 @@ function goto_reference(ref) {
if (num < 0) {alert("Invalid reference: "+ref); return;}
var p = $("p");
if (num >= p.length) {alert("Reference not found: "+ref); return;}
$.scrollTo($(p[num]), 1000);
$.scrollTo($(p[num]), 1000,
{onAfter:function(){window.py_bridge.animated_scroll_done()}});
}
'''