File: simple-provider.cs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (153 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download | duplicates (12)
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
//
// The simple provider is an example provider
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//
// Use like this:
//   mono assembler.exe --simple DIRECTORY --out name
//
// Then create a .source file in your sources directory, and copy
// name.tree and name.zip to the sources directory.
//
// To view the tree generated, use:
//   mono dump.exe name.tree
//
namespace Monodoc {
using System;
using System.IO;
using System.Text;

//
// The simple provider generates the information source
//
public class SimpleProvider : Provider {
	string basedir;
	
	public SimpleProvider (string base_directory)
	{
		basedir = base_directory;
		if (!Directory.Exists (basedir))
			throw new FileNotFoundException (String.Format ("The directory `{0}' does not exist", basedir));
	}

	public override void PopulateTree (Tree tree)
	{
		Node top = tree.LookupNode ("Directory at: " + basedir, "simple:");
		
		foreach (string dir in Directory.GetDirectories (basedir)){
			string url = Path.GetFileName (dir);
			Node n = top.LookupNode ("Dir: " + url, url);
			PopulateDir (n, dir);
		}
	}

#pragma warning disable 219
	void PopulateDir (Node me, string dir)
	{
		Console.WriteLine ("Adding: " + dir);
		foreach (string child_dir in Directory.GetDirectories (dir)){
			string url = Path.GetFileName (child_dir);
			Node n = me.LookupNode ("Dir: " + url, "simple-directory:" + url);
			PopulateDir (me, child_dir);
		}

		foreach (string file in Directory.GetFiles (dir)){
			Console.WriteLine ("   File: " + file);
			string file_code = me.tree.HelpSource.PackFile (file);

			//
			// The url element encoded for the file is:
			//  originalfilename#CODE
			//
			// The code is assigned to us after the file has been packaged
			// We use the original-filename later to render html or text files
			//
			Node n = me.LookupNode (Path.GetFileName (file), file + "#" + file_code);
			
		}
	}

	public override void CloseTree (HelpSource hs, Tree tree)
	{
	}
}

//
// The HelpSource is used during the rendering phase.
//

public class SimpleHelpSource : HelpSource {
	Encoding enc;
	
	public SimpleHelpSource (string base_file, bool create) : base (base_file, create)
	{
		enc = new UTF8Encoding (false, false);
	}

	public override string GetText (string url, out Node match_node)
	{
		match_node = null;

		string c = GetCachedText (url);
		if (c != null)
			return c;

		if (url.StartsWith ("simple:") || url.StartsWith ("simple-directory:"))
			return GetTextFromUrl (url);

		return null;
	}

	string GetTextFromUrl (string url)
	{
		// Remove "simple:" prefix
		url = url.Substring (7);

		if (url.StartsWith ("simple-directory:"))
			return String.Format ("<html>This is a directory entry point: {0} </html>",
					      url.Substring (17));

		// Otherwise the last element of the url is the file code we got.
		int pound = url.LastIndexOf ("#");
		string code;
		if (pound == -1)
			code = url;
		else
			code = url.Substring (pound+1);


		Stream s = GetHelpStream (code);
		if (s == null)
			return String.Format ("<html>No stream for this node: {0} </html>", url);

		//
		// Now, get the file type
		//
		int slash = url.LastIndexOf ("/");
		string fname = url.Substring (slash + 1, pound - slash - 1).ToLower ();

		if (fname.EndsWith (".html") || fname.EndsWith (".htm")){
			TextReader r = new StreamReader (s, enc);
			return r.ReadToEnd ();
		}

		if (fname.EndsWith (".png") || fname.EndsWith (".jpg") ||
		    fname.EndsWith (".jpeg") || fname.EndsWith (".gif")){
			return "<html>Image file, have not implemented rendering this yet</html>";
		}

		// Convert text to HTML
		StringBuilder result = new StringBuilder ("<html>");
		TextReader reader = new StreamReader (s, enc);
		string line;
		
		while ((line = reader.ReadLine ()) != null){
			result.Append (line);
			result.Append ("<br>");
		}
		result.Append ("<html>");
		return result.ToString ();
	}
}
}