--
-- Haddock - A Haskell Documentation Tool
--
-- (c) Simon Marlow 2003
--
module Haddock.Backends.DevHelp (ppDevHelpFile) where
import Haddock.ModuleTree
import Haddock.Types hiding (Doc)
import Haddock.Utils
import Module
import Name ( Name, nameModule, getOccString, nameOccName )
#if __GLASGOW_HASKELL__ < 609
import PackageConfig (stringToPackageId)
#endif
import Data.Maybe ( fromMaybe )
import qualified Data.Map as Map
import Text.PrettyPrint
ppDevHelpFile :: FilePath -> String -> Maybe String -> [Interface] -> IO ()
ppDevHelpFile odir doctitle maybe_package ifaces = do
let devHelpFile = package++".devhelp"
tree = mkModuleTree True [ (ifaceMod iface, toDescription iface) | 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 _ _short ts) =
case ts of
[] -> text "ppAttribs<>text "/>"
_ ->
text "ppAttribs<>text ">" $$
nest 4 (ppModuleTree (s:ss) ts) $+$
text ""
where
ppLink | leaf = text (moduleHtmlFile (mkModule (stringToPackageId "")
(mkModuleName 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 :: [(Name, [Module])]
index = Map.toAscList (foldr getModuleIndex Map.empty ifaces)
getModuleIndex iface fm =
Map.unionWith (++) (Map.fromListWith (flip (++)) [(name, [mdl]) | name <- ifaceExports iface, nameModule name == mdl]) fm
where mdl = ifaceMod iface
ppList :: [(Name, [Module])] -> Doc
ppList [] = empty
ppList ((name,refs):mdls) =
ppReference name refs $$
ppList mdls
ppReference :: Name -> [Module] -> Doc
ppReference _ [] = empty
ppReference name (mdl:refs) =
text "text (escapeStr (getOccString name))<>text"\" link=\""<>text (nameHtmlRef mdl (nameOccName name))<>text"\"/>" $$
ppReference name refs