acronymmer.py final (?)

Taking into account the comments on PhotoMatt’s acronymit page (where I shamelessly stole some ideas, including the list of words!), I’ve modified acronymmer.py so that it:

  1. Does not apply <acronym> tags to instances that have been acronymmed already.
  2. Does not apply tags to instances that are inside href or title strings.
  3. Does not save the file if no changes have been made.

Here it is, including my current acronym list. (I know some of these are Abbreviations, but ecto, and some browsers do not handle this tag). Also, this code needs to be re-pasted - I was trying to make it (XHTML) valid, and it’s broken now.

 1     #! /usr/bin/env python
 2         
 3     'A script for ecto that adds abbr and acronym tags to the text'
 4         
 5     acronyms = {
 6     'WYSIWYG' : 'what you see is what you get',
 7     'XHTML' : 'eXtensible HyperText Markup Language',
 8     'IIRC' : 'if I remember correctly',
 9     'HDTV' : 'High Definition TeleVision',
10     'LGPL' : 'GNU Lesser General Public License',
11     'MSDN' : 'Microsoft Developer Network',
12     'WCAG' : 'Web Content Accessibility Guidelines',
13     'SOAP' : 'Simple Object Access Protocol',
14     'OPML' : 'Outline Processor Markup Language',
15     'MSIE' : 'Microsoft Internet Explorer',
16     'FOAF' : 'Friend of a Friend vocabulary',
17     'GFDL' : 'GNU Free Documentation License',
18     'XSLT' : 'eXtensible Stylesheet Language Transformation',
19     'HTML' : 'HyperText Markup Language',
20     'IMAP' : 'Internet Message Access Protocol',
21     'RAID' : 'Redundant Array of Independent Disks',
22     'MPEG' : 'Motion Picture Experts Group',
23     'JPEG' : 'Joint Photographic Experts Group',
24     'CSTA' : 'Central Scorpions Touch Association',
25     'CDDB' : 'Compact Disc DataBase (Gracenote)',
26     'XBMC' : 'Xbox Media Centre',
27     'VNC' : 'Virtual Network Computing',
28     'URL' : 'Uniform Resource Locator',
29     'W3C' : 'World Wide Web Consortium',
30     'MSN' : 'Microsoft Network',
31     'USB' : 'Universal Serial Bus',
32     'P2P' : 'Peer To Peer',
33     'PBS' : 'Public Broadcasting System',
34     'RSS' : 'Really Simple Syndication',
35     'SIG' : 'Special Interest Group',
36     'RDF' : 'Resource Description Framework',
37     'AOL' : 'American Online',
38     'PHP' : 'PHP Hypertext Processor',
39     'SSN' : 'Social Security Number',
40     'JSP' : 'Java Server Pages',
41     'DOM' : 'Document Object Model',
42     'DTD' : 'Document Type Definition',
43     'DVD' : 'Digital Versatile Disc',
44     'DNS' : 'Domain Name System',
45     'CSS' : 'Cascading Style Sheets',
46     'CGI' : 'Common Gateway Interface',
47     'CMS' : 'Content Management System',
48     'FAQ' : 'Frequently Asked Questions',
49     'FSF' : 'Free Software Foundation',
50     'API' : 'Application Interface',
51     'PDF' : 'Portable Document Format',
52     'IIS' : 'Internet Infomation Server',
53     'XML' : 'eXtensible Markup Language',
54     'XSL' : 'eXtensible Stylesheet Language',
55     'GPL' : 'GNU General Public License',
56     'KDE' : 'K Desktop Environment',
57     'STB' : 'Set Top Box',
58     'MP3' : 'Mpeg Layer 3',
59     'PVR' : 'Personal (Digital) Video Recorder',
60     'GUI' : 'Graphical User Interface',
61     'CLI' : 'Command Line Interface',
62     'AAC' : 'Advanced Audio Coding',
63     'IE' : 'Internet Explorer',
64     'CD' : 'Compact Disk',
65     'GB' : 'Gigabyte',
66     'MB' : 'Megabyte',
67     'KB' : 'Kilobyte',
68     'TV' : 'TeleVision',
69     'PC' : 'Personal Computer',
70     'NSLU2':'[Linksys] Network Storage Link (USB) 2.0'
71     }
72     
73     
74     import sys, re
75     data = open(sys.argv[1]).read()
76     olddata = data
77     
78     # replace only the first instance of each acronym/abbreviation
79     for each in acronyms:
80         d = re.search(r'(?!<.*(title|href)='*>)?\b%s\b(?!(.*'>|</acronym>))' % each, data)
81         if d:
82             data = data[:d.start()] + '<acronym title='' + \
83                    acronyms[each] + '>' + \
84                    each + '</acronym>' + data[d.end():]
85         
86     if data <> olddata:
87         open(sys.argv[1],'w').write(data)