on b5477e2 help sign in
project-m36/RelationHTML.hs
39 lines 1.4 kB history raw
1module RelationHTML where
2import RelationType
3import Relation
4import RelationTuple
5import qualified Data.HashSet as HS
6import qualified Data.List as L
7import RelationAttribute
9attributesAsHTML :: Attributes -> String
10attributesAsHTML attrs = "<tr>" ++ (foldr folder "" attrNameList) ++ "</tr>"
11 where
12 folder attrName acc = acc ++ "<th>" ++ attrName ++ "</th>"
13 attrNameList = sortedAttributeNameList (attributeNameSet attrs)
15relationAsHTML :: Relation -> String
16relationAsHTML rel@(Relation attrNameSet tupleSet) = "<table border=\"1\">" ++ (attributesAsHTML attrNameSet) ++ (tupleSetAsHTML tupleSet) ++ "<tfoot>" ++ tablefooter ++ "</tfoot></table>"
17 where
18 tablefooter = "<tr><td colspan=\"100%\">" ++ show (cardinality rel) ++ " tuples</td></tr>"
20writeHTML :: String -> IO ()
21writeHTML = writeFile "/home/agentm/rel.html"
23writeRel :: Relation -> IO ()
24writeRel = writeHTML . relationAsHTML
26atomAsHTML :: Atom -> String
27atomAsHTML (StringAtom s) = s
28atomAsHTML (IntAtom i) = show i
29atomAsHTML (RelationAtom rel) = relationAsHTML rel
31tupleAsHTML :: RelationTuple -> String
32tupleAsHTML tuple = "<tr>" ++ L.foldr folder "" (tupleSortedAssocs tuple) ++ "</tr>"
33 where
34 folder tup acc = acc ++ "<td>" ++ (atomAsHTML (snd tup)) ++ "</td>"
36tupleSetAsHTML :: RelationTupleSet -> String
37tupleSetAsHTML tupSet = HS.foldr folder "" tupSet
38 where
39 folder tuple acc = acc ++ tupleAsHTML tuple