diff options
Diffstat (limited to 'watchnews/web.py')
-rw-r--r-- | watchnews/web.py | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/watchnews/web.py b/watchnews/web.py index 438e649..05ffcdc 100644 --- a/watchnews/web.py +++ b/watchnews/web.py @@ -9,6 +9,8 @@ import difflib class DiffSupport: + def __init__(self): + self.inline_style = False def _diff(self, line1, line2): diff = list(difflib._mdiff([line1], [line2])) @@ -27,21 +29,31 @@ class DiffSupport: html.td(*self._format_diff(diff2)) )) return html.table(rows, **{'class': 'textdiff'}) - + def _format_diff(self, line): + actionclass = { + '+': 'diff_add', + '-': 'diff_sub', + '^': 'diff_chg'} + actionname = { + '+': 'i', + '-': 'strike', + '^': 'strong'} elems = [] nextpos = line.find("\x00") while nextpos != -1 and nextpos + 1 < len(line): - actionclass = { - '+': 'diff_add', '-': 'diff_sub', - '^': 'diff_chg'}[line[nextpos + 1]] endpos = line.find("\x01", nextpos + 2) if nextpos != 0: # intermediate unchanged text elems += [html.span(line[:nextpos])] text = line[nextpos + 2:endpos] - elems += [html.span(text, **{'class': actionclass})] + if self.inline_style: + elem = html.span(text) + elem.xmlname = actionname[line[nextpos+1]] + elems += [elem] + else: + elems += [html.span(text, **{'class':actionclass[line[nextpos+1]]})] line = line[endpos:] nextpos = line.find("\x00") @@ -53,8 +65,10 @@ class DiffSupport: class Difftable(html.div, DiffSupport): - def __init__(self, to_version, from_version=None): + def __init__(self, to_version, from_version=None, inline_style=False): super().__init__() + self.xmlname = "div" + self.inline_style = inline_style if from_version == None: self.single_version(to_version) else: |