#! /usr/bin/python

import re, sys, os, string

def addquotes(matchobj):
	return matchobj.group(1) + '="' + matchobj.group(2) + '"' + matchobj.group(3)

def fixEndTag(matchobj):
	str = matchobj.group(1)
	ch = str[-1]
	str = str[:-1]
	wss = ''
	while len(str) and str[-1] in [' ', '\t', '\n', '\r', '\f', '\v']:
		wss = str[-1] + wss
		str = str[:-1]
	return str + '>' + wss + ch

def fixPendingEnd(match):
	return '<' + match.group(1) + match.group(2) + "</%s>" % match.group(1)

def lowertag(matchobj):
	return matchobj.group(1) + string.lower(matchobj.group(2))

docbookfile = open(sys.argv[1])
text = docbookfile.read()
docbookfile.close()
orig_text = text

pat = re.compile(r'(<[^>]*\s[^> \t\n]+)=([^ \t\n\r\f\v">]+)([\s>])', re.DOTALL)

while 1:
	text, n = re.subn(pat, addquotes, text)
	if not n:
		break
	else:
		sys.stderr.write("replaced %d wrong parameters\n" % n)
	
# fixing broken endtag
for tag in "email", "para":
	text, n = re.subn("(</%s\s+[^>])" % tag, fixEndTag, text)
	if n:
		sys.stderr.write("fixed %d wrong end tags\n" % n)

for tag in "lt", "gt", "amp", "lcub", "rcub", "lowbar", "etago", "dollar", \
	"percnt", "lsqb", "rsqb", "num", "verbar": # kdevelop strangeness
	text = re.sub("&&%s;;" % tag, "&%s;" % tag, text)

text, n2 = re.subn('&\s', '&amp; ', text)
text, n3 = re.subn('&&\s', '&amp;&amp; ', text)
n = n2 + n3
if n:
	sys.stderr.write("fixed %d wrong amps\n" % n)

pat = re.compile("(<\w*)([A-Z])")
while 1:
	text, n = re.subn(pat, lowertag, text)
	if not n:
		break
	
pat = re.compile("(</\w*)([A-Z])")
while 1:
	text, n = re.subn(pat, lowertag, text)
	if not n:
		break
	
text, n = re.subn('<([^> \t\n\r]+)([^>]*>[^<]+)</>', fixPendingEnd, text)
if n:
	sys.stderr.write("fixed %d pending ends\n" % n)

text = re.sub('´', '\'', text)

text = re.sub("<graphic(\s[^>]*)>\s*</graphic>",
			  "<mediaobject>\n"
			  "<imageobject>\n"
			  "<imagedata\\1>\n"
			  "</imageobject>\n"
			  "</mediaobject>", text)
text = re.sub("<inlinegraphic(\s[^>]*)>\s*</inlinegraphic>",
			  "<inlinemediaobject>\n"
			  "<imageobject>\n"
			  "<imagedata\\1>\n"
			  "</imageobject>\n"
			  "</inlinemediaobject>", text)

text = re.sub("<imagedata(\\s.*)format=\"png\"", "<imagedata\\1format=\"PNG\"", text)
text = re.sub("&kdedocindex;", "&documentation.index;", text)

if not text == orig_text:
	docbookfile = open(sys.argv[1], 'w')
	docbookfile.write(text)
	docbookfile.close()

