User:Smallman12q/doctoparameter mod.py
Appearance
# Simple script to create forms for my SnipManager
# Copyright (C) Odie5533
# GNU GPL 3.0+
# Written for Python 2.6
# Last modified May 21, 2009
import re
import urllib
import sys, string
def uniqify(seq):
seen = {}
result = []
for item in seq:
marker = item
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
while True:
base_url = "http://en.wikipedia.org/w/index.php?title=Template:%s&action=raw"
doc_base_url = "http://en.wikipedia.org/w/index.php?title=Template:%s/doc&action=raw"
wiki_page = raw_input("Template name (without Template:): ")
wiki_page = wiki_page.replace(" ", "_")
wiki_page_ = wiki_page.replace("_", " ")
page_url = base_url % (wiki_page)
doc_url = doc_base_url % (wiki_page)
uh = urllib.urlopen(page_url)
content = uh.read()
uh.close()
uh = urllib.urlopen(doc_url)
docs = uh.read()
uh.close()
if docs == '':
docs = content
# Parse the parameters from the main template page
params = re.findall("{{{([^}\|]+)[^}]*}}}", content)
params = uniqify(params)
# Attempt to parse the help for the parameters from the docs
descriptions = {}
for param in params:
reg = "'''%s''' (?:.\s+|–\s*)?(.*)" % (param)
match = re.search(reg, docs, re.M)
if match:
desc = match.group(1)
desc = desc.replace(r"'''", "")
desc = desc.replace(r"''", "")
desc = desc.replace("'", "\\'")
desc = desc.replace("–", "-")
if desc[3] == ' ':
desc = desc[4:]
descriptions[param] = desc
#{{<a href="/wiki/Template:Cite_news" title="Template:Cite news">Cite news</a>}}
#lines = ["'%s' : [ { 'prepend':'{{%s', 'append':'}}', 'description':'' }," % (wiki_page_, wiki_page_) ] #for printing to file
#prepend, append, description
print ("'%s' : [ { 'prepend':'{{%s', 'append':'}}', 'description': " + "'The template can be found at {{<a href=" +'"/wiki/Template:%s"' + ' title="Template:%s">%s</a>}}' + "'" + " },") % (wiki_page_, wiki_page_,wiki_page_,wiki_page_,wiki_page_)
for s in params:
help = ''
if s in descriptions:
help = descriptions[s]
#lines.append(" {'title':'%s:', 'code':'%s', 'help':'%s'}," % (s.capitalize(), s, help)) #for printing to file
print (" {'title':'%s:', 'code':'%s', 'help':'%s'}," % (s.capitalize(), s, help))
#We have not yet gotten around to adding this tooltip, but we plan to add it someday.
#lines[-1] = lines[-1][:-1] #remove trailing comma #for printing to file
#lines.append(" ],") #for printing to file
#The following is for printing to file
#fo = open('%s.txt' % (wiki_page), 'w') #for printing to file
#fo.write('\n'.join(lines)) #for printing to file
#fo.close() #for printing to file
print "================================================EOF==============================="