This commit is contained in:
Kovid Goyal 2012-01-04 13:35:31 +05:30
parent 896c451aef
commit 99b5a75691
4 changed files with 35 additions and 10 deletions

View File

@ -4,7 +4,7 @@
### ###
Copyright 2011, Kovid Goyal <kovid@kovidgoyal.net> Copyright 2011, Kovid Goyal <kovid@kovidgoyal.net>
Released under the GPLv3 License 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) -> log = (error) ->
@ -51,6 +51,24 @@ fstr = (d) -> # {{{
ans 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 class CanonicalFragmentIdentifier
# This class is a namespace to expose CFI functions via the window.cfi # 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 # Add id assertions for robustness where possible
id = node.getAttribute?('id') 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 cfi = '/' + index + idspec + cfi
node = p node = p
@ -117,7 +136,7 @@ class CanonicalFragmentIdentifier
error = null error = null
node = doc 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 if ( (r = cfi.match(simple_node_regex)) is not null ) # Path step
target = parseInt(r[1]) target = parseInt(r[1])
assertion = r[2] assertion = r[2]
@ -253,7 +272,7 @@ class CanonicalFragmentIdentifier
(if target.parentNode then target.parentNode else target).normalize() (if target.parentNode then target.parentNode else target).normalize()
if name in ['audio', 'video'] if name in ['audio', 'video']
tail = "~" + fstr target.currentTime tail = "~" + get_current_time(target)
if name in ['img', 'video'] if name in ['img', 'video']
px = ((x + cwin.scrollX - target.offsetLeft)*100)/target.offsetWidth px = ((x + cwin.scrollX - target.offsetLeft)*100)/target.offsetWidth

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>Testing CFI functionality</title> <title>Testing CFI functionality</title>
<script type="text/javascript" src="../cfi.coffee"></script> <script type="text/javascript" src="cfi.coffee"></script>
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="cfi-test.coffee"></script> <script type="text/javascript" src="cfi-test.coffee"></script>
</head> </head>

View File

@ -16,10 +16,9 @@ except ImportError:
if False: init_calibre, serve if False: init_calibre, serve
from calibre.utils.coffeescript import serve from calibre.utils.coffeescript import serve
def run_devel_server(): def run_devel_server():
os.chdir(os.path.dirname(os.path.abspath(__file__))) os.chdir(os.path.dirname(os.path.abspath(__file__)))
serve() serve(resources={'/cfi.coffee':'../cfi.coffee'})
if __name__ == '__main__': if __name__ == '__main__':
run_devel_server() run_devel_server()

View File

@ -15,12 +15,15 @@ import time, SimpleHTTPServer, SocketServer, os, subprocess
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
generated_files = set() generated_files = set()
special_resources = {}
def translate_path(self, path): def translate_path(self, path):
path = self.special_resources.get(path, path)
if path.endswith('jquery.js'): if path.endswith('jquery.js'):
return P('content_server/jquery.js') return P('content_server/jquery.js')
if path.endswith('.coffee'): 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, return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self,
path) path)
@ -49,8 +52,12 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
f.write('alert("Compilation of %s failed");'%src) f.write('alert("Compilation of %s failed");'%src)
return dest return dest
def serve(port=8000): class HTTPD(SocketServer.TCPServer):
httpd = SocketServer.TCPServer(('localhost', port), Handler) allow_reuse_address = True
def serve(resources={}, port=8000):
Handler.special_resources = resources
httpd = HTTPD(('localhost', port), Handler)
print('serving at localhost:%d'%port) print('serving at localhost:%d'%port)
try: try:
try: try: