This commit is contained in:
Kovid Goyal 2012-01-04 16:01:44 +05:30
parent 99b5a75691
commit 4d2b798cbc
3 changed files with 49 additions and 38 deletions

View File

@ -107,12 +107,12 @@ class CanonicalFragmentIdentifier
cfi = "!" + cfi cfi = "!" + cfi
continue continue
break break
# Increase index by the length of all previous sibling text nodes # Find position of node in parent
index = 0 index = 0
child = p.firstChild child = p.firstChild
while true while true
index |= 1 index |= 1 # Increment index by 1 if it is even
if child.nodeType in [1, 7] if child.nodeType == 1
index++ index++
if child == node if child == node
break break
@ -120,8 +120,7 @@ class CanonicalFragmentIdentifier
# Add id assertions for robustness where possible # Add id assertions for robustness where possible
id = node.getAttribute?('id') id = node.getAttribute?('id')
idok = id and id.match(/^[-a-zA-Z_0-9.\u007F-\uFFFF]+$/) idspec = if id then "[#{ escape_for_cfi(id) }]" else ''
idspec = if idok then "[#{ escape_for_cfi(id) }]" else ''
cfi = '/' + index + idspec + cfi cfi = '/' + index + idspec + cfi
node = p node = p
@ -155,11 +154,18 @@ class CanonicalFragmentIdentifier
error = "No matching child found for CFI: " + cfi error = "No matching child found for CFI: " + cfi
break break
index |= 1 # Increment index by 1 if it is even index |= 1 # Increment index by 1 if it is even
if child.nodeType in [1, 7] # We have an element or a PI if child.nodeType == 1
index++ index++
if ( index == target ) if ( index == target )
cfi = cfi.substr(r[0].length) cfi = cfi.substr(r[0].length)
node = child node = child
if assertion and node.id != assertion
# The found child does not match the id assertion,
# trust the id assertion if an element with that id
# exists
child = doc.getElementById(assertion)
if child
node = child
break break
child = child.nextSibling child = child.nextSibling

View File

@ -18,7 +18,7 @@ except ImportError:
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(resources={'/cfi.coffee':'../cfi.coffee'}) serve(resources={'cfi.coffee':'../cfi.coffee'})
if __name__ == '__main__': if __name__ == '__main__':
run_devel_server() run_devel_server()

View File

@ -10,20 +10,32 @@ __docformat__ = 'restructuredtext en'
''' '''
Utilities to help with developing coffeescript based apps Utilities to help with developing coffeescript based apps
''' '''
import time, SimpleHTTPServer, SocketServer, os, subprocess import time, SimpleHTTPServer, SocketServer, os, subprocess, cStringIO
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
generated_files = set()
special_resources = {} special_resources = {}
compiled_cs = {}
def send_head(self):
path = self.path
if path.endswith('.coffee'):
path = path[1:] if path.startswith('/') else path
path = self.special_resources.get(path, path)
raw, mtime = self.compile_coffeescript(path)
self.send_response(200)
self.send_header("Content-type", b'text/javascript')
self.send_header("Content-Length", bytes(len(raw)))
self.send_header("Last-Modified", self.date_time_string(int(mtime)))
self.end_headers()
return cStringIO.StringIO(raw)
return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
def translate_path(self, path): def translate_path(self, path):
path = self.special_resources.get(path, 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'):
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)
@ -34,23 +46,23 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
except: except:
time.sleep(0.01) time.sleep(0.01)
sstat = os.stat(src) sstat = os.stat(src)
return (not os.access(dest, os.R_OK) or sstat.st_mtime > return sstat.st_mtime > dest
os.stat(dest).st_mtime)
def compile_coffeescript(self, src): def compile_coffeescript(self, src):
dest = os.path.splitext(src)[0] + '.js' raw, mtime = self.compiled_cs.get(src, (None, 0))
self.generated_files.add(dest) if self.newer(src, mtime):
if self.newer(src, dest): mtime = time.time()
with open(dest, 'wb') as f: try:
try: raw = subprocess.check_output(['coffee', '-c', '-p', src])
subprocess.check_call(['coffee', '-c', '-p', src], stdout=f) except:
except: print('Compilation of %s failed'%src)
print('Compilation of %s failed'%src) cs = '''
f.seek(0) // Compilation of coffeescript failed
f.truncate() alert("Compilation of %s failed");
f.write('// Compilation of coffeescript failed') '''%src
f.write('alert("Compilation of %s failed");'%src) raw = cs.encode('utf-8')
return dest self.compiled_cs[src] = (raw, mtime)
return raw, mtime
class HTTPD(SocketServer.TCPServer): class HTTPD(SocketServer.TCPServer):
allow_reuse_address = True allow_reuse_address = True
@ -60,14 +72,7 @@ def serve(resources={}, port=8000):
httpd = HTTPD(('localhost', port), Handler) httpd = HTTPD(('localhost', port), Handler)
print('serving at localhost:%d'%port) print('serving at localhost:%d'%port)
try: try:
try: httpd.serve_forever()
httpd.serve_forever() except KeyboardInterrupt:
except KeyboardInterrupt: raise SystemExit(0)
raise SystemExit(0)
finally:
for x in Handler.generated_files:
try:
os.remove(x)
except:
pass