Script inserts missing thead and may move first tr

This script works just the same as before by default but now if there is
no thead element, a thead is inserted and the first row found is moved into the
thead element. This allows the script to work in places where a proper
thead may not be generated.