forked from vaishaksuresh/webdevchecklist.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecklist.cs
More file actions
139 lines (107 loc) · 3.87 KB
/
Checklist.cs
File metadata and controls
139 lines (107 loc) · 3.87 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Xml;
public static class Checklist
{
// Public
public static Dictionary<string, Dictionary<string, XmlDocument>> Docs = new Dictionary<string, Dictionary<string, XmlDocument>>();
public const string Title = "Web Developer Checklist";
// Private
private const string defaultSite = "webdevchecklist.com";
private static string SitesFolder = HostingEnvironment.MapPath("~/sites/");
static Checklist()
{
BuildCache();
}
private static void BuildCache()
{
Docs.Clear();
foreach (string folder in Directory.GetDirectories(SitesFolder))
{
var dic = new Dictionary<string, XmlDocument>();
foreach (string file in Directory.GetFiles(folder, "*.xml", SearchOption.AllDirectories))
{
XmlDocument doc = new XmlDocument();
doc.Load(file);
string key = file
.Replace(folder, string.Empty)
.Replace("Sections", string.Empty)
.Replace("\\", "/")
.Replace(".xml", string.Empty)
.TrimStart('/');
if (key.Contains("/") && key.EndsWith("index"))
{
key = key.Replace("/index", string.Empty);
}
dic.Add(key, doc);
}
Docs.Add(Path.GetFileName(folder), dic);
}
}
public static XmlDocument GetXmlDocument(HttpRequestBase request)
{
string site = GetSiteName(request);
var section = Docs[site];
var pageName = GetPageName(request);
var result = section.Keys.SingleOrDefault(k => k.Equals(pageName, StringComparison.OrdinalIgnoreCase));
if (result != null)
{
return section[result];
}
return Docs[site]["index"];
}
public static string GetSiteName(HttpRequestBase request)
{
return request.Url.Host.StartsWith("localhost") ? defaultSite : request.Url.Host;
}
public static string GetSiteSectionFolder(HttpRequestBase request)
{
string siteName = GetSiteName(request);
return Path.Combine(SitesFolder, siteName, "Sections");
}
public static string GetPageTitle(HttpRequestBase request)
{
XmlDocument doc = GetXmlDocument(request);
XmlAttribute attr = doc.SelectSingleNode("checklist").Attributes["name"];
if (attr != null)
{
return attr.InnerText;
}
return Title;
}
public static string GetPageName(HttpRequestBase request)
{
string site = GetSiteName(request);
if (Docs.ContainsKey(site))
{
var pair = Docs[site];
var path = request.RawUrl.Trim('/');
while (true)
{
string clean = string.IsNullOrEmpty(path) ? "index" : path;
var result = pair.Keys.SingleOrDefault(k => k.Equals(clean, StringComparison.OrdinalIgnoreCase));
if (result != null)
return result;
int index = path.LastIndexOf('/');
if (index == -1)
break;
path = path.Substring(0, index);
}
}
return Title;
}
public static string GetBaseName(HttpRequestBase request)
{
string clean = request.RawUrl.Trim('/');
if (Docs.Any(d => d.Key.Equals(clean, StringComparison.OrdinalIgnoreCase)))
return clean;
int index = clean.IndexOf('/');
if (index > -1)
clean = clean.Substring(0, index);
return clean;
}
}