Extract more information from diffs when highlighting; specifically HG export metadata.

This commit is contained in:
gbrandl 2009-01-06 11:10:56 +01:00
parent 4ed502bb97
commit 1f08630bc2
5 changed files with 64 additions and 7 deletions

View File

@ -159,8 +159,8 @@ class Paste(object):
))
if template:
from lodgeit.lib.diff import prepare_udiff
rv = prepare_udiff(udiff)
return rv and rv[0] or None
diff, info = prepare_udiff(udiff)
return diff and diff[0] or None
return udiff
@cached_property

View File

@ -9,11 +9,12 @@
:license: BSD
"""
import re
import time
from cgi import escape
def prepare_udiff(udiff):
"""Prepare an udiff for a template"""
"""Prepare an udiff for a template."""
return DiffRenderer(udiff).prepare()
@ -66,8 +67,50 @@ class DiffRenderer(object):
do(line)
do(next)
def _parse_info(self):
"""Look for custom information preceding the diff."""
nlines = len(self.lines)
if not nlines:
return
firstline = self.lines[0]
info = []
# look for Hg export changeset
if firstline.startswith('# HG changeset patch'):
info.append(('Type', 'HG export changeset'))
i = 0
line = firstline
while line.startswith('#'):
if line.startswith('# User'):
info.append(('User', line[7:].strip()))
elif line.startswith('# Date'):
try:
t, tz = map(int, line[7:].split())
info.append(('Date', time.strftime(
'%b %d, %Y %H:%M:%S', time.gmtime(float(t) - tz))))
except Exception:
pass
elif line.startswith('# Branch'):
info.append(('Branch', line[9:].strip()))
i += 1
if i == nlines:
return info
line = self.lines[i]
commitmsg = ''
while not line.startswith('diff'):
commitmsg += line + '\n'
i += 1
if i == nlines:
return info
line = self.lines[i]
info.append(('Commit message', '\n' + commitmsg.strip()))
self.lines = self.lines[i:]
return info
def _parse_udiff(self):
"""Parse the diff an return data for the template."""
info = self._parse_info()
in_header = True
header = []
lineiter = iter(self.lines)
@ -165,7 +208,7 @@ class DiffRenderer(object):
except StopIteration:
pass
return files
return files, info
def prepare(self):
return self._parse_udiff()

View File

@ -110,7 +110,7 @@ _escaped_marker = re.compile(r'^\\(?=###)(?m)')
def highlight(code, language, _preview=False):
"""Highlight a given code to HTML"""
"""Highlight a given code to HTML."""
if not _preview:
if language == 'diff':
return highlight_diff(code)
@ -163,8 +163,8 @@ def preview_highlight(code, language, num=5):
def highlight_diff(code):
"""Highlights an unified diff."""
diffs = prepare_udiff(code)
return render_template('utils/udiff.html', diffs=diffs)
diffs, info = prepare_udiff(code)
return render_template('utils/udiff.html', diffs=diffs, info=info)
def format_creole(code):

View File

@ -491,6 +491,12 @@ div.diff-table {
background: white;
}
div.diff-table div.diffinfo {
background: #CBD8DB;
color: #111;
padding: 5px;
}
div.diff-table div.meta {
background: #CBD8DB;
color: #111;

View File

@ -1,5 +1,13 @@
{% from 'utils/macros.html' import render_diff_part %}
<div class="diff-table">
{%- if info %}
<div class="diffinfo">
{%- for (k, v) in info %}
{% if k %}<b>{{ k }}:</b> {% endif %}{{ v|replace('\n', '<br>') }}
{%- if not loop.last %}<br>{% endif %}
{%- endfor %}
</div>
{%- endif %}
{% for diff in diffs %}
<div class="diff">
{% if diff.is_header %}