#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import xml.etree.ElementTree as ET

input_list = []

for arg in sys.argv[1:]:
	input_list.append(arg)

if len(input_list) < 1:
	print 'usage: makemd.py <classes.xml>'
	sys.exit(0)


def validate_tag(elem, tag):
	if elem.tag != tag:
		print "Tag mismatch, expected '" + tag + "', got " + elem.tag
		sys.exit(255)


class_names = []
classes = {}


def make_class_list(class_list, columns):

	f = open('class_list.md', 'wb')
	prev = 0
	col_max = len(class_list) / columns + 1
	print ('col max is ', col_max)
	col_count = 0
	row_count = 0
	last_initial = ''
	fit_columns = []

	for n in range(0, columns):
		fit_columns += [[]]

	indexers = []
	last_initial = ''

	idx = 0
	for n in class_list:
		col = idx / col_max
		if col >= columns:
			col = columns - 1
		fit_columns[col] += [n]
		idx += 1
		if n[:1] != last_initial:
			indexers += [n]
		last_initial = n[:1]

	row_max = 0
	f.write("\n")

	for n in range(0, columns):
		if len(fit_columns[n]) > row_max:
			row_max = len(fit_columns[n])

	f.write("| ")
	for n in range(0, columns):
		f.write(" | |")

	f.write("\n")
	f.write("| ")
	for n in range(0, columns):
		f.write(" --- | ------- |")
	f.write("\n")

	for r in range(0, row_max):
		s = '| '
		for c in range(0, columns):
			if r >= len(fit_columns[c]):
				continue

			classname = fit_columns[c][r]
			initial = classname[0]
			if classname in indexers:
				s += '**' + initial + '** | '
			else: