Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Global.asax « webdoc - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af59de1ca8a7f11b7c7a1fad5cfe3b78c3079112 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<%@ Application ClassName="Mono.Website.Global" %>
<%@ Import Namespace="Monodoc" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Linq" %>
<%@ Assembly name="monodoc" %>

<script runat="server" language="c#" >

public static RootTree help_tree;
[ThreadStatic]
static SearchableIndex search_index;
public static string ua = null;
// These are dictionary of path for external couple (html, css, js) files that should get included
static Dictionary<ExternalResourceType, string> externalHeader = null;
static Dictionary<ExternalResourceType, string> externalFooter = null;

void Application_Start ()
{
	HelpSource.use_css = true;
	HelpSource.FullHtml = false;
	HelpSource.UseWebdocCache = true;
	var rootDir = WebConfigurationManager.AppSettings["MonodocRootDir"];
	if (!string.IsNullOrEmpty (rootDir))
		help_tree = RootTree.LoadTree (rootDir);
	else
		help_tree = RootTree.LoadTree ();
	ua = WebConfigurationManager.AppSettings["GoogleAnalytics"];
	externalHeader = ParseExternalDefinition (WebConfigurationManager.AppSettings["ExternalHeader"]);
	externalFooter = ParseExternalDefinition (WebConfigurationManager.AppSettings["ExternalFooter"]);
	SettingsHandler.Settings.EnableEditing = false;
}

public static readonly string kipunji_root_url = "http://docs.go-mono.com/";
private static readonly string prefixes = "TNCFEMP";

public static bool should_redirect_to_kipunji (string link)
{
	return prefixes.IndexOf (link [0]) > -1;
}

public static void redirect_to_kipunji (HttpContext context, string link)
{
	StringBuilder res = new StringBuilder ();
        res.Append (kipunji_root_url);

	if (link.StartsWith ("T:")) {

	      int end = link.Length;
	      string post = String.Empty;
	      if (link.Length > 3 && link [link.Length - 2] == '/') {
                  end = link.Length - 2;
	          switch (link [link.Length - 1]) {
	      	     case '*':
		     	  post = "/Members";
			  break;
		     case 'M':
		           post = "/Members#methods";
			   break;
		     case 'P':
		     	   post = "/Members#properties";
			   break;
		     case 'C':
		     	   post = "/Members#ctors";
			   break;
	             case 'F':
		     	   post = "/Members#fields";
			   break;
		     case 'E':
		     	   post = "/Members#events";
			   break;
	          }
              }

	      res.Append (link.Substring (2, end - 2));
	      res.Append (post);
	          
	} else if (link.StartsWith ("C:")) {
	      // HACK: Instead of linking to the proper ctor just send them to all the ctors
	      res.AppendFormat ("{0}/Members#ctors", link.Substring (2, link.Length - 2));
	} else if (link.StartsWith ("N:") || link.StartsWith ("M:") || link.StartsWith ("P:") || link.StartsWith ("C:") || link.StartsWith ("E:"))
	      res.Append (link.Substring (2, link.Length - 2));

	context.Response.RedirectPermanent (res.ToString ());
}

public static string CreateTreeBootFragment ()
{
	var fragment = new System.Text.StringBuilder ();

	for (int i = 0; i < help_tree.Nodes.Count; i++){
		Node n = (Node)help_tree.Nodes [i];

		string url = n.PublicUrl;

		if (n.Caption == "Base Class Library" || n.Caption == "Mono Libraries")
			url = kipunji_root_url + (n.Caption == "Base Class Library" ? "?display_all=true" : String.Empty);

		fragment.Append ("tree.CreateItem (root, '" + n.Caption + "', '" + url + "', ");
	
		if (n.Nodes.Count != 0)
			fragment.Append ("'" + i + "'");
		else
			fragment.Append ("null");
	
		if (i == help_tree.Nodes.Count-1)
			fragment.Append (", true");
		else
			fragment.Append (", false");

		fragment.Append (@");
			");
	}

	return fragment.ToString ();
}

public static SearchableIndex GetSearchIndex ()
{
	if (search_index != null)
		return search_index;
	return (search_index = help_tree.GetSearchIndex ());
}

public enum ExternalResourceType {
	Unknown,
	Html,
	Css,
	Javascript
}

public static string IncludeExternalHeader (ExternalResourceType type)
{
	return IncludeExternalFile (type, externalHeader);
}

public static string IncludeExternalFooter (ExternalResourceType type)
{
	return IncludeExternalFile (type, externalFooter);
}

static string IncludeExternalFile (ExternalResourceType type, Dictionary<ExternalResourceType, string> paths)
{
	string path;
	if (paths == null || !paths.TryGetValue (type, out path) || !File.Exists (path))
		return string.Empty;
	if (type == ExternalResourceType.Javascript) {
		return string.Format ("{1}script type='text/javascript' src='{0}'{2}{1}/script{2}", path, '<', '>');
	} else if (type == ExternalResourceType.Css) {
		return string.Format ("{1}link type='text/css' rel='stylesheet' href='{0}' /{2}", path, '<', '>');
	} else {
		return File.ReadAllText (path);
	}
}

static Dictionary<ExternalResourceType, string> ParseExternalDefinition (string definitionPath)
{
	if (string.IsNullOrEmpty (definitionPath) || !File.Exists (definitionPath))
		return null;
	// A definition file is a simple file with a line for each resource type in a key value fashion
	var lines = File.ReadAllLines (definitionPath);
	var result = lines.Where (l => !string.IsNullOrEmpty (l) && l[0] != '#') // Take non-empty, non-comment lines
		.Select (l => l.Split ('='))
		.Where (a => a != null && a.Length == 2)
		.Select (a => { ExternalResourceType t; return Tuple.Create (Enum.TryParse (a[0].Trim (), true, out t) ? t : ExternalResourceType.Unknown, a[1].Trim ()); })
		.Where (t => t.Item1 != ExternalResourceType.Unknown)
		.ToDictionary (t => t.Item1, t => t.Item2);

	return result;
}

</script>