toHTML n (IFace ns xs rs ss ds ws bs _) = text "<html><body>\n" $$
                                          text ("<h2>API for module "++n++"</h2>\n") $$
                                          section ns "Imported modules" prImports $$
                                          section xs "Default declarations" (hpr ',') $$
                                          section ke' "Kind declarations" (pr . flip Types []) $$
                                          section ds' "Type declarations" (pr . Types [] . map addSubs) $$
                                          section te' "Toplevel declarations" (prTop . stripTopdecls) $$
                                          text "</html>"
                      
  where section xs header f             = if null xs 
                                           then empty 
                                           else text ("<h4>"++header++"</h4>\n<pre>") $$ f xs $$ text "</pre>"
        
        Types ke ds'                    = ds
        ke'                             = [(n,k) | (n,k) <- ke, notElem n (dom ds')]
        Binds _ te _                    = bs
        addSubs (n,DData vs _ cs)       = (n,DData vs (map (\(_,Constr (s:_) _ _) -> s) cs1) cs2)
         where (cs1,cs2)                = partition (isGenerated . fst) cs
        addSubs (n,DRec b vs _ ss)      = (n,DRec b vs (map snd ss1) ss2)
         where (ss1, ss2)               = partition (isGenerated . fst) (map stripStar ss)
        addSubs d                       = d
        stripStar (n,Scheme rh ps ke)   = (n,Scheme rh ps (filter (( /= Star) . snd) ke))
        te'                             = map (sV . stripStar)  (filter (not . isGenerated . fst) te)
        stripTopdecls te                = bs1 ++ bs2
         where (bs1,bs2)                = partition (flip elem ws . fst ) te
toHTML n (IFace ns xs rs ss ds ws bs _) = text "<html><body>\n" $$
                                          text ("<h2>API for module "++n++"</h2>\n") $$
                                          section ns "Imported modules" prImports $$
                                          section xs "Default declarations" (hpr ',') $$
                                          section ke' "Kind declarations" (pr . flip Types []) $$
                                          section ds' "Type declarations" (pr . Types [] . map addSubs) $$
                                          section te' "Toplevel declarations" (prTop . stripTopdecls) $$
                                          text "</html>"
                      
  where section xs header f             = if null xs 
                                           then empty 
                                           else text ("<h4>"++header++"</h4>\n<pre>") $$ f xs $$ text "</pre>"
        
        Types ke ds'                    = ds
        ke'                             = [(n,k) | (n,k) <- ke, notElem n (dom ds')]
        Binds _ te _                    = bs
        addSubs (n,DData vs _ cs)       = (n,DData vs (map (\(_,Constr (s:_) _ _) -> s) cs1) cs2)
         where (cs1,cs2)                = partition (isGenerated . fst) cs
        addSubs (n,DRec b vs _ ss)      = (n,DRec b vs (map snd ss1) ss2)
         where (ss1, ss2)               = partition (isGenerated . fst) (map stripStar ss)
        addSubs d                       = d
        stripStar (n,Scheme rh ps ke)   = (n,Scheme rh ps (filter (( /= Star) . snd) ke))
        te'                             = map (sV . stripStar)  (filter (not . isGenerated . fst) te)
        stripTopdecls te                = bs1 ++ bs2
         where (bs1,bs2)                = partition (flip elem ws . fst ) te
toHTML n (IFace ns xs rs ss ds ws bs _) = text "<html><body>\n" $$
                                          text ("<h2>API for module "++n++"</h2>\n") $$
                                          section ns "Imported modules" prImports $$
                                          section xs "Default declarations" (hpr ',') $$
                                          section ke' "Kind declarations" (pr . flip Types []) $$
                                          section ds' "Type declarations" (pr . Types [] . map addSubs) $$
                                          section te' "Toplevel declarations" (prTop . stripTopdecls) $$
                                          text "</html>"
                      
  where section xs header f             = if null xs 
                                           then empty 
                                           else text ("<h4>"++header++"</h4>\n<pre>") $$ f xs $$ text "</pre>"
        
        Types ke ds'                    = ds
        ke'                             = [(n,k) | (n,k) <- ke, notElem n (dom ds')]
        Binds _ te _                    = bs
        addSubs (n,DData vs _ cs)       = (n,DData vs (map (\(_,Constr (s:_) _ _) -> s) cs1) cs2)
         where (cs1,cs2)                = partition (isGenerated . fst) cs
        addSubs (n,DRec b vs _ ss)      = (n,DRec b vs (map snd ss1) ss2)
         where (ss1, ss2)               = partition (isGenerated . fst) (map stripStar ss)
        addSubs d                       = d
        stripStar (n,Scheme rh ps ke)   = (n,Scheme rh ps (filter (( /= Star) . snd) ke))
        te'                             = map (sV . stripStar)  (filter (not . isGenerated . fst) te)
        stripTopdecls te                = bs1 ++ bs2
         where (bs1,bs2)                = partition (flip elem ws . fst ) te