2 // The simple provider is an example provider
5 // Miguel de Icaza (miguel@ximian.com)
8 // mono assembler.exe --simple DIRECTORY --out name
10 // Then create a .source file in your sources directory, and copy
11 // name.tree and name.zip to the sources directory.
13 // To view the tree generated, use:
14 // mono dump.exe name.tree
22 // The simple provider generates the information source
24 public class SimpleProvider : Provider {
27 public SimpleProvider (string base_directory)
29 basedir = base_directory;
30 if (!Directory.Exists (basedir))
31 throw new FileNotFoundException (String.Format ("The directory `{0}' does not exist", basedir));
34 public override void PopulateTree (Tree tree)
36 Node top = tree.LookupNode ("Directory at: " + basedir, "simple:");
38 foreach (string dir in Directory.GetDirectories (basedir)){
39 string url = Path.GetFileName (dir);
40 Node n = top.LookupNode ("Dir: " + url, url);
45 #pragma warning disable 219
46 void PopulateDir (Node me, string dir)
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);
55 foreach (string file in Directory.GetFiles (dir)){
56 Console.WriteLine (" File: " + file);
57 string file_code = me.tree.HelpSource.PackFile (file);
60 // The url element encoded for the file is:
61 // originalfilename#CODE
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
66 Node n = me.LookupNode (Path.GetFileName (file), file + "#" + file_code);
71 public override void CloseTree (HelpSource hs, Tree tree)
77 // The HelpSource is used during the rendering phase.
80 public class SimpleHelpSource : HelpSource {
83 public SimpleHelpSource (string base_file, bool create) : base (base_file, create)
85 enc = new UTF8Encoding (false, false);
88 public override string GetText (string url, out Node match_node)
92 string c = GetCachedText (url);
96 if (url.StartsWith ("simple:") || url.StartsWith ("simple-directory:"))
97 return GetTextFromUrl (url);
102 string GetTextFromUrl (string url)
104 // Remove "simple:" prefix
105 url = url.Substring (7);
107 if (url.StartsWith ("simple-directory:"))
108 return String.Format ("<html>This is a directory entry point: {0} </html>",
111 // Otherwise the last element of the url is the file code we got.
112 int pound = url.LastIndexOf ("#");
117 code = url.Substring (pound+1);
120 Stream s = GetHelpStream (code);
122 return String.Format ("<html>No stream for this node: {0} </html>", url);
125 // Now, get the file type
127 int slash = url.LastIndexOf ("/");
128 string fname = url.Substring (slash + 1, pound - slash - 1).ToLower ();
130 if (fname.EndsWith (".html") || fname.EndsWith (".htm")){
131 TextReader r = new StreamReader (s, enc);
132 return r.ReadToEnd ();
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>";
140 // Convert text to HTML
141 StringBuilder result = new StringBuilder ("<html>");
142 TextReader reader = new StreamReader (s, enc);
145 while ((line = reader.ReadLine ()) != null){
146 result.Append (line);
147 result.Append ("<br>");
149 result.Append ("<html>");
150 return result.ToString ();