[Tutor] Mad Villain Seeks Source Review.

Ignacio Vazquez-Abrams ignacio@openservices.net
Mon, 20 Aug 2001 22:12:25 -0400 (EDT)


On Mon, 20 Aug 2001, Ignacio Vazquez-Abrams wrote:

> On Mon, 20 Aug 2001, Tesla Coil wrote:
>
> > Phase One of My Diabolical Plan to Reduce the
> > World Wide Web to 256 Grayscale seems to have
> > been delayed...
> >
> >  [snip]
> >     colorsearch = '[=][\s]?['+colorsearch+']'
>
> That RE is _Wrong_. Try "'=\s?\'(%s)\'' % colorsearch".

Turns out that that wasn't the only problem. Here, have fun.

---
#! /usr/bin/python2

import re

namedcolors={
  'maroon' : '262626',
  'green' : '4b4b4b',
  'red' : '4c4c4c',
  'navy' : '0e0e0e',
  'purple' : '343434',
  'lime' : '969696',
  'blue' : '1c1c1c',
  'magenta' : '686868',
  'olive' : '717171',
  'yellow' : 'e2e2e2',
  'teal' : '595959',
  'cyan' : 'b2b2b2',
}

def namerepl(match):
  global namedcolors
  a,b,c,d=match.group(1,2,3,4)
  t=d or c
  return "='#%s'" % namedcolors[t.lower()]

def firstpass(line):
  global namedcolors
  colors='|'.join(namedcolors.keys())
  colorsearch='=\s*((\'(%s)\')|(%s))' % (colors, colors)
  colorre=re.compile(colorsearch, re.I)
  line=colorre.sub(namerepl, line)
  return line

teststring = "<BODY BGCOLOR='Magenta'TEXT= Yellow>"
print firstpass(teststring)
---

Here's a link to a complete set of color names in case you needed it:

http://developer.netscape.com/docs/manuals/htmlguid/colortab.htm

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>