Generate autodoc index with SML/NJ HTML lib
[hcoop/domtool2.git] / src / autodoc.sml
... / ...
CommitLineData
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19(* Generating HTML documentation automatically *)
20
21structure Autodoc :> AUTODOC = struct
22
23open Ast HTML HtmlPrint
24open PD
25
26fun uppercase s =
27 case s of
28 "" => s
29 | _ => str (Char.toUpper (String.sub (s, 0)))
30 ^ String.extract (s, 1, NONE)
31
32fun check' G fname =
33 let
34 val prog = Parse.parse fname
35 in
36 if !ErrorMsg.anyErrors then
37 G
38 else
39 Tycheck.checkFile G (Defaults.tInit ()) prog
40 end
41
42fun autodoc {outdir, infiles} =
43 let
44 val (prov, infiles) = Order.order infiles
45 val _ = HtmlPrint.setProviders prov
46
47 val G = foldl (fn (fname, G) => check' G fname) Env.empty infiles
48
49 fun annotate_decl d =
50 case d of
51 DVal (name, NONE, e) =>
52 (case Env.lookupVal G name of
53 NONE => d
54 | SOME t => DVal (name, SOME t, e))
55 | _ => d
56
57 fun modify file =
58 let
59 val file' = #file (OS.Path.splitDirFile file)
60 val file' = #base (OS.Path.splitBaseExt file')
61 in
62 file'
63 end
64
65 fun doFile file =
66 let
67 val (desc, decls, _) = Parse.parse file
68
69 val file' = modify file
70
71 val title = "Domtool Module " ^ uppercase file'
72
73 val outf = TextIO.openOut (outdir ^ "/" ^ file' ^ ".html")
74
75 (*fun doDecl (d, desc, _) =
76 Option.app (fn desc => (TextIO.output (outf, "<p>");
77 TextIO.output (outf, desc);
78 TextIO.output (outf, "</p>\n"))) desc*)
79
80 val body = Hn {n = 1,
81 align = NONE,
82 content = PCDATA title}
83
84 val body = case desc of
85 NONE => body
86 | SOME desc => BlockList [body,
87 P {align = NONE,
88 content = PCDATA desc}]
89
90 val summaries = foldr (fn ((d, desc, _), summaries) =>
91 HtmlPrint.output (p_decl_fref (annotate_decl d))
92 :: BR {clear = NONE}
93 :: summaries)
94 [] decls
95
96 val entries = map (fn (d, desc, _) =>
97 let
98 val cblock = HtmlPrint.output (p_decl (annotate_decl d))
99
100 val dblock = case desc of
101 NONE => TextBlock (PCDATA "")
102 | SOME desc => BLOCKQUOTE (TextBlock (PCDATA desc))
103 in
104 BlockList [P {align = NONE,
105 content = TT cblock},
106 dblock]
107 end) decls
108
109 val body = BlockList (body
110 :: HR {align = NONE,
111 noshade = false,
112 size = NONE,
113 width = NONE}
114 :: TextBlock (TT (TextList summaries))
115 :: HR {align = NONE,
116 noshade = false,
117 size = NONE,
118 width = NONE}
119 :: entries)
120
121 val html = HTML {version = NONE,
122 head = [Head_TITLE title],
123 body = BODY {background = NONE,
124 bgcolor = NONE,
125 text = NONE,
126 link = NONE,
127 vlink = NONE,
128 alink = NONE,
129 content = body}}
130 in
131 PrHTML.prHTML {putc = (fn ch => TextIO.output1 (outf, ch)),
132 puts = (fn s => TextIO.output (outf, s))} html;
133 TextIO.closeOut outf
134 end
135
136 val title = "Domtool Module Index"
137
138 val items = map (fn file =>
139 let
140 val file' = modify file
141 in
142 LI {ty = NONE,
143 value = NONE,
144 content = TextBlock (A {name = NONE,
145 href = SOME (file' ^ ".html"),
146 rel = NONE,
147 rev = NONE,
148 title = NONE,
149 content = PCDATA (uppercase file')})}
150 end) infiles
151
152 val index = HTML {version = NONE,
153 head = [Head_TITLE title],
154 body = BODY {background = NONE,
155 bgcolor = NONE,
156 text = NONE,
157 link = NONE,
158 vlink = NONE,
159 alink = NONE,
160 content = UL {ty = NONE,
161 compact = false,
162 content = items}}}
163
164 val outf = TextIO.openOut (outdir ^ "/index.html")
165 in
166 PrHTML.prHTML {putc = (fn ch => TextIO.output1 (outf, ch)),
167 puts = (fn s => TextIO.output (outf, s))} index;
168 TextIO.closeOut outf;
169
170 app doFile infiles
171 end
172
173end