views:

54

answers:

1

I have an oldstring:

'foobarba <span class="foo">z</span>'

and a newstring:

'foodbar ba<span class="foo">z</span>'

a string is given for a classname, it could be "foo" again, let's say "bar".

Given newstring, oldstring and bar, I want to end up with:

'foo<span class="bar">d</span> ba<span class="foo">z</span>'

I want to diff the two strings. If the new string is shorter, maybe check for empty tags and through them out .. but for now we can do nothing. But, if the new string is longer (it will be longer by probably 1 character, maybe more, but few), I want to find the new character(s), check whether it is wrapped by an appropriate span (one with the given bar) and, if not wrapped, wrap it.

If the oldstring is now:

'foo<span class="bar">d</span> ba<span class="foo">z</span>'

given foo again as the classname, and the newstring:

'foo<span class="bar">dz</span> ba<span class="foo">z</span>'

I would like to sense the new z character and come out with

'foo<span class="bar">d</span><span class="foo">z</span> ba<span class="foo">z</span>'

OR

'foo<span class="bar">d<span class="foo">z</span></span> ba<span class="foo">z</span>'

Suggestions for good python modules or even perhaps a quick trick from the stdlib would be great. idk, I'm tired; might wake up with the answer tomorrow.

+1  A: 

Use difflib from the stdlib to find differences between your strings, then just insert your span tags around the diffs.

Paul McGuire