This repository was archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHtmlTransformer.cs
More file actions
184 lines (155 loc) · 6.8 KB
/
Copy pathHtmlTransformer.cs
File metadata and controls
184 lines (155 loc) · 6.8 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
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
174
175
176
177
178
179
180
181
182
183
184
/*
* @version: 2.1.1
* @author: Ext.NET, Inc. http://www.ext.net/
* @date: 2012-12-16
* @copyright: Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license: See license.txt and http://www.ext.net/licensetransformer/.
* @website: http://www.ext.net/
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Transformer.NET.Html
{
public class HtmlTransformer : TextTransformer
{
private static Regex headStartRegex = new Regex(@"<head[.\w\W]*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static Regex headEndRegex = new Regex(@"</head>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static Regex titleStartRegex = new Regex(@"<title[.\w\W]*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static Regex titleEndRegex = new Regex(@"</title>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static Regex bodyStartRegex = new Regex(@"<body[.\w\W]*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static Regex bodyEndRegex = new Regex(@"</body>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
public HtmlTransformer(string text) : base(text) { }
public HtmlTransformer(string text, TextTransformerConfig config) : base(text, config) { }
public override List<Type> StandardTokens
{
get
{
List<Type> tokens = base.StandardTokens;
tokens.AddRange(new List<Type>
{
typeof(JsTag),
typeof(CssTag),
typeof(ScriptTag),
typeof(StyleTag)
});
return tokens;
}
}
Dictionary<string, TokenSelector> selectors;
public override Dictionary<string, TokenSelector> Selectors
{
get
{
if (this.selectors == null)
{
this.selectors = new Dictionary<string, TokenSelector>();
this.selectors.Add("headstart", new TokenSelector { Regex = headStartRegex });
this.selectors.Add("headend", new TokenSelector { Regex = headEndRegex, Position = SelectorPosition.Before });
this.selectors.Add("titlestart", new TokenSelector { Regex = titleStartRegex });
this.selectors.Add("titleend", new TokenSelector { Regex = titleEndRegex, Position = SelectorPosition.Before });
this.selectors.Add("bodystart", new TokenSelector { Regex = bodyStartRegex });
this.selectors.Add("bodyend", new TokenSelector { Regex = bodyEndRegex, Position = SelectorPosition.Before });
}
return this.selectors;
}
}
private Dictionary<string, DefaultTemplateTag> defaultTemplates;
public override Dictionary<string, DefaultTemplateTag> DefaultTemplates
{
get
{
if (this.defaultTemplates == null)
{
this.defaultTemplates = new Dictionary<string, DefaultTemplateTag>();
this.defaultTemplates.Add("js", new DefaultTemplateTag("js", "<script type=\"text/javascript\" src=\"{0}\"></script>"));
this.defaultTemplates.Add("script", new DefaultTemplateTag("script", "<script type=\"text/javascript\">{0}</script>"));
this.defaultTemplates.Add("cdata", new DefaultTemplateTag("cdata", "<script type=\"text/javascript\">\n\t//<![CDATA[\n\t\t{0}\n\t//]]>\n\t</script>"));
this.defaultTemplates.Add("css", new DefaultTemplateTag("css", "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />"));
this.defaultTemplates.Add("style", new DefaultTemplateTag("style", "<style type=\"text/css\">\n{0}\t</style>"));
this.defaultTemplates.Add("title", new DefaultTemplateTag("title", "<title>{0}</title>"));
}
return this.defaultTemplates;
}
}
new public static string Transform(string text)
{
return new Transformer.NET.Html.HtmlTransformer(text).Transform();
}
new public static string Transform(string text, TextTransformerConfig config)
{
return new Transformer.NET.Html.HtmlTransformer(text, config).Transform();
}
new public static string Transform(string text, List<Type> tokensType)
{
return new Transformer.NET.Html.HtmlTransformer(text).Transform(tokensType);
}
new public static string Transform(string text, Dictionary<string, string> variables)
{
return new Transformer.NET.Html.HtmlTransformer(text).Transform(variables);
}
new public static string Transform(string text, List<Type> tokensType, Dictionary<string, string> variables)
{
return new Transformer.NET.Html.HtmlTransformer(text).Transform(tokensType, variables);
}
}
[TagName("js")]
public class JsTag : ItemTag
{
public JsTag(Match match) : base(match) { }
public override string Template
{
get
{
return "js";
}
}
public virtual string Path
{
get
{
return this.GetAttribute("path");
}
}
public override string ApplyTemplate(TemplateTag tpl)
{
return tpl.Apply(this.Path);
}
}
[TagName("script")]
public class ScriptTag : ItemTag
{
public ScriptTag(Match match) : base(match) { }
public override string Template
{
get
{
return "script";
}
}
}
[TagName("css")]
public class CssTag : JsTag
{
public CssTag(Match match) : base(match) { }
public override string Template
{
get
{
return "css";
}
}
}
[TagName("style")]
public class StyleTag : ItemTag
{
public StyleTag(Match match) : base(match) { }
public override string Template
{
get
{
return "style";
}
}
}
}