From BlenderWiki
Note: Doesn't like new lines between list items. This should be fixed.
Solution: either change the way groups are formed or make a script to compact lists.
#!/usr/local/bin/python
#ArrangeWikiLists v 0.5
#BeBraw
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2006: BeBraw
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
import sys
from sys import *
from operator import itemgetter
try:
sys.argv[1]
except IndexError:
print "Error, no input file specified!"
sys.exit()
else:
if sys.argv[1] == "-?" or sys.argv[1] == "?":
print "arrange -inputName [-outputName]"
sys.exit()
else:
inputName = sys.argv[1]
try:
sys.argv[2]
except IndexError:
outputName = "output.txt"
else:
outputName = sys.argv[1]
fInput=open(inputName, 'r')
lines = fInput.readlines()
lines.append(" ") # Add empty line to get the last group ok to the output. hackish
#Open file to be written into.
fOutput = open(outputName,"w")
#The algorithm finds list groups and arranges them. after that list is written
#to output file.
group = {}
for lineIndex, line in enumerate(lines):
if line[0] == '*': #Find *. Each member of list has * in front.
#After that we get suitable part of the line to be compared so that the
#list can be arranged. Note that we also keep their indexes in a dict.
#Example of a dict: index -> comparable
if line.find('|') != -1:
group[lineIndex] = line[line.find('|')+1:]
elif line.find('[') != -1:
slicedLine = line[line.find('[')+1:]
if slicedLine.find(' ') != -1:
group[lineIndex] = slicedLine[slicedLine.find(' ')+1:]
elif line.find(' ') != -1:
slicedLine = line[line.find(' ')+1:]
group[lineIndex] = slicedLine
else:
if group != {}:
#Write sorted group to file.
group = sorted(group.items(), key=itemgetter(1)) #Sorts according to key.
for g in group:
fOutput.write(lines[g[0]])
#Write the line to file in any case.
fOutput.write(line)
group = {}
fInput.close()
fOutput.close()