summaryrefslogtreecommitdiff
path: root/ocamldoc/odoc_man.ml
blob: ae965673a138733c756b033702e252ca0933424e (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
(***********************************************************************)
(*                             OCamldoc                                *)
(*                                                                     *)
(*            Maxence Guesdon, projet Cristal, INRIA Rocquencourt      *)
(*                                                                     *)
(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)


(** The man pages generator. *)
open Odoc_info 
open Parameter
open Value
open Type
open Exception
open Class 
open Module
open Search


(** A class used to get a [text] for info structures. *)
class virtual info =
  object (self)
    (** The list of pairs [(tag, f)] where [f] is a function taking
       the [text] associated to [tag] and returning man code. 
       Add a pair here to handle a tag.*)
    val mutable tag_functions = ([] : (string * (Odoc_info.text -> string)) list)

    (** Return man code for a [text]. *)
    method virtual man_of_text : Odoc_info.text -> string

    (** Groff string for an author list. *)
    method man_of_author_list l =
      match l with
	[] ->
	  ""
      | _ ->
	  ".B \""^Odoc_messages.authors^"\"\n:\n"^
	  (String.concat ", " l)^
	  "\n.sp\n"

    (** Groff string for the given optional version information.*)
    method man_of_version_opt v_opt =
      match v_opt with
	None -> ""
      | Some v -> ".B \""^Odoc_messages.version^"\"\n:\n"^v^"\n.sp\n"

    (** Groff string for the given optional since information.*)
    method man_of_since_opt s_opt =
      match s_opt with
	None -> ""
      | Some s -> ".B \""^Odoc_messages.since^"\"\n"^s^"\n.sp\n"

    (** Groff string for the given list of raised exceptions.*)
    method man_of_raised_exceptions l =
      match l with
	[] -> ""
      | (s, t) :: [] -> ".B \""^Odoc_messages.raises^" "^s^"\"\n"^(self#man_of_text t)^"\n.sp\n"
      | _ ->
	  ".B \""^Odoc_messages.raises^"\"\n"^
	  (String.concat ""
	     (List.map
		(fun (ex, desc) -> ".TP\n.B \""^ex^"\"\n"^(self#man_of_text desc)^"\n")
		l
	     )
	  )^"\n.sp\n"

    (** Groff string for the given "see also" reference. *)
    method man_of_see (see_ref, t)  =
      let t_ref = 
	match see_ref with
	  Odoc_info.See_url s -> [ Odoc_info.Link (s, t) ]
	| Odoc_info.See_file s -> (Odoc_info.Code s) :: (Odoc_info.Raw " ") :: t
	| Odoc_info.See_doc s -> (Odoc_info.Italic [Odoc_info.Raw s]) :: (Odoc_info.Raw " ") :: t
      in
      self#man_of_text t_ref

    (** Groff string for the given list of "see also" references.*)
    method man_of_sees l =
      match l with
	[] -> ""
      | see :: [] -> ".B \""^Odoc_messages.see_also^"\"\n"^(self#man_of_see see)^"\n.sp\n"
      | _ ->
	  ".B \""^Odoc_messages.see_also^"\"\n"^
	  (String.concat ""
	     (List.map
		(fun see -> ".TP\n \"\"\n"^(self#man_of_see see)^"\n")
		l
	     )
	  )^"\n.sp\n"

    (** Groff string for the given optional return information.*)
    method man_of_return_opt return_opt =
      match return_opt with
	None -> ""
      | Some s -> ".B "^Odoc_messages.returns^"\n"^(self#man_of_text s)^"\n.sp\n"

    (** Return man code for the given list of custom tagged texts. *)
    method man_of_custom l =
      let buf = Buffer.create 50 in
      List.iter
	(fun (tag, text) ->
	  try
	    let f = List.assoc tag tag_functions in
	    Buffer.add_string buf (f text)
	  with
	    Not_found ->
	      Odoc_info.warning (Odoc_messages.tag_not_handled tag)
	)
	l;
      Buffer.contents buf

    (** Return the groff string to display an optional info structure. *)
    method man_of_info info_opt =
	match info_opt with
	None ->
	  ""
      | Some info ->
	  let module M = Odoc_info in
	  (match info.M.i_deprecated with
	    None -> ""
	  | Some d -> ".B \""^Odoc_messages.deprecated^"\"\n"^(self#man_of_text d)^"\n.sp\n")^
	  (match info.M.i_desc with
	    None -> "" 
	  | Some d when d = [Odoc_info.Raw ""] -> ""
	  | Some d -> (self#man_of_text d)^"\n.sp\n"
	  )^
	  (self#man_of_author_list info.M.i_authors)^
	  (self#man_of_version_opt info.M.i_version)^
	  (self#man_of_since_opt info.M.i_since)^
	  (self#man_of_raised_exceptions info.M.i_raised_exceptions)^
	  (self#man_of_return_opt info.M.i_return_value)^
	  (self#man_of_sees info.M.i_sees)^
	  (self#man_of_custom info.M.i_custom)
  end

(** This class is used to create objects which can generate a simple html documentation. *)
class man =
  object (self)
    inherit info

    (** Get a file name from a module or class complete name. *)
    method file_name name = name^".man"

    (** Escape special sequences of characters in a string. *)
    method escape (s : string) = s

    (** Open a file for output. Add the target directory.*)
    method open_out file =
      let f = Filename.concat !Odoc_args.target_dir file in
      open_out f
      
    (** Return the groff string for a text, without correction of blanks. *)
    method private man_of_text2 t = String.concat "" (List.map self#man_of_text_element t)

    (** Return the groff string for a text, with blanks corrected. *)
    method man_of_text t =
      let s = self#man_of_text2 t in
      let s2 = Str.global_replace (Str.regexp "\n[ ]*") "\n" s in
      Str.global_replace (Str.regexp "\n\n") "\n" s2

    (** Return the groff string for a text element. *)
    method man_of_text_element te =
      match te with
      |	Odoc_info.Raw s -> s
      |	Odoc_info.Code s -> 
	  let s2 = "\n.B "^(Str.global_replace (Str.regexp "\n") "\n.B " (self#escape s))^"\n" in
	  s2
      |	Odoc_info.CodePre s -> 
	  let s2 = "\n.B "^(Str.global_replace (Str.regexp "\n") "\n.B " (self#escape s))^"\n" in
	  s2
      |	Odoc_info.Verbatim s -> self#escape s
      |	Odoc_info.Bold t
      |	Odoc_info.Italic t
      |	Odoc_info.Emphasize t
      |	Odoc_info.Center t
      |	Odoc_info.Left t
      |	Odoc_info.Right t -> self#man_of_text2 t
      |	Odoc_info.List tl ->
	  (String.concat ""
	     (List.map
		(fun t -> ".TP\n \"\"\n"^(self#man_of_text2 t)^"\n")
		tl
	     )
	  )^"\n"
      |	Odoc_info.Enum tl ->
	  (String.concat ""
	     (List.map
		(fun t -> ".TP\n \"\"\n"^(self#man_of_text2 t)^"\n")
		tl
	     )
	  )^"\n"
      |	Odoc_info.Newline ->
	  "\n.sp\n"
      |	Odoc_info.Block t ->
	  "\n.sp\n"^(self#man_of_text2 t)^"\n.sp\n"
      |	Odoc_info.Title (n, l_opt, t) ->
	  self#man_of_text2 [Odoc_info.Code (Odoc_info.string_of_text t)]
      |	Odoc_info.Latex _ ->
	  (* don't care about LaTeX stuff in HTML. *)
	  ""
      |	Odoc_info.Link (s, t) ->
	  self#man_of_text2 t
      |	Odoc_info.Ref (name, _) ->
	  self#man_of_text_element 
	    (Odoc_info.Code (Odoc_info.use_hidden_modules name))
      |	Odoc_info.Superscript t ->
	  "^{"^(self#man_of_text2 t)
      |	Odoc_info.Subscript t ->
	  "_{"^(self#man_of_text2 t)

    (** Groff string to display code. *)
    method man_of_code s = self#man_of_text [ Code s ]

    (** Take a string and return the string where fully qualified idents 
       have been replaced by idents relative to the given module name.*)
    method relative_idents m_name s =
      let f str_t = 
	let match_s = Str.matched_string str_t in
	Odoc_info.apply_if_equal 
	  Odoc_info.use_hidden_modules 
	  match_s
	  (Name.get_relative m_name match_s)
      in
      let s2 = Str.global_substitute
	  (Str.regexp "\\([A-Z]\\([a-zA-Z_'0-9]\\)*\\.\\)+\\([a-z][a-zA-Z_'0-9]*\\)")
	  f
	  s
      in
      s2

    (** Groff string to display a [Types.type_expr].*)
    method man_of_type_expr m_name t =
      let s = String.concat "\n"
	  (Str.split (Str.regexp "\n") (Odoc_misc.string_of_type_expr t))
      in
      let s2 = Str.global_replace (Str.regexp "\n") "\n.B " s in
      "\n.B "^(self#relative_idents m_name s2)^"\n"

    (** Groff string to display a [Types.class_type].*)
    method man_of_class_type_expr m_name t =
      let s = String.concat "\n"
	  (Str.split (Str.regexp "\n") (Odoc_misc.string_of_class_type t))
      in
      let s2 = Str.global_replace (Str.regexp "\n") "\n.B " s in
      "\n.B "^(self#relative_idents m_name s2)^"\n"

    (** Groff string to display a [Types.type_expr list].*)
    method man_of_type_expr_list m_name sep l =
      let s = Odoc_misc.string_of_type_list sep l in
      let s2 = Str.global_replace (Str.regexp "\n") "\n.B " s in
      "\n.B "^(self#relative_idents m_name s2)^"\n"

    (** Groff string to display a [Types.module_type]. *)
    method man_of_module_type m_name t =
      let s = String.concat "\n"
	  (Str.split (Str.regexp "\n") (Odoc_misc.string_of_module_type t))
      in
      let s2 = Str.global_replace (Str.regexp "\n") "\n.B " s in
      "\n.B "^(self#relative_idents m_name s2)^"\n"

    (** Groff string code for a value. *)
    method man_of_value v =
      Odoc_info.reset_type_names () ;
      "\n.I val "^(Name.simple v.val_name)^" \n: "^
      (self#man_of_type_expr (Name.father v.val_name) v.val_type)^
      ".sp\n"^
      (self#man_of_info v.val_info)^
      "\n.sp\n"

    (** Groff string code for an exception. *)
    method man_of_exception e =
      Odoc_info.reset_type_names () ;
      "\n.I exception "^(Name.simple e.ex_name)^" \n"^
      (match e.ex_args with
	[] -> ""
      |	_ -> 
	  ".B of "^
	  (self#man_of_type_expr_list (Name.father e.ex_name) " * " e.ex_args)
      )^
      (match e.ex_alias with
	None -> ""
      | Some ea -> " = "^
	  (
	   match ea.ea_ex with
	     None -> ea.ea_name
	   | Some e -> e.ex_name
	  )
      )^
      "\n.sp\n"^
      (self#man_of_info e.ex_info)^
      "\n.sp\n"

    (** Groff string for a type. *)
    method man_of_type t =
      Odoc_info.reset_type_names () ;
      let father = Name.father t.ty_name in
      ".I type "^
      (match t.ty_parameters with
	[] -> ""
      |	tp :: [] -> (Odoc_misc.string_of_type_expr tp)
      |	l ->
	  (self#man_of_type_expr_list father ", " l)
      )^
      (match t.ty_parameters with [] -> "" | _ -> ".I ")^(Name.simple t.ty_name)^" \n"^
      (match t.ty_manifest with None -> "" | Some typ -> "= "^(self#man_of_type_expr father typ))^
      (match t.ty_kind with
	Type_abstract -> 
	  ""
      |	Type_variant l ->
	  "=\n "^
	  (String.concat ""
	     (List.map 
		(fun constr ->
		  "| "^constr.vc_name^
		  (match constr.vc_args, constr.vc_text with
		    [], None -> "\n " 
		  | [], (Some t) -> "  (* "^(self#man_of_text t)^" *)\n "
		  | l, None -> 
		      "\n.B of "^(self#man_of_type_expr_list father " * " l)^" "
		  | l, (Some t) ->
		      "\n.B of "^(self#man_of_type_expr_list father " * " l)^
		      ".I \"  \"\n"^
		      "(* "^(self#man_of_text t)^" *)\n "
		  )
		)
		l
	     )
	  )
      |	Type_record l ->
	  "= {"^
	  (String.concat ""
	     (List.map 
		(fun r ->
		  (if r.rf_mutable then "\n\n.B mutable \n" else "\n ")^
		  r.rf_name^" : "^(self#man_of_type_expr father r.rf_type)^";"^
		  (match r.rf_text with
		    None ->
		      ""
		  | Some t ->
		      "  (* "^(self#man_of_text t)^" *) "
		  )^""
		)
		l
	     )
	  )^
	  "\n }\n"
      )^
      "\n.sp\n"^(self#man_of_info t.ty_info)^
      "\n.sp\n"

    (** Groff string for a class attribute. *)
    method man_of_attribute a =
      ".I val "^
      (if a.att_mutable then Odoc_messages.mutab^" " else "")^
      (Name.simple a.att_value.val_name)^" : "^
      (self#man_of_type_expr (Name.father a.att_value.val_name) a.att_value.val_type)^
      "\n.sp\n"^(self#man_of_info a.att_value.val_info)^
      "\n.sp\n"

    (** Groff string for a class method. *)
    method man_of_method m =
      ".I method "^
      (if m.met_private then "private " else "")^
      (if m.met_virtual then "virtual " else "")^
      (Name.simple m.met_value.val_name)^" : "^
      (self#man_of_type_expr (Name.father m.met_value.val_name) m.met_value.val_type)^
      "\n.sp\n"^(self#man_of_info m.met_value.val_info)^
      "\n.sp\n"

    (** Groff for a list of parameters. *)
    method man_of_parameter_list m_name l =
      match l with
	[] ->
	  ""
      | _ ->
	  "\n.B "^Odoc_messages.parameters^": \n"^
	  (String.concat ""
	     (List.map
		(fun p ->
		  ".TP\n"^
		  "\""^(Parameter.complete_name p)^"\"\n"^
		  (self#man_of_type_expr m_name (Parameter.typ p))^"\n"^
		  (self#man_of_parameter_description p)^"\n"
		)
		l
	     )
	  )^"\n"

    (** Groff for the description of a function parameter. *)
    method man_of_parameter_description p =
      match Parameter.names p with
	[] ->
	  ""
      | name :: [] ->
	  (
           (* Only one name, no need for label for the description. *)
	   match Parameter.desc_by_name p name with
	     None -> ""
	   | Some t -> "\n "^(self#man_of_text t)
	  )
      | l ->
          (*  A list of names, we display those with a description. *)
	  String.concat ""
	    (List.map
	       (fun n ->
		 match Parameter.desc_by_name p n with
		   None -> ""
		 | Some t -> (self#man_of_code (n^" : "))^(self#man_of_text t)
	       )
	       l
	    )

    (** Groff string for a list of module parameters. *)
    method man_of_module_parameter_list m_name l =
      match l with
	[] ->
	  ""
      | _ ->
	  ".B \""^Odoc_messages.parameters^":\"\n"^
	  (String.concat ""
	     (List.map
		(fun (p, desc_opt) ->
		  ".TP\n"^
		  "\""^p.mp_name^"\"\n"^
		  (self#man_of_module_type m_name p.mp_type)^"\n"^
		  (match desc_opt with
		    None -> ""
		  | Some t -> self#man_of_text t)^
		  "\n"
		)
		l
	     )
	  )^"\n\n"

    (** Groff string for a class. *)
    method man_of_class c =
      let buf = Buffer.create 32 in
      let p = Printf.bprintf in
      Odoc_info.reset_type_names () ;
      let father = Name.father c.cl_name in
      p buf ".I class %s"
	(if c.cl_virtual then "virtual " else "");
      (
       match c.cl_type_parameters with
	 [] -> ()
       | l -> p buf "[%s.I] " (Odoc_misc.string_of_type_list ", " l)
      );
      p buf "%s : %s" 
	(Name.simple c.cl_name)
	(self#man_of_class_type_expr (Name.father c.cl_name) c.cl_type);
      p buf "\n.sp\n%s\n.sp\n" (self#man_of_info c.cl_info);
      Buffer.contents buf

    (** Groff string for a class type. *)
    method man_of_class_type ct =
      let buf = Buffer.create 32 in
      let p = Printf.bprintf in
      Odoc_info.reset_type_names () ;
      p buf ".I class type %s"
	(if ct.clt_virtual then "virtual " else "");
      (
       match ct.clt_type_parameters with
	[] -> ()
      |	l -> p buf "[%s.I ] " (Odoc_misc.string_of_type_list ", " l)
      );
      p buf "%s = %s" 
	(Name.simple ct.clt_name)
	(self#man_of_class_type_expr (Name.father ct.clt_name) ct.clt_type);
      p buf "\n.sp\n%s\n.sp\n" (self#man_of_info ct.clt_info);
      Buffer.contents buf

    (** Groff string for a module. *)
    method man_of_module m =
      ".I module "^(Name.simple m.m_name)^
      " : "^(self#man_of_module_type (Name.father m.m_name) m.m_type)^
      "\n.sp\n"^(self#man_of_info m.m_info)^"\n.sp\n"

    (** Groff string for a module type. *)
    method man_of_modtype mt =
      ".I module type "^(Name.simple mt.mt_name)^
      " = "^
      (match mt.mt_type with 
	None -> ""
      | Some t -> self#man_of_module_type (Name.father mt.mt_name) t
      )^
      "\n.sp\n"^(self#man_of_info mt.mt_info)^"\n.sp\n"

    (** Groff string for a module comment.*)
    method man_of_module_comment text =
      "\n.pp\n"^
      (self#man_of_text [Code ("=== "^(Odoc_misc.string_of_text text)^" ===")])^
      "\n.pp\n"

    (** Groff string for a class comment.*)
    method man_of_class_comment text =
      "\n.pp\n"^
      (self#man_of_text [Code ("=== "^(Odoc_misc.string_of_text text)^" ===")])^
      "\n.pp\n"

    (** Groff string for an included module. *)
    method man_of_included_module m_name im =
      ".I include "^
      (
       match im.im_module with
	 None -> im.im_name
       | Some mmt ->
	   let name = 
	     match mmt with
	       Mod m -> m.m_name
	     | Modtype mt -> mt.mt_name
	   in
	   self#relative_idents m_name name
      )^
      "\n.sp\n"

    (** Generate the man page for the given class.*)
    method generate_for_class cl =
      Odoc_info.reset_type_names () ;
      let date = Unix.time () in
      let file = self#file_name cl.cl_name in
      try
	let chanout = self#open_out file in
	output_string chanout
	  (".TH \""^Odoc_messages.clas^"\" "^
	   cl.cl_name^" "^
	   "\""^(Odoc_misc.string_of_date ~hour: false date)^"\" "^ 
	   "Odoc "^
	   "\""^(match !Odoc_args.title with Some t -> t | None -> "")^"\n");

	output_string chanout
	  (
	   ".SH "^Odoc_messages.clas^"\n"^
	   Odoc_messages.clas^"   "^cl.cl_name^"\n"^
	   ".SH "^Odoc_messages.documentation^"\n"^
	   ".sp\n"
	  );
	output_string chanout (self#man_of_class cl);

	(* parameters *)
	output_string chanout 
	  (self#man_of_parameter_list "" cl.cl_parameters);
	(* a large blank *)
	output_string chanout "\n.sp\n.sp\n";

(*
        (* class inheritance *)
	self#generate_class_inheritance_info chanout cl;
*)
	(* the various elements *)
	List.iter 
	  (fun element ->
	    match element with
	      Class_attribute a ->
		output_string chanout (self#man_of_attribute a)
	    | Class_method m ->
		output_string chanout (self#man_of_method m)
	    | Class_comment t ->
		output_string chanout (self#man_of_class_comment t)
	  )
	  (Class.class_elements cl);

	close_out chanout
      with
	Sys_error s ->
	  incr Odoc_info.errors ;
	  prerr_endline s

    (** Generate the man page for the given class type.*)
    method generate_for_class_type ct =
      Odoc_info.reset_type_names () ;
      let date = Unix.time () in
      let file = self#file_name ct.clt_name in
      try
	let chanout = self#open_out file in
	output_string chanout
	  (".TH \""^Odoc_messages.class_type^"\" "^
	   ct.clt_name^" "^
	   "\""^(Odoc_misc.string_of_date ~hour: false date)^"\" "^ 
	   "Odoc "^
	   "\""^(match !Odoc_args.title with Some t -> t | None -> "")^"\n");

	output_string chanout
	  (
	   ".SH "^Odoc_messages.class_type^"\n"^
	   Odoc_messages.class_type^"   "^ct.clt_name^"\n"^
	   ".SH "^Odoc_messages.documentation^"\n"^
	   ".sp\n"
	  );
	output_string chanout (self#man_of_class_type ct);

	(* a large blank *)
	output_string chanout "\n.sp\n.sp\n";
(*
        (* class inheritance *)
	self#generate_class_inheritance_info chanout cl;
*)
	(* the various elements *)
	List.iter 
	  (fun element ->
	    match element with
	      Class_attribute a ->
		output_string chanout (self#man_of_attribute a)
	    | Class_method m ->
		output_string chanout (self#man_of_method m)
	    | Class_comment t ->
		output_string chanout (self#man_of_class_comment t)
	  )
	  (Class.class_type_elements ct);

	close_out chanout
      with
	Sys_error s ->
	  incr Odoc_info.errors ;
	  prerr_endline s

    (** Generate the man file for the given module type. 
       @raise Failure if an error occurs.*)
    method generate_for_module_type mt =
      let date = Unix.time () in
      let file = self#file_name mt.mt_name in
      try
	let chanout = self#open_out file in
	output_string chanout
	  (".TH \""^Odoc_messages.module_type^"\" "^
	   mt.mt_name^" "^
	   "\""^(Odoc_misc.string_of_date ~hour: false date)^"\" "^ 
	   "Odoc "^
	   "\""^(match !Odoc_args.title with Some t -> t | None -> "")^"\n");

	output_string chanout
	  (
	   ".SH "^Odoc_messages.module_type^"\n"^
	   Odoc_messages.module_type^"   "^mt.mt_name^"\n"^
	   ".SH "^Odoc_messages.documentation^"\n"^
	   ".sp\n"^
	   Odoc_messages.module_type^"\n"^
	   ".BI \""^(Name.simple mt.mt_name)^"\"\n"^
	   " = "^
	   (match mt.mt_type with
	     None -> ""
	   | Some t -> self#man_of_module_type (Name.father mt.mt_name) t
	   )^
	   "\n.sp\n"^
	   (self#man_of_info mt.mt_info)^"\n"^
	   ".sp\n"
	  );

	(* parameters for functors *)
	output_string chanout 
	  (self#man_of_module_parameter_list "" (Module.module_type_parameters mt));
	(* a large blank *)
	output_string chanout "\n.sp\n.sp\n";

	(* module elements *)
	List.iter 
	  (fun ele ->
	    match ele with
	      Element_module m ->
		output_string chanout (self#man_of_module m)
	    | Element_module_type mt ->
		output_string chanout (self#man_of_modtype mt)
	    | Element_included_module im ->
		output_string chanout (self#man_of_included_module mt.mt_name im)
	    | Element_class c ->
		output_string chanout (self#man_of_class c)
	    | Element_class_type ct ->
		output_string chanout (self#man_of_class_type ct)
	    | Element_value v ->
		output_string chanout (self#man_of_value v)
	    | Element_exception e ->
		output_string chanout (self#man_of_exception e)
	    | Element_type t ->
		output_string chanout (self#man_of_type t)
	    | Element_module_comment text ->
		output_string chanout (self#man_of_module_comment text)
	  )
	  (Module.module_type_elements mt);

	close_out chanout

      with
	Sys_error s ->
	  incr Odoc_info.errors ;
	  prerr_endline s

    (** Generate the man file for the given module. 
       @raise Failure if an error occurs.*)
    method generate_for_module m =
      let date = Unix.time () in
      let file = self#file_name m.m_name in
      try
	let chanout = self#open_out file in
	output_string chanout
	  (".TH \""^Odoc_messages.modul^"\" "^
	   m.m_name^" "^
	   "\""^(Odoc_misc.string_of_date ~hour: false date)^"\" "^ 
	   "Odoc "^
	   "\""^(match !Odoc_args.title with Some t -> t | None -> "")^"\n");

	output_string chanout
	  (
	   ".SH "^Odoc_messages.modul^"\n"^
	   Odoc_messages.modul^"   "^m.m_name^"\n"^
	   ".SH "^Odoc_messages.documentation^"\n"^
	   ".sp\n"^
	   Odoc_messages.modul^"\n"^
	   ".BI \""^(Name.simple m.m_name)^"\"\n"^
	   " : "^(self#man_of_module_type (Name.father m.m_name) m.m_type)^
	   "\n.sp\n"^
	   (self#man_of_info m.m_info)^"\n"^
	   ".sp\n"
	  );

	(* parameters for functors *)
	output_string chanout 
	  (self#man_of_module_parameter_list "" (Module.module_parameters m));
	(* a large blank *)
	output_string chanout "\n.sp\n.sp\n";

	(* module elements *)
	List.iter 
	  (fun ele ->
	    match ele with
	      Element_module m ->
		output_string chanout (self#man_of_module m)
	    | Element_module_type mt ->
		output_string chanout (self#man_of_modtype mt)
	    | Element_included_module im ->
		output_string chanout (self#man_of_included_module m.m_name im)
	    | Element_class c ->
		output_string chanout (self#man_of_class c)
	    | Element_class_type ct ->
		output_string chanout (self#man_of_class_type ct)
	    | Element_value v ->
		output_string chanout (self#man_of_value v)
	    | Element_exception e ->
		output_string chanout (self#man_of_exception e)
	    | Element_type t ->
		output_string chanout (self#man_of_type t)
	    | Element_module_comment text ->
		output_string chanout (self#man_of_module_comment text)
	  )
	  (Module.module_elements m);

	close_out chanout

      with
	Sys_error s ->
	  raise (Failure s)

    (** Create the groups of elements to generate pages for. *)
    method create_groups module_list =
      let name res_ele = 
	match res_ele with
	  Res_module m -> m.m_name
	| Res_module_type mt -> mt.mt_name
	| Res_class c -> c.cl_name
	| Res_class_type ct -> ct.clt_name
	| Res_value v -> Name.simple v.val_name
	| Res_type t -> Name.simple t.ty_name
	| Res_exception e -> Name.simple e.ex_name
	| Res_attribute a -> Name.simple a.att_value.val_name
	| Res_method m -> Name.simple m.met_value.val_name
	| Res_section s -> assert false
      in
      let all_items_pre = Odoc_info.Search.search_by_name module_list (Str.regexp ".*")  in
      let all_items = List.filter 
	  (fun r -> match r with Res_section _ -> false | _ -> true)
	  all_items_pre
      in
      let sorted_items = List.sort (fun e1 -> fun e2 -> compare (name e1) (name e2)) all_items in
      let rec f acc1 acc2 l = 
	match l with
	  [] -> acc2 :: acc1
	| h :: q ->
	    match acc2 with
	      [] -> f acc1 [h] q
	    | h2 :: q2 -> 
		if (name h) = (name h2) then
		  if List.mem h acc2 then
		    f acc1 acc2 q
		  else
		    f acc1 (acc2 @ [h]) q 
		else
		  f (acc2 :: acc1) [h] q
      in
      f [] [] sorted_items

    (** Generate a man page for a group of elements with the same name. 
       A group must not be empty.*)
    method generate_for_group l =
     let name = 
       Name.simple 
	 (
	  match List.hd l with
	    Res_module m -> m.m_name
	  | Res_module_type mt -> mt.mt_name
	  | Res_class c -> c.cl_name
	  | Res_class_type ct -> ct.clt_name
	  | Res_value v -> v.val_name
	  | Res_type t -> t.ty_name
	  | Res_exception e -> e.ex_name
	  | Res_attribute a -> a.att_value.val_name
	  | Res_method m -> m.met_value.val_name
	  | Res_section s -> s
	 )
     in
     let date = Unix.time () in
      let file = self#file_name name in
      try
	let chanout = self#open_out file in
	output_string chanout
	  (".TH \""^name^"\" "^
	   "man "^
	   "\""^(Odoc_misc.string_of_date ~hour: false date)^"\" "^ 
	   "Odoc "^
	   "\""^(match !Odoc_args.title with Some t -> t | None -> "")^"\n");

	let f ele =
	  match ele with
	    Res_value v ->
	      output_string chanout
		("\n.SH "^Odoc_messages.modul^" "^(Name.father v.val_name)^"\n"^
		 (self#man_of_value v))
	  | Res_type t ->
	      output_string chanout
		("\n.SH "^Odoc_messages.modul^" "^(Name.father t.ty_name)^"\n"^
		 (self#man_of_type t))
	  | Res_exception e ->
	      output_string chanout
		("\n.SH "^Odoc_messages.modul^" "^(Name.father e.ex_name)^"\n"^
		 (self#man_of_exception e))
	  | Res_attribute a ->
	      output_string chanout
		("\n.SH "^Odoc_messages.clas^" "^(Name.father a.att_value.val_name)^"\n"^
		 (self#man_of_attribute a))
	  | Res_method m ->
	      output_string chanout
		("\n.SH "^Odoc_messages.clas^" "^(Name.father m.met_value.val_name)^"\n"^
		 (self#man_of_method m))
	  | Res_class c ->
	      output_string chanout
		("\n.SH "^Odoc_messages.modul^" "^(Name.father c.cl_name)^"\n"^
		 (self#man_of_class c))
	  | Res_class_type ct ->
	      output_string chanout
		("\n.SH "^Odoc_messages.modul^" "^(Name.father ct.clt_name)^"\n"^
		 (self#man_of_class_type ct))
	  | _ ->
	      (* normalement on ne peut pas avoir de module ici. *)
	      ()
	in
	List.iter f l;
	close_out chanout
      with
	Sys_error s ->
	  incr Odoc_info.errors ;
	  prerr_endline s

    (** Generate all the man pages from a module list. *)
    method generate module_list =
      let sorted_module_list = Sort.list (fun m1 -> fun m2 -> m1.m_name < m2.m_name) module_list in
      let groups = self#create_groups sorted_module_list in
      let f group = 
	match group with
	  [] ->
	    ()
	| [Res_module m] -> self#generate_for_module m
	| [Res_module_type mt] -> self#generate_for_module_type mt
	| [Res_class cl] -> self#generate_for_class cl
	| [Res_class_type ct] -> self#generate_for_class_type ct
	| l ->
	    self#generate_for_group l
      in
      List.iter f groups 
  end