diff options
author | Jacob Mason <jacoblmason@gmail.com> | 2010-08-05 19:29:24 -0500 |
---|---|---|
committer | Jacob Mason <jacoblmason@gmail.com> | 2010-08-05 19:29:24 -0500 |
commit | d29f65112e327054dde4501a61d3c7d4fa16bcbf (patch) | |
tree | 48b635b1cfc2e57daece7d49e7839e3fcf7429e2 /sphinx/websupport/storage/differ.py | |
parent | fbd047b8f593a439d0a8ca9eec02ad2491d2bd78 (diff) | |
download | sphinx-git-d29f65112e327054dde4501a61d3c7d4fa16bcbf.tar.gz |
added storage package
Diffstat (limited to 'sphinx/websupport/storage/differ.py')
-rw-r--r-- | sphinx/websupport/storage/differ.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/sphinx/websupport/storage/differ.py b/sphinx/websupport/storage/differ.py new file mode 100644 index 000000000..4e5660c59 --- /dev/null +++ b/sphinx/websupport/storage/differ.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +""" + sphinx.websupport.storage.differ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + A differ for creating an HTML representations of proposal diffs + + :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import re +from cgi import escape +from difflib import Differ + +class CombinedHtmlDiff(object): + + highlight_regex = re.compile(r'([\+\-\^]+)') + + def _highlight_text(self, text, next, tag): + next = next[2:] + new_text = [] + start = 0 + for match in self.highlight_regex.finditer(next): + new_text.append(text[start:match.start()]) + new_text.append('<%s>' % tag) + new_text.append(text[match.start():match.end()]) + new_text.append('</%s>' % tag) + start = match.end() + new_text.append(text[start:]) + return ''.join(new_text) + + def _handle_line(self, line, next=None): + prefix = line[0] + text = line[2:] + + if prefix == ' ': + return text + elif prefix == '?': + return '' + + if next[0] == '?': + tag = 'ins' if prefix == '+' else 'del' + text = self._highlight_text(text, next, tag) + css_class = 'prop_added' if prefix == '+' else 'prop_removed' + + return '<span class="%s">%s</span>\n' % (css_class, text.rstrip()) + + def make_html(self, source, proposal): + proposal = escape(proposal) + + differ = Differ() + diff = list(differ.compare(source.splitlines(1), + proposal.splitlines(1))) + html = [] + line = diff.pop(0) + next = diff.pop(0) + while True: + html.append(self._handle_line(line, next)) + line = next + try: + next = diff.pop(0) + except IndexError: + self._handle_line(line) + break + return ''.join(html) |