def _str2html(src, strip=False, indent_subsequent=0,
              highlight_inner=False):
    if strip:
        src = src.strip()
    orig_src = src
    try:
        src = PySourceColor.str2html(src, form='snip')
        src = error_re.sub('', src)
        src = pre_re.sub('', src)
        src = re.sub(r'^[\n\r]{0,1}', '', src)
        src = re.sub(r'[\n\r]{0,1}$', '', src)
    except:
        src = html_quote(orig_src)
    lines = src.splitlines()
    if len(lines) == 1:
        return lines[0]
    indent = ' '*indent_subsequent
    for i in range(1, len(lines)):
        lines[i] = indent+lines[i]
        if highlight_inner and i == len(lines)/2:
            lines[i] = '<span class="source-highlight">%s</span>' % lines[i]
    src = '<br>\n'.join(lines)
    src = whitespace_re.sub(
        lambda m: '&nbsp;'*(len(m.group(0))-1) + ' ', src)
    return src
示例#2
0
 def source_code(self, req):
     location = req.params['location']
     module_name, lineno = location.split(':', 1)
     module = sys.modules[module_name]
     filename = module.__file__
     if filename.endswith('.pyc'):
         filename = filename[:-1]
     f = open(filename, 'rb')
     source = f.read()
     f.close()
     html = (
         ('<div>Module: <b>%s</b> file: %s</div>'
          % (module_name, filename))
         + PySourceColor.str2html(source, form='snip', linenumbers=-1))
     source_lines = len(source.splitlines())
     if source_lines < 60:
         html += '\n<br>'*(60-source_lines)
     res = Response(content_type='text/html', charset='utf8')
     res.body = html
     return res