e567dc19660f563de902837f27a4459a585a18d2
[mono.git] / mcs / class / monodoc / Monodoc / providers / simple-provider.cs
1 //
2 // The simple provider is an example provider
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // Use like this:
8 //   mono assembler.exe --simple DIRECTORY --out name
9 //
10 // Then create a .source file in your sources directory, and copy
11 // name.tree and name.zip to the sources directory.
12 //
13 // To view the tree generated, use:
14 //   mono dump.exe name.tree
15 //
16 namespace Monodoc {
17 using System;
18 using System.IO;
19 using System.Text;
20
21 //
22 // The simple provider generates the information source
23 //
24 public class SimpleProvider : Provider {
25         string basedir;
26         
27         public SimpleProvider (string base_directory)
28         {
29                 basedir = base_directory;
30                 if (!Directory.Exists (basedir))
31                         throw new FileNotFoundException (String.Format ("The directory `{0}' does not exist", basedir));
32         }
33
34         public override void PopulateTree (Tree tree)
35         {
36                 Node top = tree.LookupNode ("Directory at: " + basedir, "simple:");
37                 
38                 foreach (string dir in Directory.GetDirectories (basedir)){
39                         string url = Path.GetFileName (dir);
40                         Node n = top.LookupNode ("Dir: " + url, url);
41                         PopulateDir (n, dir);
42                 }
43         }
44
45 #pragma warning disable 219
46         void PopulateDir (Node me, string dir)
47         {
48                 Console.WriteLine ("Adding: " + dir);
49                 foreach (string child_dir in Directory.GetDirectories (dir)){
50                         string url = Path.GetFileName (child_dir);
51                         Node n = me.LookupNode ("Dir: " + url, "simple-directory:" + url);
52                         PopulateDir (me, child_dir);
53                 }
54
55                 foreach (string file in Directory.GetFiles (dir)){
56                         Console.WriteLine ("   File: " + file);
57                         string file_code = me.tree.HelpSource.PackFile (file);
58
59                         //
60                         // The url element encoded for the file is:
61                         //  originalfilename#CODE
62                         //
63                         // The code is assigned to us after the file has been packaged
64                         // We use the original-filename later to render html or text files
65                         //
66                         Node n = me.LookupNode (Path.GetFileName (file), file + "#" + file_code);
67                         
68                 }
69         }
70
71         public override void CloseTree (HelpSource hs, Tree tree)
72         {
73         }
74 }
75
76 //
77 // The HelpSource is used during the rendering phase.
78 //
79
80 public class SimpleHelpSource : HelpSource {
81         Encoding enc;
82         
83         public SimpleHelpSource (string base_file, bool create) : base (base_file, create)
84         {
85                 enc = new UTF8Encoding (false, false);
86         }
87
88         public override string GetText (string url, out Node match_node)
89         {
90                 match_node = null;
91
92                 string c = GetCachedText (url);
93                 if (c != null)
94                         return c;
95
96                 if (url.StartsWith ("simple:") || url.StartsWith ("simple-directory:"))
97                         return GetTextFromUrl (url);
98
99                 return null;
100         }
101
102         string GetTextFromUrl (string url)
103         {
104                 // Remove "simple:" prefix
105                 url = url.Substring (7);
106
107                 if (url.StartsWith ("simple-directory:"))
108                         return String.Format ("<html>This is a directory entry point: {0} </html>",
109                                               url.Substring (17));
110
111                 // Otherwise the last element of the url is the file code we got.
112                 int pound = url.LastIndexOf ("#");
113                 string code;
114                 if (pound == -1)
115                         code = url;
116                 else
117                         code = url.Substring (pound+1);
118
119
120                 Stream s = GetHelpStream (code);
121                 if (s == null)
122                         return String.Format ("<html>No stream for this node: {0} </html>", url);
123
124                 //
125                 // Now, get the file type
126                 //
127                 int slash = url.LastIndexOf ("/");
128                 string fname = url.Substring (slash + 1, pound - slash - 1).ToLower ();
129
130                 if (fname.EndsWith (".html") || fname.EndsWith (".htm")){
131                         TextReader r = new StreamReader (s, enc);
132                         return r.ReadToEnd ();
133                 }
134
135                 if (fname.EndsWith (".png") || fname.EndsWith (".jpg") ||
136                     fname.EndsWith (".jpeg") || fname.EndsWith (".gif")){
137                         return "<html>Image file, have not implemented rendering this yet</html>";
138                 }
139
140                 // Convert text to HTML
141                 StringBuilder result = new StringBuilder ("<html>");
142                 TextReader reader = new StreamReader (s, enc);
143                 string line;
144                 
145                 while ((line = reader.ReadLine ()) != null){
146                         result.Append (line);
147                         result.Append ("<br>");
148                 }
149                 result.Append ("<html>");
150                 return result.ToString ();
151         }
152 }
153 }