aboutsummaryrefslogtreecommitdiff
path: root/src/HaddockHH.hs
blob: d4dc07e89c4ef3cc3b5b266493fd64d69005c98c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module HaddockHH(ppHHContents, ppHHIndex, ppHHProject) where

import HsSyn hiding(Doc)

#if __GLASGOW_HASKELL__ < 503
import Pretty
import FiniteMap
#else
import Text.PrettyPrint
import Data.FiniteMap
import Data.Char
#endif

import Maybe	( fromMaybe )
import HaddockModuleTree
import HaddockUtil
import HaddockTypes


ppHHContents :: FilePath -> String -> Maybe String -> [ModuleTree] -> IO ()
ppHHContents odir doctitle maybe_package tree = do
  let contentsHHFile = package++".hhc"

      html =
      	text "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">" $$
	text "<HTML>" $$
	text "<HEAD>" $$
	text "<META name=\"GENERATOR\" content=\"Haddock\">" $$
	text "<!-- Sitemap 1.0 -->" $$
	text "</HEAD><BODY>" $$
	ppModuleTree tree $$
	text "</BODY><HTML>"
  writeFile (odir ++ pathSeparator:contentsHHFile) (render html)
  where
	package = fromMaybe "pkg" maybe_package
	
	ppModuleTree :: [ModuleTree] -> Doc
	ppModuleTree ts =
		text "<OBJECT type=\"text/site properties\">" $$
		text "<PARAM name=\"FrameName\" value=\"main\">" $$
		text "</OBJECT>" $$
		text "<UL>" $+$
		nest 4 (text "<LI>" <> nest 4
		                (text "<OBJECT type=\"text/sitemap\">" $$
		                 nest 4 (text "<PARAM name=\"Name\" value=\""<>text doctitle<>text "\">" $$
		                         text "<PARAM name=\"Local\" value=\"index.html\">") $$
		                 text "</OBJECT>") $+$
		        text "</LI>" $$
		        text "<UL>" $+$
		        nest 4 (fn [] ts) $+$
		        text "</UL>") $+$
		text "</UL>"

	fn :: [String] -> [ModuleTree] -> Doc
	fn ss [x]    = ppNode ss x
	fn ss (x:xs) = ppNode ss x $$ fn ss xs
        fn _  []     = error "HaddockHH.ppHHContents.fn: no module trees given"

	ppNode :: [String] -> ModuleTree -> Doc
	ppNode ss (Node s leaf _pkg []) =
	  ppLeaf s ss leaf
	ppNode ss (Node s leaf _pkg ts) =
	  ppLeaf s ss leaf $$
	  text "<UL>" $+$
	  nest 4 (fn (s:ss) ts) $+$
	  text "</UL>"

	ppLeaf s ss isleaf  =
		text "<LI>" <> nest 4
			(text "<OBJECT type=\"text/sitemap\">" $$
			 text "<PARAM name=\"Name\" value=\"" <> text s <> text "\">" $$
			 (if isleaf then text "<PARAM name=\"Local\" value=\"" <> text (moduleHtmlFile mdl) <> text "\">" else empty) $$
			 text "</OBJECT>") $+$
		text "</LI>"
		where 
			mdl = foldr (++) "" (s' : map ('.':) ss')
			(s':ss') = reverse (s:ss)
			-- reconstruct the module name
		
-------------------------------
ppHHIndex :: FilePath -> Maybe String -> [(Module,Interface)] -> IO ()
ppHHIndex odir maybe_package ifaces = do
  let indexHHFile = package++".hhk"
  
      html = 
      	text "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">" $$
	text "<HTML>" $$
	text "<HEAD>" $$
	text "<META name=\"GENERATOR\" content=\"Haddock\">" $$
	text "<!-- Sitemap 1.0 -->" $$
	text "</HEAD><BODY>" $$
	text "<UL>" $+$
	nest 4 (ppList index) $+$
	text "</UL>" $$
	text "</BODY><HTML>"
  writeFile (odir ++ pathSeparator:indexHHFile) (render html)
  where
	package = fromMaybe "pkg" maybe_package
  	
	index :: [(HsName, [Module])]
	index = fmToList (foldr getIfaceIndex emptyFM ifaces)

	getIfaceIndex (mdl,iface) fm =
		addListToFM_C (++) fm [(name, [mdl]) | (name, Qual mdl' _) <- fmToList (iface_env iface), mdl == mdl']
	
	ppList [] = empty
	ppList ((name,refs):mdls)  =
		text "<LI>" <> nest 4
				(text "<OBJECT type=\"text/sitemap\">" $$
				 text "<PARAM name=\"Name\" value=\"" <> text (show name) <> text "\">" $$
				 ppReference name refs $$
				 text "</OBJECT>") $+$
		text "</LI>" $$
		ppList mdls

	ppReference name [] = empty
	ppReference name (Module mdl:refs) =
		text "<PARAM name=\"Local\" value=\"" <> text (nameHtmlRef mdl name) <> text "\">" $$
		ppReference name refs


ppHHProject :: FilePath -> String -> Maybe String -> [(Module,Interface)] -> [FilePath] -> IO ()
ppHHProject odir doctitle maybe_package ifaces pkg_paths = do
  let projectHHFile = package++".hhp"
      doc =
        text "[OPTIONS]" $$
        text "Compatibility=1.1 or later" $$
        text "Compiled file=" <> text package <> text ".chm" $$
        text "Contents file=" <> text package <> text ".hhc" $$
        text "Default topic=" <> text contentsHtmlFile $$
        text "Display compile progress=No" $$
        text "Index file=" <> text package <> text ".hhk" $$
        text "Title=" <> text doctitle $$
	space $$
        text "[FILES]" $$
        ppMods ifaces $$
        text contentsHtmlFile $$
        text indexHtmlFile $$
        ppIndexFiles chars $$
        ppLibFiles ("":pkg_paths)
  writeFile (odir ++ pathSeparator:projectHHFile) (render doc)
  where
    package = fromMaybe "pkg" maybe_package
	
    ppMods [] = empty
    ppMods ((Module mdl,_):ifaces) =
        text (moduleHtmlFile mdl) $$
        ppMods ifaces
		
    ppIndexFiles []     = empty
    ppIndexFiles (c:cs) =
        text (subIndexHtmlFile c) $$
        ppIndexFiles cs
        
    ppLibFiles []           = empty
    ppLibFiles (path:paths) =
        ppLibFile cssFile   $$
    	ppLibFile iconFile  $$
    	ppLibFile jsFile    $$
    	ppLibFile plusFile  $$
        ppLibFile minusFile $$
        ppLibFiles paths
        where
            toPath fname | null path = fname
	                 | otherwise = path++pathSeparator:fname
            ppLibFile fname = text (toPath fname)

    chars :: [Char]
    chars = keysFM (foldr getIfaceIndex emptyFM ifaces)

    getIfaceIndex (mdl,iface) fm =
        addListToFM fm [(toUpper (head (show name)),()) | (name, Qual mdl' _) <- fmToList (iface_env iface), mdl == mdl']