2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / tools / mdoc / Mono.Documentation / assembler.cs
1 //
2 // The assembler: Help compiler.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@gnome.org)
6 //
7 // (C) 2003 Ximian, Inc.
8 //
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Xml;
13 using Monodoc;
14 using Mono.Options;
15
16 namespace Mono.Documentation {
17         
18 public class MDocAssembler : MDocCommand {
19         public override void Run (IEnumerable<string> args)
20         {
21                 string[] validFormats = {
22                         "ecma", 
23                         "ecmaspec", 
24                         "error", 
25                         "hb", 
26                         "man", 
27                         "simple", 
28                         "xhtml"
29                 };
30                 var formats = new Dictionary<string, List<string>> ();
31                 string prefix = "tree";
32                 string cur_format = "ecma";
33                 var options = new OptionSet () {
34                         { "f|format=",
35                                 "The documentation {FORMAT} used in DIRECTORIES.  " + 
36                                         "Valid formats include:\n  " +
37                                         string.Join ("\n  ", validFormats) + "\n" +
38                                         "If not specified, the default format is `ecma'.",
39                                 v => {
40                                         if (Array.IndexOf (validFormats, v) < 0)
41                                                 Error ("Invalid documentation format: {0}.", v);
42                                         cur_format = v;
43                                 } },
44                         { "o|out=",
45                                 "Provides the output file prefix; the files {PREFIX}.zip and " + 
46                                         "{PREFIX}.tree will be created.\n" +
47                                         "If not specified, `tree' is the default PREFIX.",
48                                 v => prefix = v },
49                         { "<>", v => AddFormat (formats, cur_format, v) },
50                 };
51                 List<string> extra = Parse (options, args, "assemble", 
52                                 "[OPTIONS]+ DIRECTORIES",
53                                 "Assemble documentation within DIRECTORIES for use within the monodoc browser.");
54                 if (extra == null)
55                         return;
56
57                 List<Provider> list = new List<Provider> ();
58                 EcmaProvider ecma = null;
59                 bool sort = false;
60                 
61                 foreach (string format in formats.Keys) {
62                         switch (format) {
63                         case "ecma":
64                                 if (ecma == null) {
65                                         ecma = new EcmaProvider ();
66                                         list.Add (ecma);
67                                         sort = true;
68                                 }
69                                 foreach (string dir in formats [format])
70                                         ecma.AddDirectory (dir);
71                                 break;
72
73                         case "xhtml":
74                         case "hb":
75                                 list.AddRange (formats [format].Select (d => (Provider) new XhtmlProvider (d)));
76                                 break;
77
78                         case "man":
79                                 list.Add (new ManProvider (formats [format].ToArray ()));
80                                 break;
81
82                         case "simple":
83                                 list.AddRange (formats [format].Select (d => (Provider) new SimpleProvider (d)));
84                                 break;
85
86                         case "error":
87                                 list.AddRange (formats [format].Select (d => (Provider) new ErrorProvider (d)));
88                                 break;
89
90                         case "ecmaspec":
91                                 list.AddRange (formats [format].Select (d => (Provider) new EcmaSpecProvider (d)));
92                                 break;
93
94                         case "addins":
95                                 list.AddRange (formats [format].Select (d => (Provider) new AddinsProvider (d)));
96                                 break;
97                         }
98                 }
99
100                 HelpSource hs = new HelpSource (prefix, true);
101                 hs.TraceLevel = TraceLevel;
102
103                 foreach (Provider p in list) {
104                         p.PopulateTree (hs.Tree);
105                 }
106
107                 if (sort)
108                         hs.Tree.Sort ();
109                               
110                 //
111                 // Flushes the EcmaProvider
112                 //
113                 foreach (Provider p in list)
114                         p.CloseTree (hs, hs.Tree);
115
116                 hs.Save ();
117         }
118
119         private void AddFormat (Dictionary<string, List<string>> d, string format, string file)
120         {
121                 if (format == null)
122                         Error ("No format specified.");
123                 List<string> l;
124                 if (!d.TryGetValue (format, out l)) {
125                         l = new List<string> ();
126                         d.Add (format, l);
127                 }
128                 l.Add (file);
129         }
130 }
131
132 }