append pango code to message about color spec requiring manual fix

When pangoize detects an old-style color spec, it prints a message that it needs a "manual fix." Unfortunately, the old markup used decimal values while pango uses hexadecimal, and authors were left to do the conversion themselves.

My modification not only does the hex conversion, it provides pango code ready to copy and paste into the line.

Going over this:

rgb =:  First step is to turn the original regular expression into a regex object. The one change is that later on, wmllint turns non-pango "<" and ">" into "&lt;/&gt;", so I have the regex match those too, in case we are dealing with a file that has already been through wmllint before.
if rgb:  Having turned the original search into a regex object, we are ready for an if test again.
  r, g, b =:  We need Python to recognize these strings as numbers.
  if > 255:  At least one old campaign ("A Sortie") has color specs that include values over 255. Given the impossibility of deciphering what color the author may have intended, I think the proper thing to do is to print an error pointing to the problem.
  else:  This, of course, is the normal case.
    hexed:  Here we convert our numbers to hexadecimal, and back into a string. Because numbers up to 15 will only have one hex digit and we need two, we will leave a "0" when we remove the "0x" prefix; then we take the last two characters, lopping off the zero from the numbers greater than 15 that already have two digits.
    print:  The new error message. With the regex object, we can cite the color spec specifically, not just refer to it as being "in line". And at the end is pango code, ready to copy and paste.
This commit is contained in:
groggydice 2013-06-06 22:25:40 -04:00
parent fddc28738b
commit 2167195685

View file

@ -782,8 +782,16 @@ def pangoize(message, filename, line):
amper = message.find('&')
if message[amper:amper+1].isspace():
message = message[:amper] + "&amp;" + message[amper+1:]
if re.search("<[0-9]+,[0-9]+,[0-9]+>", message):
print '"%s", line %d: color spec in line requires manual fix.' % (filename, line)
rgb = re.search("(?:<|&lt;)([0-9]+),([0-9]+),([0-9]+)(>|&gt;)", message)
if rgb:
r = int(rgb.group(1))
g = int(rgb.group(2))
b = int(rgb.group(3))
if ( r or g or b) > 255:
print '"%s", line %d: RGB color value over 255 requires manual fix (%s).' % (filename, line, rgb.group())
else:
hexed = hex(r).replace('0x', '0')[-2:] + hex(g).replace('0x', '0')[-2:] + hex(b).replace('0x', '0')[-2:]
print '"%s", line %d: color spec (%s) requires manual fix (<span color=\'#%s\'>, </span>).' % (filename, line, rgb.group(), hexed)
# Hack old-style Wesnoth markup
for (oldstyle, newstart, newend) in pango_conversions:
if oldstyle not in message: