summaryrefslogtreecommitdiff
path: root/sphinx/websupport/storage/differ.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/websupport/storage/differ.py')
-rw-r--r--sphinx/websupport/storage/differ.py73
1 files changed, 43 insertions, 30 deletions
diff --git a/sphinx/websupport/storage/differ.py b/sphinx/websupport/storage/differ.py
index c82ba7427..068d7e6fc 100644
--- a/sphinx/websupport/storage/differ.py
+++ b/sphinx/websupport/storage/differ.py
@@ -14,39 +14,18 @@ from cgi import escape
from difflib import Differ
class CombinedHtmlDiff(object):
-
+ """Create an HTML representation of the differences between two pieces
+ of text.
+ """
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 is not None and 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):
+ """Return the HTML representation of the differences between
+ `source` and `proposal`.
+
+ :param source: the original text
+ :param proposal: the proposed text
+ """
proposal = escape(proposal)
differ = Differ()
@@ -64,3 +43,37 @@ class CombinedHtmlDiff(object):
self._handle_line(line)
break
return ''.join(html)
+
+ def _handle_line(self, line, next=None):
+ """Handle an individual line in a diff."""
+ prefix = line[0]
+ text = line[2:]
+
+ if prefix == ' ':
+ return text
+ elif prefix == '?':
+ return ''
+
+ if next is not None and 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 _highlight_text(self, text, next, tag):
+ """Highlight the specific changes made to a line by adding
+ <ins> and <del> tags.
+ """
+ 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)
+