ecto: Auto abbr/acronym

There are a couple of instances of scripts out there that automatically apply abbr and acronym tags to pages, but I wanted to be able to do the same in ecto. This is also the first time I wrote a plugin script for ecto, and I wanted to do it in python. Please note that this here script is untested until I get onto my Mac and test the hell out of it. The script works, with the caveat listed in TODO.

 1     #! /usr/bin/env python
 2     
 3     'A script for ecto that adds abbr and acronym tags to the text'
 4         
 5     TODO = '''
 6     Fix it so that acronyms without a space either side (for example,
 7     that finish a sentence) work.
 8         
 9     Lookup on the internet for a list of acronyms/abbreviations?
10     '''
11         
12     acronyms={'WYSIWYG':'What You See Is What You Get',
13               'DOM':'Document Object Model'}
14     abbrs={'XHTML':'eXtensible HyperText Markup Language',
15            'NSLU2':'[Linksys] Network Storage Link (USB) 2.0'}
16         
17     # Add more values to your hearts content…
18     
19     # get input data - depends on implementation.  For ecto:
20     import sys
21     data = open(sys.argv[1]).read()
22         
23     # replace only the first instance of each acronym/abbreviation
24     for each in acronyms:
25         data.replace(' '+each+' ', '<acronym title="'+acronyms[each]+'">'+each+'</acronym>',1)
26     for each in abbrs:
27         data.replace(' '+each+' ', '<abbr title="'+abbrs[each]+'">'+each+'</abbr>',1)
28         
29     #return data to ecto
30     open(sys.argv[1],'w').write(data)

Note: Comments turned off: too much Spam on this entry.

blog comments powered by Disqus