module HaddockDevHelp(ppDevHelpFile) where
import HsSyn hiding(Doc)
import qualified Map
#if __GLASGOW_HASKELL__ < 503
import Pretty
#else
import Text.PrettyPrint
import Data.Char
#endif
import Maybe ( fromMaybe )
import HaddockModuleTree
import HaddockUtil
import HaddockTypes
ppDevHelpFile :: FilePath -> String -> Maybe String -> [(Module,Interface)] -> IO ()
ppDevHelpFile odir doctitle maybe_package ifaces = do
let devHelpFile = package++".devhelp"
tree = mkModuleTree [ (mod, iface_package iface, toDescription iface)
| (mod, iface) <- ifaces ]
doc =
text "" $$
(text "text doctitle<>
text "\" link=\""<>text contentsHtmlFile<>text"\" author=\"\" name=\""<>text package<>text "\">") $$
text "" $$
nest 4 (ppModuleTree [] tree) $+$
text "" $$
text "" $$
nest 4 (ppList index) $+$
text "" $$
text ""
writeFile (pathJoin [odir, devHelpFile]) (render doc)
where
package = fromMaybe "pkg" maybe_package
ppModuleTree :: [String] -> [ModuleTree] -> Doc
ppModuleTree ss [x] = ppNode ss x
ppModuleTree ss (x:xs) = ppNode ss x $$ ppModuleTree ss xs
ppModuleTree _ [] = error "HaddockHH.ppHHContents.fn: no module trees given"
ppNode :: [String] -> ModuleTree -> Doc
ppNode ss (Node s leaf _pkg _short ts) =
case ts of
[] -> text "ppAttribs<>text "/>"
ts ->
text "ppAttribs<>text ">" $$
nest 4 (ppModuleTree (s:ss) ts) $+$
text ""
where
ppLink | leaf = text (moduleHtmlFile mdl)
| otherwise = empty
ppAttribs = text "name="<>doubleQuotes (text s)<+>text "link="<>doubleQuotes ppLink
mdl = foldr (++) "" (s' : map ('.':) ss')
(s':ss') = reverse (s:ss)
-- reconstruct the module name
index :: [(HsName, [Module])]
index = Map.toAscList (foldr getIfaceIndex Map.empty ifaces)
getIfaceIndex (mdl,iface) fm =
Map.unionWith (++) (Map.fromListWith (flip (++)) [(name, [mdl]) | (name, Qual mdl' _) <- Map.toAscList (iface_env iface), mdl == mdl']) fm
ppList [] = empty
ppList ((name,refs):mdls) =
ppReference name refs $$
ppList mdls
ppReference name [] = empty
ppReference name (Module mdl:refs) =
text "text (escapeStr (show name))<>text"\" link=\""<>text (nameHtmlRef mdl name)<>text"\"/>" $$
ppReference name refs