Merge pull request #260 from pcc/topmost
[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 Monodoc.Providers;
15 using Mono.Options;
16
17 namespace Mono.Documentation {
18         
19 public class MDocAssembler : MDocCommand {
20         static readonly string[] ValidFormats = {
21                 "ecma", 
22                 "ecmaspec", 
23                 "error", 
24                 "hb", 
25                 "man", 
26                 "simple", 
27                 "xhtml"
28         };
29
30         public static Option[] CreateFormatOptions (MDocCommand self, Dictionary<string, List<string>> formats)
31         {
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                                                 self.Error ("Invalid documentation format: {0}.", v);
42                                         cur_format = v;
43                                 } },
44                         { "<>", v => AddFormat (self, formats, cur_format, v) },
45                 };
46                 return new Option[]{options[0], options[1]};
47         }
48
49         public override void Run (IEnumerable<string> args)
50         {
51                 var formats = new Dictionary<string, List<string>> ();
52                 string prefix = "tree";
53                 var formatOptions = CreateFormatOptions (this, formats);
54                 var options = new OptionSet () {
55                         formatOptions [0],
56                         { "o|out=",
57                                 "Provides the output file prefix; the files {PREFIX}.zip and " + 
58                                         "{PREFIX}.tree will be created.\n" +
59                                         "If not specified, `tree' is the default PREFIX.",
60                                 v => prefix = v },
61                         formatOptions [1],
62                 };
63                 List<string> extra = Parse (options, args, "assemble", 
64                                 "[OPTIONS]+ DIRECTORIES",
65                                 "Assemble documentation within DIRECTORIES for use within the monodoc browser.");
66                 if (extra == null)
67                         return;
68
69                 List<Provider> list = new List<Provider> ();
70                 EcmaProvider ecma = null;
71                 bool sort = false;
72                 
73                 foreach (string format in formats.Keys) {
74                         switch (format) {
75                         case "ecma":
76                                 if (ecma == null) {
77                                         ecma = new EcmaProvider ();
78                                         list.Add (ecma);
79                                         sort = true;
80                                 }
81                                 foreach (string dir in formats [format])
82                                         ecma.AddDirectory (dir);
83                                 break;
84
85                         case "xhtml":
86                         case "hb":
87                                 list.AddRange (formats [format].Select (d => (Provider) new XhtmlProvider (d)));
88                                 break;
89
90                         case "man":
91                                 list.Add (new ManProvider (formats [format].ToArray ()));
92                                 break;
93
94                         case "error":
95                                 list.AddRange (formats [format].Select (d => (Provider) new ErrorProvider (d)));
96                                 break;
97
98                         case "ecmaspec":
99                                 list.AddRange (formats [format].Select (d => (Provider) new EcmaSpecProvider (d)));
100                                 break;
101
102                         case "addins":
103                                 list.AddRange (formats [format].Select (d => (Provider) new AddinsProvider (d)));
104                                 break;
105                         }
106                 }
107
108                 HelpSource hs = new HelpSource (prefix, true);
109                 hs.TraceLevel = TraceLevel;
110
111                 foreach (Provider p in list) {
112                         p.PopulateTree (hs.Tree);
113                 }
114
115                 if (sort && hs.Tree != null)
116                         hs.Tree.RootNode.Sort ();
117                               
118                 //
119                 // Flushes the EcmaProvider
120                 //
121                 foreach (Provider p in list)
122                         p.CloseTree (hs, hs.Tree);
123
124                 hs.Save ();
125         }
126
127         private static void AddFormat (MDocCommand self, Dictionary<string, List<string>> d, string format, string file)
128         {
129                 if (format == null)
130                         self.Error ("No format specified.");
131                 List<string> l;
132                 if (!d.TryGetValue (format, out l)) {
133                         l = new List<string> ();
134                         d.Add (format, l);
135                 }
136                 l.Add (file);
137         }
138 }
139
140 }