#!/usr/bin/python
import math
import sys

facets = {}
facetmax = {}
tags = []
max = 0

infile = sys.stdin
if len(sys.argv) > 1:
	infile = open(sys.argv[1],"r")
for line in infile.readlines():
	ftag, count = line.split(": ")
	if ftag.startswith("special::"): continue
	facet, tag = ftag.split("::")
	if not facets.has_key(facet):
		facets[facet] = {}
		facetmax[facet] = 0
	count = int(count)
	facets[facet][tag] = count
	if count > facetmax[facet]: facetmax[facet] = count
	if count > max: max = count
infile.close()

print """\
<html>
<head>
<title>Debtags Tag Cloud</title>
<style type="text/css">
.facet {
    padding: 1px;
    display:block;
    margin-bottom: 2em;
}
.ft {
    color: red;
    font-weight: bold;
    margin: 1em;
    font-size: 120%;
}
a {
    color: black;
    text-decoration: none;
    padding-right: .1em;
}
a:hover {
    text-decoration: underline;
}
.tag0 {
    font-size: 60%;
}
.tag1 {
    font-size: 80%;
}
.tag2 {
    font-size: 120%;
}
.tag3 {
    font-size: 140%;
}
.tag4 {
    font-size: 160%;
}
.tag5 {
    font-size: 180%;
}
</style>
</head>
<body>\
""",

magicscale = 50
lastfacet = None
sortedFacets = facets.keys()
sortedFacets.sort()
for facet in sortedFacets:
	tags = facets[facet].keys()
	tags.sort()
	print '<span class="facet"><span class="ft">%s</span>' % facet
	for tag in tags:
		count = facets[facet][tag]
		fmax = facetmax[facet]
		size = math.log(1+magicscale*float(count)/fmax)/math.log(magicscale+1)
		size = int(size*5+.5)
		print '<a class="tag%d" href="http://debtags.alioth.debian.org/cgi-bin/index.cgi?tags=%s::%s">%s</a> ' % (size, facet, tag, tag),
	print "</span>",

print '</body></html>'
