Difference between revisions of "Mailto.py"

From MythTV Official Wiki
Jump to: navigation, search
m (new template)
Line 6: Line 6:
  
 
== Source code ==
 
== Source code ==
{{Box File|mailto.py|
+
{{Code box|mailto.py|
 
<pre>
 
<pre>
 
import re
 
import re

Revision as of 20:56, 3 April 2007

Important.png Note: The correct title of this article is mailto.py. It appears incorrectly here due to technical restrictions.


This is a pywikipedia script, developed to support maintenance of this site.

Several user pages have email links on them. Mediawiki supports the mailto: type designator. However, many of the entries, due to MoinMoin's style, have spaces in them. This script cleans up the mail links, and make them operable again.

Source code

Script.png mailto.py

import re
from pywikipedia import wikipedia

site = wikipedia.getSite()

for page in site.allpages():
        if not page.isRedirectPage():
                wikitext = page.get()

                try:
                        mailtoR = re.compile("mailto:\((.*)\)")
                        results = mailtoR.search(wikitext)
                        before, after = results.span()
                        final = wikitext[0:before] + re.compile("\(|\)").sub("", re.compile(" ").sub("_", results.group())) + wikitext[after:]
                        if wikitext <> final:
                                page.put(newtext = final, comment='[[pywikipedia]] assisted cleanup ([[mailto.py]])-> fixing legacy email references', minorEdit=True)
                except:
                        pass

How to run it

To run the script, it needs no arguments.

% python mailto.py

It will search all pages, and where it sees: mailto:(foo at bar), it will replace it with mailto:foo_at_bar.