# # # patch "common.py" # from [02468252bf61578b2443ee7b77fceb7207136127] # to [1462ca963d4b3959019bb0ac26a5311f6f2bd406] # ============================================================ --- common.py 02468252bf61578b2443ee7b77fceb7207136127 +++ common.py 1462ca963d4b3959019bb0ac26a5311f6f2bd406 @@ -47,3 +47,161 @@ def ago(event): rv = "%s" % (plural(minutes, "minute", "minutes")) return rv +def link(mt, link_type, link_to, description = None, no_quote = False): + hq = html_escape() + if not no_quote and description != None: description = hq(description) + if link_type == "revision": + rv = '' % (urllib.quote(link_to)) + if description != None: rv += description + else: rv += hq(link_to[:8]) + ".." + rv += '' + if description == None: rv = '[' + rv + ']' + elif link_type == "diff" or link_type == "download_diff": + link_to = map(urllib.quote, filter(lambda x: x != None, link_to)) + if link_type == "diff": + handler = "diff.psp" + else: + handler = "getdiff.py" + uri = '%s?id1=%s&id2=%s' % (handler, link_to[0], link_to[1]) + if len(link_to) == 3: + uri += '&fname=%s' % (link_to[2]) + rv = '' + if description != None: rv += description + else: rv += "diff" + rv += '' + elif link_type == "download": + if type(link_to) == type([]): + rv = '' % (urllib.quote(link_to[0]), + urllib.quote(link_to[1])) + link_id = link_to[0] + else: + rv = '' % (urllib.quote(link_to)) + link_id = link_to + if description != None: rv += description + "" + else: rv = "[" + rv + hq(link_id[:8]) + ".." + "]" + elif link_type == "file": + revision_id, path = link_to + rv = '' % (urllib.quote(revision_id), + urllib.quote(path)) + if description != None: rv += description + "" + else: rv = "[" + rv + hq(path + '@' + revision_id[:8]) + ".." + "]" + elif link_type == "fileinbranch": + branch, path = link_to + rv = '' % (urllib.quote(branch), + urllib.quote(path)) + if description != None: rv += description + "" + else: rv = "[" + rv + hq(path + '@' + branch) + "]" + elif link_type == "branch": + rv = '' % (urllib.quote(link_to)) + if description != None: rv += description + else: rv += hq(link_to) + rv += '' + elif link_type == "tar": + rv = '' % (urllib.quote(link_to)) + if description != None: rv += description + else: rv = "tar of [" + rv + hq(link_to[:8]) + "..]" + "]" + rv += '' + elif link_type == "headofbranch": + rv = '' % (urllib.quote(link_to)) + if description != None: rv += description + else: rv += "head of " + hq(link_to) + rv += '' + elif link_type == "manifest": + if type(link_to) == type([]): + link_to, path = link_to + rv = '' % (urllib.quote(link_to), urllib.quote(path)) + else: + rv = '' % (urllib.quote(link_to)) + if description != None: rv += description + else: rv += hq(link_to[:8]) + ".." + rv += '' + if description == None: rv = '[' + rv + ']' + else: + rv = 'Unknown link type: %s' % (hq(link_type)) + return '%s' % (hq(link_type+'Link'), rv) + +def html_escape(): + "returns a function stolen from pydoc that can be used to escape HTML" + return lambda x: type_wrapper(escape_function, x) + +from enscriptlangs import enscript_langs +from utility import run_command +import mimetypes +import config +import pipes + +# is it binary? +def is_binary(str): + nontext_chars = "\x01\x02\x03\x04\x05\x06\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1c\x1d\x1e\x1f" + check = {} + for char in nontext_chars: + check[char] = True + for i in str: + if check.has_key(i): return True + return False + +# hm, later on might make this be some javascript that does an call back to the server. +# then could have a pull down to let people choose which enscript encoding to use, and +# just update the DOM with the new data. +def colourise_code(req, hq, matching_file_id, path, contents, filter=None): + mime_type = mimetypes.guess_type(path)[0] + if mime_type == None: mime_type = 'text/plain' + if mime_type == 'image/png' or mime_type == 'image/jpeg' or mime_type == 'image/gif': + display_as_image = True + else: display_as_image = False + + # okay; can we guess a valid enscript filter to run this through? + tsp = mime_type.split('/', 1) + if filter == None and tsp[0] == 'text': + candidate = tsp[1] + if candidate.startswith('x-'): candidate = candidate[2:] + if candidate.endswith('src'): candidate = candidate[:-3] + if candidate.endswith('hdr'): candidate = candidate[:-3] + if candidate == 'c++': candidate = 'cpp' # ugly + if candidate in enscript_langs: filter = candidate + if filter == None: + # heh, will at least work for lua files + last_dot = path.rfind('.') + if last_dot == -1: last_dot = 0 + candidate = path[last_dot:] + if candidate in enscript_langs: filter = candidate + + # if no filter then let's check if it's binary or not; if not binary + # we'll just treat it as text; otherwise display a warning and a download + # link + if filter == None and not is_binary(contents): + filter = 'text' + + req.write('''
')
+ def stop_code():
+ req.write('')
+ def text():
+ start_code()
+ req.write(hq(contents))
+ stop_code()
+ def enscript():
+ command = config.enscript_path + ' -o - --color --language=html'
+ command += ' --highlight=%s' % (pipes.quote(filter))
+ result = run_command(command, to_child=contents)
+ if result['exitcode'] != 0:
+ raise Exception('Error running enscript (%s) : "%s".' % (hq(command), hq(result['childerr'])))
+ in_contents = False
+ for line in result['fromchild'].split('\n'):
+ if line.startswith(''):
+ in_contents = True
+ start_code()
+ elif line.startswith(''):
+ in_contents = False
+ stop_code()
+ elif in_contents:
+ req.write(line + '\r\n')
+ if filter == "text": text()
+ else: enscript()
+ else:
+ req.write('''This file seems to binary and not suitable for display in the browser. You must %s the file and use a suitable viewer.
''' % (link("download", [matching_file_id, path], "download"))) + req.write('''