1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . IO ;
4
- using System . Linq ;
5
- using System . Web ;
6
- using System . Web . Hosting ;
7
- using System . Xml ;
8
-
9
- public static class Checklist
10
- {
11
- // Public
12
- public static Dictionary < string , Dictionary < string , XmlDocument > > Docs = new Dictionary < string , Dictionary < string , XmlDocument > > ( ) ;
13
- public const string Title = "Web Developer Checklist" ;
14
-
15
- // Private
16
- private const string defaultSite = "webdevchecklist.com" ;
17
- private static string SitesFolder = HostingEnvironment . MapPath ( "~/sites/" ) ;
18
-
19
- static Checklist ( )
20
- {
21
- BuildCache ( ) ;
22
- }
23
-
24
- private static void BuildCache ( )
25
- {
26
- Docs . Clear ( ) ;
27
-
28
- foreach ( string folder in Directory . GetDirectories ( SitesFolder ) )
29
- {
30
- var dic = new Dictionary < string , XmlDocument > ( ) ;
31
-
32
- foreach ( string file in Directory . GetFiles ( folder , "*.xml" , SearchOption . AllDirectories ) )
33
- {
34
- XmlDocument doc = new XmlDocument ( ) ;
35
- doc . Load ( file ) ;
36
-
37
- string key = file
38
- . Replace ( folder , string . Empty )
39
- . Replace ( "Sections" , string . Empty )
40
- . Replace ( "\\ " , "/" )
41
- . Replace ( ".xml" , string . Empty )
42
- . TrimStart ( '/' ) ;
43
-
44
- if ( key . Contains ( "/" ) && key . EndsWith ( "index" ) )
45
- {
46
- key = key . Replace ( "/index" , string . Empty ) ;
47
- }
48
-
49
- dic . Add ( key , doc ) ;
50
- }
51
-
52
- Docs . Add ( Path . GetFileName ( folder ) , dic ) ;
53
- }
54
- }
55
-
56
- public static XmlDocument GetXmlDocument ( HttpRequestBase request )
57
- {
58
- string site = GetSiteName ( request ) ;
59
-
60
- var section = Docs [ site ] ;
61
- var pageName = GetPageName ( request ) ;
62
- var result = section . Keys . SingleOrDefault ( k => k . Equals ( pageName , StringComparison . OrdinalIgnoreCase ) ) ;
63
-
64
- if ( result != null )
65
- {
66
- return section [ result ] ;
67
- }
68
-
69
- return Docs [ site ] [ "index" ] ;
70
- }
71
-
72
- public static string GetSiteName ( HttpRequestBase request )
73
- {
74
- return request . Url . Host . StartsWith ( "localhost" ) ? defaultSite : request . Url . Host ;
75
- }
76
-
77
- public static string GetSiteSectionFolder ( HttpRequestBase request )
78
- {
79
- string siteName = GetSiteName ( request ) ;
80
-
81
- return Path . Combine ( SitesFolder , siteName , "Sections" ) ;
82
- }
83
-
84
- public static string GetPageTitle ( HttpRequestBase request )
85
- {
86
- XmlDocument doc = GetXmlDocument ( request ) ;
87
- XmlAttribute attr = doc . SelectSingleNode ( "checklist" ) . Attributes [ "name" ] ;
88
-
89
- if ( attr != null )
90
- {
91
- return attr . InnerText ;
92
- }
93
-
94
- return Title ;
95
- }
96
-
97
- public static string GetPageName ( HttpRequestBase request )
98
- {
99
- string site = GetSiteName ( request ) ;
100
-
101
- if ( Docs . ContainsKey ( site ) )
102
- {
103
- var pair = Docs [ site ] ;
104
- var path = request . RawUrl . Trim ( '/' ) ;
105
-
106
- while ( true )
107
- {
108
- string clean = string . IsNullOrEmpty ( path ) ? "index" : path ;
109
- var result = pair . Keys . SingleOrDefault ( k => k . Equals ( clean , StringComparison . OrdinalIgnoreCase ) ) ;
110
-
111
- if ( result != null )
112
- return result ;
113
-
114
- int index = path . LastIndexOf ( '/' ) ;
115
- if ( index == - 1 )
116
- break ;
117
-
118
- path = path . Substring ( 0 , index ) ;
119
- }
120
- }
121
-
122
- return Title ;
123
- }
124
-
125
- public static string GetBaseName ( HttpRequestBase request )
126
- {
127
- string clean = request . RawUrl . Trim ( '/' ) ;
128
-
129
- if ( Docs . Any ( d => d . Key . Equals ( clean , StringComparison . OrdinalIgnoreCase ) ) )
130
- return clean ;
131
-
132
- int index = clean . IndexOf ( '/' ) ;
133
-
134
- if ( index > - 1 )
135
- clean = clean . Substring ( 0 , index ) ;
136
-
137
- return clean ;
138
- }
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Linq ;
5
+ using System . Web ;
6
+ using System . Web . Hosting ;
7
+ using System . Xml ;
8
+
9
+ public static class Checklist
10
+ {
11
+ // Public
12
+ public static Dictionary < string , Dictionary < string , XmlDocument > > Docs = new Dictionary < string , Dictionary < string , XmlDocument > > ( ) ;
13
+ public const string Title = "Web Developer Checklist" ;
14
+
15
+ // Private
16
+ private const string defaultSite = "webdevchecklist.com" ;
17
+ private static string SitesFolder = HostingEnvironment . MapPath ( "~/sites/" ) ;
18
+
19
+ static Checklist ( )
20
+ {
21
+ BuildCache ( ) ;
22
+ }
23
+
24
+ private static void BuildCache ( )
25
+ {
26
+ Docs . Clear ( ) ;
27
+
28
+ foreach ( string folder in Directory . GetDirectories ( SitesFolder ) )
29
+ {
30
+ var dic = new Dictionary < string , XmlDocument > ( ) ;
31
+
32
+ foreach ( string file in Directory . GetFiles ( folder , "*.xml" , SearchOption . AllDirectories ) )
33
+ {
34
+ XmlDocument doc = new XmlDocument ( ) ;
35
+ doc . Load ( file ) ;
36
+
37
+ string key = file
38
+ . Replace ( folder , string . Empty )
39
+ . Replace ( "Sections" , string . Empty )
40
+ . Replace ( "\\ " , "/" )
41
+ . Replace ( ".xml" , string . Empty )
42
+ . TrimStart ( '/' ) ;
43
+
44
+ if ( key . Contains ( "/" ) && key . EndsWith ( "index" ) )
45
+ {
46
+ key = key . Replace ( "/index" , string . Empty ) ;
47
+ }
48
+
49
+ dic . Add ( key , doc ) ;
50
+ }
51
+
52
+ Docs . Add ( Path . GetFileName ( folder ) , dic ) ;
53
+ }
54
+ }
55
+
56
+ public static XmlDocument GetXmlDocument ( HttpRequestBase request )
57
+ {
58
+ string site = GetSiteName ( request ) ;
59
+
60
+ var section = Docs [ site ] ;
61
+ var pageName = GetPageName ( request ) ;
62
+ var result = section . Keys . SingleOrDefault ( k => k . Equals ( pageName , StringComparison . OrdinalIgnoreCase ) ) ;
63
+
64
+ if ( result != null )
65
+ {
66
+ return section [ result ] ;
67
+ }
68
+
69
+ return Docs [ site ] [ "index" ] ;
70
+ }
71
+
72
+ public static string GetSiteName ( HttpRequestBase request )
73
+ {
74
+ return request . Url . Host . StartsWith ( "localhost" ) ? defaultSite : request . Url . Host ;
75
+ }
76
+
77
+ public static string GetSiteSectionFolder ( HttpRequestBase request )
78
+ {
79
+ string siteName = GetSiteName ( request ) ;
80
+
81
+ return Path . Combine ( SitesFolder , siteName , "Sections" ) ;
82
+ }
83
+
84
+ public static string GetPageTitle ( HttpRequestBase request )
85
+ {
86
+ XmlDocument doc = GetXmlDocument ( request ) ;
87
+ XmlAttribute attr = doc . SelectSingleNode ( "checklist" ) . Attributes [ "name" ] ;
88
+
89
+ if ( attr != null )
90
+ {
91
+ return attr . InnerText ;
92
+ }
93
+
94
+ return Title ;
95
+ }
96
+
97
+ public static string GetPageName ( HttpRequestBase request )
98
+ {
99
+ string site = GetSiteName ( request ) ;
100
+
101
+ if ( Docs . ContainsKey ( site ) )
102
+ {
103
+ var pair = Docs [ site ] ;
104
+ var path = request . RawUrl . Trim ( '/' ) ;
105
+
106
+ while ( true )
107
+ {
108
+ string clean = string . IsNullOrEmpty ( path ) ? "index" : path ;
109
+ var result = pair . Keys . SingleOrDefault ( k => k . Equals ( clean , StringComparison . OrdinalIgnoreCase ) ) ;
110
+
111
+ if ( result != null )
112
+ return result ;
113
+
114
+ int index = path . LastIndexOf ( '/' ) ;
115
+ if ( index == - 1 )
116
+ break ;
117
+
118
+ path = path . Substring ( 0 , index ) ;
119
+ }
120
+ }
121
+
122
+ return Title ;
123
+ }
124
+
125
+ public static string GetBaseName ( HttpRequestBase request )
126
+ {
127
+ string clean = request . RawUrl . Trim ( '/' ) ;
128
+
129
+ if ( Docs . Any ( d => d . Key . Equals ( clean , StringComparison . OrdinalIgnoreCase ) ) )
130
+ return clean ;
131
+
132
+ int index = clean . IndexOf ( '/' ) ;
133
+
134
+ if ( index > - 1 )
135
+ clean = clean . Substring ( 0 , index ) ;
136
+
137
+ return clean ;
138
+ }
139
139
}
0 commit comments