-
Notifications
You must be signed in to change notification settings - Fork 2
/
Template.ahk
257 lines (227 loc) · 9.86 KB
/
Template.ahk
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#NoEnv
/*
Copyright 2011 Anthony Zhang <azhang9@gmail.com>
This file is part of AutoHotkey.net Website Generator. Source code is available at <https://github.com/Uberi/AutoHotkey.net-Website-Generator>.
AutoHotkey.net Website Generator is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
;initializes resources needed by the templating engine
TemplateInit()
{
global ForumUsername, Template, TemplateSingleTags, TemplateMatchedTags, TemplateProperties, TemplateScriptProperties, TemplateAttributePattern, TemplateTagPattern
TemplateSingleTags := Object("ahk_script",Func("TemplateProcessScript")) ;self contained tag
TemplateMatchedTags := Object("ahk_for_each",Func("TemplateProcessForEach")
,"ahk_repeat",Func("TemplateProcessRepeat")
,"ahk_if",Func("TemplateProcessIf")
,"ahk_if_not",Func("TemplateProcessIfNot")) ;matched tag
;template properties
FormatTime, Temp1,, Time
FormatTime, Temp2,, ShortDate
TemplateProperties := Object("Author",ForumUsername
,"Time",Temp1
,"Date",Temp2
,"Template",Template)
TemplateScriptProperties := Object() ;set up an empty script properties object
;build up the pattern matching template tags
TemplateAttributePattern := "\s+([\w-]+)(?:\s*=\s*(?:""([^""]*)""|'([^']*)'|([^\s""'``=<>]*)))?" ;pattern matching a single tag attribute
TemplateTagPattern := "iS)<("
For Key In TemplateSingleTags
TemplateTagPattern .= Key . "|" ;insert tag name into the pattern
For Key In TemplateMatchedTags
TemplateTagPattern .= Key . "|" ;insert opening and closing tag names into the pattern
TemplateTagPattern := SubStr(TemplateTagPattern,1,-1) . ")((?:" . TemplateAttributePattern . ")*)\s*>"
}
;parses an HTML template and processes any template tags that are present
TemplatePage(Template)
{
global TemplateSingleTags, TemplateMatchedTags, TemplateAttributePattern, TemplateTagPattern
Position := 1, Position1 := 1, Result := "" ;initialize variables
While, Position := RegExMatch(Template,TemplateTagPattern,Output,Position) ;loop over each opening or closing template HTML tag
{
Result .= SubStr(Template,Position1,Position - Position1) ;append the sections of the template between template tags
Position += StrLen(Output), Position1 := Position ;move past the tag, store the position
If ObjHasKey(TemplateMatchedTags,Output1) ;tag is to be matched
{
MatchedPosition := TemplateMatchTag(Template,Position,Output1,TagContents)
If (MatchedPosition = 0) ;skip over mismatched tag
Continue
Result .= TemplateMatchedTags[Output1](TemplateAttributes(Output2),TagContents)
Position := MatchedPosition, Position1 := MatchedPosition ;move to the end of the closing tag
}
Else ;self contained tag
Result .= TemplateSingleTags[Output1](TemplateAttributes(Output2)) ;process the template tag
}
Return, Result . SubStr(Template,Position1) ;return the resulting page with the last section appended
}
;matches a template tag
TemplateMatchTag(ByRef Template,Position,TagName,ByRef TagContents)
{
global TemplateAttributePattern
TagDepth := 1, Position1 := Position
While, (TagDepth > 0) && (Position := RegExMatch(Template,"iS)<(/?)" . TagName . "(?:" . TemplateAttributePattern . ")*\s*>",Output,Position))
{
Position += StrLen(Output)
If (Output1 = "") ;opening tag
TagDepth ++
Else ;closing tag
TagDepth --
}
If TagDepth > 0 ;tag could not be matched
Return, 0
TagContents := SubStr(Template,Position1,Position - (Position1 + StrLen(Output)))
Return, Position
}
;parses the template tag attributes into an object
TemplateAttributes(Attributes)
{
global TemplateAttributePattern
Position := 1, Result := Object() ;initialize variables
While, Position := RegExMatch(Attributes,TemplateAttributePattern,Output,Position) ;loop over each tag attribute
{
Position += StrLen(Output) ;move past the current attribute
Result[Output1] := (Output2 != "") ? Output2 : ((Output3 != "") ? Output3 : Output4) ;set the attribute in the result object
}
Return, Result
}
TemplateProcessScript(This,Attributes)
{
global TemplateProperties, TemplateScriptProperties
For Key In Attributes
{
If ObjHasKey(TemplateProperties,Key)
Return, TemplateProperties[Key]
If ObjHasKey(TemplateScriptProperties,Key)
Return, TemplateScriptProperties[Key]
}
}
TemplateProcessForEach(This,Attributes,TagContents)
{
global TemplateScriptProperties
ScriptProperties := TemplateScriptProperties ;backup the script property object to be restored afterwards
TemplateScriptProperties := Object() ;create a new script property object for the current tag
;look for type filters
TypeFilter := ""
If ObjHasKey(Attributes,"Script")
TypeFilter := "Script"
Else If ObjHasKey(Attributes,"Library")
TypeFilter := "Library"
;loop through each script
Result := ""
For Index, Entry In GetResults(TypeFilter)
{
;prepare the script properties object
TemplateScriptProperties.Index := Index
TemplateScriptProperties.Fragment := Entry.Fragment
TemplateScriptProperties.Title := HTMLEscape(Entry.Title)
TemplateScriptProperties.Image := HTMLEscape(Entry.Image)
;unescape certain HTML entities
FoundPos := 1, FoundPos1 := 1, Description := ""
While, FoundPos := RegExMatch(Entry.Description,"S)<[^>]+>",Match,FoundPos) ;unescape quotes only inside hyperlink tags
{
Description .= HTMLEscape(SubStr(Entry.Description,FoundPos1,FoundPos - FoundPos1)) . Match
FoundPos += StrLen(Match), FoundPos1 := FoundPos
}
Description .= SubStr(Entry.Description,FoundPos1)
TemplateScriptProperties.Description := Description
TemplateScriptProperties.Topic := HTMLEscape(Entry.URL)
TemplateScriptProperties.Source := HTMLEscape(Entry.Source)
Result .= TemplatePage(TagContents)
}
TemplateScriptProperties := ScriptProperties ;restore the previous script property object
Return, Result
}
TemplateProcessRepeat(This,Attributes,TagContents)
{
RepeatCount := 1, Result := ""
For Key In Attributes
{
If Key Is Integer
{
RepeatCount := Key
Break
}
}
Loop, %RepeatCount%
Result .= TemplatePage(TagContents)
Return, Result
}
TemplateProcessIf(This,Attributes,TagContents)
{
global TemplateProperties, TemplateScriptProperties
For Key In Attributes
{
If ObjHasKey(TemplateProperties,Key) && TemplateProperties[Key] != "" ;key is present and not blank in template properties
Return, TemplatePage(TagContents)
If ObjHasKey(TemplateScriptProperties,Key) && TemplateScriptProperties[Key] != "" ;key is present and not blank in script properties
Return, TemplatePage(TagContents)
Break
}
}
TemplateProcessIfNot(This,Attributes,TagContents)
{
global TemplateProperties, TemplateScriptProperties
For Key In Attributes
{
If ObjHasKey(TemplateProperties,Key) && TemplateProperties[Key] != "" ;key is present and not blank in template properties
Return
If ObjHasKey(TemplateScriptProperties,Key) && TemplateScriptProperties[Key] != "" ;key is present and not blank in script properties
Return
Break
}
Return, TemplatePage(TagContents)
}
;retrieve the results of searching the forum, or the cached results if available
GetResults(TypeFilter = "")
{
global ForumUsername, SortEntries, RelativeLinks, DownloadResources
static Results := ""
If !IsObject(Results)
{
Results := ForumSearchAll(ForumUsername)
;add URL fragments to each result
UsedFragmentList := Object() ;an object containing all URL fragments generated so far
For Index, Result In Results
Result.Fragment := GenerateURLFragment(Result.Title,UsedFragmentList)
;process options
If RelativeLinks
MakeRelativeLinks(Results)
If DownloadResources
DownloadPageResources(Results)
If SortEntries
Results := SortByTitle(Results)
}
If (TypeFilter != "") ;process the script type filter if given
{
Filtered := []
For Index, Result In Results
{
If (DetectTopicCategory(Result.Title,Result.Description) = TypeFilter)
ObjInsert(Filtered,Result)
}
Return, Filtered
}
Return, Results
}
;generates a unique URL fragment from a title
GenerateURLFragment(Title,UsedFragmentList)
{
Fragment := RegExReplace(Title,"S)\W")
If ObjHasKey(UsedFragmentList,Fragment) ;fragment has already been used
{
;find an unused fragment
Index := 1
While, ObjHasKey(UsedFragmentList,Fragment . Index)
Index ++
Fragment .= Index
}
UsedFragmentList[Fragment] := "" ;reserve the used fragment in the used fragment list
Return, Fragment
}