From 99b5a7569122f8236bf49b0028ca7b37078f56b1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 4 Jan 2012 13:35:31 +0530 Subject: [PATCH] ... --- src/calibre/ebooks/oeb/display/cfi.coffee | 27 ++++++++++++++++--- .../display/test/{test.html => index.html} | 2 +- src/calibre/ebooks/oeb/display/test/test.py | 3 +-- src/calibre/utils/coffeescript.py | 13 ++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) rename src/calibre/ebooks/oeb/display/test/{test.html => index.html} (83%) diff --git a/src/calibre/ebooks/oeb/display/cfi.coffee b/src/calibre/ebooks/oeb/display/cfi.coffee index 9c6e18ea06..a6cf638eed 100644 --- a/src/calibre/ebooks/oeb/display/cfi.coffee +++ b/src/calibre/ebooks/oeb/display/cfi.coffee @@ -4,7 +4,7 @@ ### Copyright 2011, Kovid Goyal Released under the GPLv3 License - Based on code originally written by Peter Sorotkin (epubcfi.js) + Based on code originally written by Peter Sorotkin (http://code.google.com/p/epub-revision/source/browse/trunk/src/samples/cfi/epubcfi.js) ### # log = (error) -> @@ -51,6 +51,24 @@ fstr = (d) -> # {{{ ans # }}} +get_current_time = (target) -> # {{{ + ans = 0 + if target.currentTime != undefined + ans = target.currentTime + fstr(ans) +# }}} + +set_current_time = (target, val) -> # {{{ + if target.currentTime == undefined + return + if target.readyState == 4 or target.readyState == "complete" + target.currentTime = val + else + fn = -> target.currentTime = val + target.addEventListener("canplay", fn, false) + +#}}} + class CanonicalFragmentIdentifier # This class is a namespace to expose CFI functions via the window.cfi @@ -102,7 +120,8 @@ class CanonicalFragmentIdentifier # Add id assertions for robustness where possible id = node.getAttribute?('id') - idspec = if id then "[#{ escape_for_cfi(id) }]" else '' + idok = id and id.match(/^[-a-zA-Z_0-9.\u007F-\uFFFF]+$/) + idspec = if idok then "[#{ escape_for_cfi(id) }]" else '' cfi = '/' + index + idspec + cfi node = p @@ -117,7 +136,7 @@ class CanonicalFragmentIdentifier error = null node = doc - until cfi.length <= 0 or error + until cfi.length < 1 or error if ( (r = cfi.match(simple_node_regex)) is not null ) # Path step target = parseInt(r[1]) assertion = r[2] @@ -253,7 +272,7 @@ class CanonicalFragmentIdentifier (if target.parentNode then target.parentNode else target).normalize() if name in ['audio', 'video'] - tail = "~" + fstr target.currentTime + tail = "~" + get_current_time(target) if name in ['img', 'video'] px = ((x + cwin.scrollX - target.offsetLeft)*100)/target.offsetWidth diff --git a/src/calibre/ebooks/oeb/display/test/test.html b/src/calibre/ebooks/oeb/display/test/index.html similarity index 83% rename from src/calibre/ebooks/oeb/display/test/test.html rename to src/calibre/ebooks/oeb/display/test/index.html index 3dbda451c0..1b93bb9739 100644 --- a/src/calibre/ebooks/oeb/display/test/test.html +++ b/src/calibre/ebooks/oeb/display/test/index.html @@ -2,7 +2,7 @@ Testing CFI functionality - + diff --git a/src/calibre/ebooks/oeb/display/test/test.py b/src/calibre/ebooks/oeb/display/test/test.py index e217027312..28a1becaeb 100644 --- a/src/calibre/ebooks/oeb/display/test/test.py +++ b/src/calibre/ebooks/oeb/display/test/test.py @@ -16,10 +16,9 @@ except ImportError: if False: init_calibre, serve from calibre.utils.coffeescript import serve - def run_devel_server(): os.chdir(os.path.dirname(os.path.abspath(__file__))) - serve() + serve(resources={'/cfi.coffee':'../cfi.coffee'}) if __name__ == '__main__': run_devel_server() diff --git a/src/calibre/utils/coffeescript.py b/src/calibre/utils/coffeescript.py index 6178685bbb..b80532be3b 100644 --- a/src/calibre/utils/coffeescript.py +++ b/src/calibre/utils/coffeescript.py @@ -15,12 +15,15 @@ import time, SimpleHTTPServer, SocketServer, os, subprocess class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): generated_files = set() + special_resources = {} def translate_path(self, path): + path = self.special_resources.get(path, path) if path.endswith('jquery.js'): return P('content_server/jquery.js') if path.endswith('.coffee'): - return self.compile_coffeescript(path[1:]) + path = path[1:] if path.startswith('/') else path + return self.compile_coffeescript(path) return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, path) @@ -49,8 +52,12 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): f.write('alert("Compilation of %s failed");'%src) return dest -def serve(port=8000): - httpd = SocketServer.TCPServer(('localhost', port), Handler) +class HTTPD(SocketServer.TCPServer): + allow_reuse_address = True + +def serve(resources={}, port=8000): + Handler.special_resources = resources + httpd = HTTPD(('localhost', port), Handler) print('serving at localhost:%d'%port) try: try: