forked from huggle/huggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Highlight.vb
706 lines (549 loc) · 28.7 KB
/
Highlight.vb
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
'This is a source code or part of Huggle project
'This file contains code for edit form
'Copyright (C) 2011 Huggle team
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU 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 General Public License for more details.
Imports System.Threading
Imports System.Text
Module Highlight
Public FontName As String = "Courier New"
Public CommentC As Color = Color.FromArgb(255, 128, 128, 128)
Public LinkC As Color = Color.FromArgb(255, 0, 0, 255)
Public MagicWordC As Color = Color.FromArgb(255, 0, 128, 128)
Public ExternalC As Color = Color.FromArgb(255, 0, 128, 208)
Public TemplateC As Color = Color.FromArgb(255, 255, 0, 0)
Public HtmlC As Color = Color.FromArgb(255, 160, 0, 160)
Public ParamCallC As Color = Color.FromArgb(255, 160, 0, 0)
Public ReferenceHC As Color = Color.FromArgb(255, 208, 255, 208)
Public ImageHC As Color = Color.FromArgb(255, 216, 216, 255)
Public ParameterHC As Color = Color.FromArgb(255, 255, 255, 160)
Public ParamNameC As Color = Color.FromArgb(255, 128, 80, 0)
Public ReadOnly Property RtfHeader() As String
Get
Return "{\rtf1{\fonttbl{\f0 " & FontName & ";}}{\colortbl ;" & _
CS(CommentC) & CS(LinkC) & CS(MagicWordC) & CS(ExternalC) & CS(TemplateC) & CS(HtmlC) & _
CS(ParamCallC) & CS(ReferenceHC) & CS(ImageHC) & CS(ParameterHC) & CS(ParamNameC) & "}\f0\fs20 "
End Get
End Property
Public ReadOnly Property RtfFooter() As String
Get
Return "\par}"
End Get
End Property
Private Function CS(ByVal Color As Color) As String
Return "\red" & CStr(Color.R) & "\green" & CStr(Color.G) & "\blue" & CStr(Color.B) & ";"
End Function
Public Function RtfEscape(ByVal Text As String) As String
Dim Break As Integer = 0
Text = Text.Replace("\", "\\").Replace("{", "\{").Replace("}", "\}").Replace(LF, "\par ")
Dim j As Integer = 0
While j < Text.Length And Break < Misc.GlExcess
Break = Break + 1
Dim a As Integer = Convert.ToInt32(Text(j))
If a > 127 Then
Text.Remove(j, 1)
Text.Insert(j, "\u" & CStr(a).PadLeft(5, "0"c) & "?")
End If
j += 1
End While
Return Text
End Function
Class HighlightRequest
Private _Done As HighlightCallback, _Text As String, Thread As Thread
Private MagicWords() As String = {"ARTICLEPAGENAME", "ARTICLEPAGENAMEE", "ARTICLESPACE", "ARTICLESPACEE", _
"BASEPAGENAME", "BASEPAGENAMEE", "CONTENTLANGUAGE", "CURRENTDAY", "CURRENTDAY2", "CURRENTDAYNAME", _
"CURRENTDOW", "CURRENTHOUR", "CURRENTMONTH", "CURRENTMONTHABBREV", "CURRENTMONTHNAME", _
"CURRENTMONTHNAMEGEN", "CURRENTTIME", "CURRENTTIMESTAMP", "CURRENTVERSION", "CURRENTWEEK", "CURRENTYEAR", _
"DIRMARK", "DIRECTIONMARK", "DISPLAYTITLE", "FULLPAGENAME", "FULLPAGENAMEE", "LOCALDAY", "LOCALDAY2", _
"LOCALDAYNAME", "LOCALDOW", "LOCALHOUR", "LOCALMONTH", "LOCALMONTHABBREV", "LOCALMONTHNAME", _
"LOCALMONTHNAMEGEN", "LOCALTIME", "LOCALTIMESTAMP", "LOCALWEEK", "LOCALYEAR", "NAMESPACE", "NAMESPACEE", _
"NUMBEROFADMINS", "NUMBEROFARTICLES", "NUMBEROFEDITS", "NUMBEROFFILES", "NUMBEROFUSERS", "PAGENAME", _
"PAGENAMEE", "PAGESINCAT", "PAGESINCATEGORY", "REVISIONID", "REVISIONDAY", "REVISIONDAY", "REVISIONMONTH", _
"REVISIONTIMESTAMP", "REVISIONYEAR", "SITENAME", "SCRIPTPATH", "SERVER", "SERVERNAME", "SUBJECTSPACE", _
"SUBJECTSPACEE", "SUBPAGENAME", "SUBPAGENAMEE", "TALKPAGENAME", "TALKPAGENAMEE", "TALKSPACE", _
"TALKSPACEE", "anchorencode", "filepath", "formatnum", "fullurl", "fullurle", "grammar", "int", "lc", _
"lcfirst", "localurl", "localurle", "msg", "msgnw", "ns", "raw", "padleft", "padright", "plural", "uc", _
"ucfirst"}
Private Directives() As String = {"#REDIRECT", "__FORCETOC__", "__HIDDENCAT__", "__INDEX__", _
"__NEWSECTIONLINK__", "__NOCC__", "__NOCONTENTCONVERT__", "__NOEDITSECTION__", "__NOGALLERY__", _
"__NOHEADER__", "__NOINDEX__", "__NOTC__", "__NOTITLECONVERT__", "__NOTOC__", "__STATICREDIRECT__", _
"__TOC__"}
Public Delegate Sub HighlightCallback(ByVal Result As String)
Private Class TemplateData
Public Text As String
Public Templates As New List(Of TemplateData)
Sub New(ByVal Text As String)
Me.Text = Text
End Sub
End Class
Public Sub Start(ByVal Text As String, ByVal Done As HighlightCallback)
_Done = Done
_Text = Text
Debug.WriteLine("thread start")
Thread = New Thread(AddressOf HighlightThread)
Thread.IsBackground = True
Thread.Start()
End Sub
Public Sub Cancel()
Thread.Abort()
End Sub
Private Sub HighlightThread()
Dim Break As Integer = 0
Dim Comment As New List(Of String), Nowiki As New List(Of String)
Dim Templates As New List(Of TemplateData)
Dim Open, Close As Integer
Dim Source As New StringBuilder(_Text, _Text.Length * 3)
Try
Source.Replace("\", "\\").Replace("{", "\{").Replace("}", "\}")
For Each Item As String In Directives
Source = Source.Replace(Item, "{\cf3 " & Item & "}")
Next Item
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("<!--", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("-->", Open)
If Close = -1 Then Exit While
Comment.Add("{\cf1 " & Str.Substring(Open, Close - Open + 3) & "}")
Source.Remove(Open, Close - Open + 3)
Source.Insert(Open, Convert.ToChar(0))
Close = Open + 1
End While
If Break >= Misc.GlExcess - 1 Then Log("Debug interrupted at Higlight.HighlightThread()")
Close = 0
'reset
Break = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("<nowiki>", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("</nowiki>", Open)
If Close = -1 Then Exit While
Nowiki.Add("{\cf0 " & Str.Substring(Open + 8, Close - Open - 8) & "}")
Source.Remove(Open + 8, Close - Open - 8)
Source.Insert(Open + 8, Convert.ToChar(1))
Close = Open + 1
End While
Source.Replace("~~~~", "{\cf3\b ~~~~}")
Close = 0
Break = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Open = Str.IndexOf("__", Close)
Break = Break + 1
If Open = -1 Then Exit While
Close = Str.IndexOf("__", Open + 1)
If Close = -1 Then Exit While
Close += 10
Source.Insert(Open, "{\cf3\b ")
Source.Insert(Close, "}")
End While
Close = 0
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Close = 0
Break = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("<u>", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("</u>", Open)
If Close = -1 Then Exit While
Close += 5
Source.Insert(Open + 3, "{\ul ")
Source.Insert(Close, "}")
End While
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("<ref", Close)
If Open = -1 Then Exit While
Dim Close1 As Integer = Str.IndexOf("</ref>", Open), _
Close2 As Integer = Str.IndexOf("/>", Open), _
Close3 As Integer = Str.IndexOf(">", Open)
If Close2 > -1 AndAlso Close2 = Close3 - 1 Then
Close = Close3 + 1
ElseIf Close1 > -1 Then
Close = Close1 + 6
Else
Exit While
End If
Close += 13
Source.Insert(Open, "{\highlight8 ")
Source.Insert(Close, "}")
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("[[", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("]]", Open)
If Close = -1 Then Exit While
Dim Position As Integer = Open + 1, Level As Integer = 1
While Level > 0 And Break < Misc.GlExcess
Dim NextOpen As Integer = Str.IndexOf("[[", Position)
Break = Break + 1
Dim NextClose As Integer = Str.IndexOf("]]", Position)
If NextOpen > -1 AndAlso NextOpen < NextClose Then
Level += 1
Position = NextOpen + 1
Else
Level -= 1
Position = NextClose + 1
End If
End While
If Position = 0 Then Exit While
Close = Position - 1
Dim LinkText As String = Str.Substring(Open + 2, Close - Open - 2)
Source.Remove(Open, Close - Open + 2)
If LinkText.StartsWith("Image:") Then
Dim Index As Integer = LinkText.IndexOf("|")
If Index > -1 Then
LinkText = "{\cf2 " & LinkText.Substring(0, Index) & "}{\b |}" & LinkText.Substring(Index + 1)
Else
LinkText = "{\cf2 " & LinkText & "}"
End If
Source.Insert(Open, "{\cf0\highlight9 [[" & LinkText & "]]}")
Close = Open + 20
Else
Dim Index1 As Integer = LinkText.IndexOf("|")
Dim Index2 As Integer = LinkText.IndexOf("\{\{")
Dim Index As Integer = -1
If Index1 > -1 Then Index = Index1
If Index2 > -1 AndAlso Index2 < Index1 Then Index = Index2
If Index > -1 Then
LinkText = "{\cf2 " & LinkText.Substring(0, Index) & "}" & LinkText.Substring(Index)
Else
LinkText = "{\cf2 " & LinkText & "}"
End If
Source.Insert(Open, "{\cf0 [[}" & LinkText & "{\cf0 ]]}")
Close = Open + 7
End If
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("http://", Close)
If Open = -1 Then Exit While
Close = Source.Length + 1
Dim Close1 As Integer = Str.IndexOf(" ", Open)
Dim Close2 As Integer = Str.IndexOf("|", Open)
Dim Close3 As Integer = Str.IndexOf("}", Open)
Dim Close4 As Integer = Str.IndexOf(LF, Open)
If Close1 > -1 Then Close = Close1
If Close2 > -1 AndAlso Close2 < Close Then Close = Close2
If Close3 > -1 AndAlso Close3 < Close Then Close = Close3 - 1
If Close4 > -1 AndAlso Close4 < Close Then Close = Close4
If Close = Source.Length + 1 OrElse Close = -1 Then Exit While
Dim Index As Integer = Str.Substring(Open, Close - Open).IndexOf(" ")
Close += 6
Source.Insert(Open, "{\cf4 ")
If Index > -1 Then Source.Insert(Open + Index + 6, "}") Else Source.Insert(Close, "}")
End While
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("'''''", Close)
If Open = -1 Then Exit While
Dim Close1 As Integer = Str.IndexOf(LF, Open + 2)
Dim Close2 As Integer = Str.IndexOf("'''''", Open + 2) + 5
Close = -1
If Close1 > -1 Then Close = Close1
If Close2 > -1 AndAlso (Close1 = -1 OrElse Close2 < Close1) Then Close = Close2
If Close = -1 Then Exit While
Close += 6
Source.Insert(Open, "{\b\i ")
Source.Insert(Close, "}")
End While
Close = 0
If Break >= Misc.GlExcess Then Log("Debug interrupted at HiglightThread")
'reset counter
Break = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("'''", Close)
If Open = -1 Then Exit While
If Open < Source.Length - 3 AndAlso Source(Open + 3) = "'" Then
Close = Open + 3
Continue While
End If
Dim Close1 As Integer = Str.IndexOf(LF, Open + 3)
Dim Close2 As Integer = Str.IndexOf("'''", Open + 3) + 3
Close = -1
If Close1 > -1 Then Close = Close1
If Close2 > -1 AndAlso (Close1 = -1 OrElse Close2 < Close1) Then Close = Close2
If Close = -1 Then Exit While
Close += 4
Source.Insert(Open, "{\b ")
Source.Insert(Close, "}")
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at HiglightThread")
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("''", Close)
If Open = -1 Then Exit While
If Open < Source.Length - 2 AndAlso Source(Open + 2) = "'" Then
Close = Open + 2
Continue While
End If
Dim Close1 As Integer = Str.IndexOf(LF, Open + 2)
Dim Close2 As Integer = Str.IndexOf("''", Open + 2) + 2
Close = -1
If Close1 > -1 Then Close = Close1
If Close2 > -1 AndAlso (Close1 = -1 OrElse Close2 < Close1) Then Close = Close2
If Close = -1 Then Exit While
Close += 4
Source.Insert(Open, "{\i ")
Source.Insert(Close, "}")
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Break = 0
Close = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("\{\{\{", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("\}\}\}", Open)
If Close = -1 Then Exit While
If Str.Substring(Open + 6, 2) = "\{" Then Continue While
Dim ParameterText As String = Str.Substring(Open + 6, Close - Open - 6)
Dim Index As Integer = ParameterText.IndexOf("|")
If Index > -1 Then ParameterText = ParameterText.Substring(0, Index) & "}{\b |}{" _
& ParameterText.Substring(Index + 1)
ParameterText = "{\b0\cf0\highlight10 \{\{\{{\cf11\b " & ParameterText & "}\}\}\}}"
Source.Remove(Open, Close - Open + 6)
Source.Insert(Open, ParameterText)
Close += 32
End While
Close = 0
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Close = 0
Break = 0
While True And Break < Misc.GlExcess
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("<", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf(">", Open)
If Close = -1 Then Exit While
Dim HtmlText As String = Str.Substring(Open + 1, Close - Open - 1)
Dim Index As Integer = HtmlText.IndexOf(" ")
If Index > -1 Then HtmlText = HtmlText.Insert(Index, "}") Else HtmlText &= "}"
HtmlText = "{\cf0\b0 <{\cf6\b " & HtmlText & ">}"
Source.Remove(Open, Close - Open + 1)
Source.Insert(Open, HtmlText)
Close += 20
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
Break = 0
Close = 0
While True
Dim Str As String = Source.ToString
Break = Break + 1
If Str.StartsWith("==") Then Open = 0 Else Open = Str.IndexOf(LF & "==", Close)
If Open = -1 Then Exit While
Close = Str.IndexOf("==", Open + 4)
If Close = -1 Then Exit While
Source.Insert(Open, "{\b ")
Source.Insert(Close + 6, "}")
Close += 7
End While
Break = 0
Close = 0
While True
Dim Str As String = Source.ToString
Break = Break + 1
Open = Str.IndexOf("\{\{", Close)
If Open = -1 OrElse Open > Str.Length - 8 Then Exit While
Close = Open + 4
If Str.Substring(Open + 4, 2) = "\{" AndAlso Str.Substring(Open + 6, 2) <> "\{" Then Continue While
Dim Level As Integer = 1
While Level > 0 And Break < Misc.GlExcess
Dim NextOpen, NextClose As Integer
Dim ThisClose As Integer = Close
Break = Break + 1
Do
NextOpen = Str.IndexOf("\{\{", ThisClose)
If NextOpen = -1 OrElse NextOpen > Str.Length - 8 Then Exit Do
If Not (Str.Substring(NextOpen + 4, 2) = "\{" AndAlso Str.Substring(NextOpen + 6, 2) <> "\{") _
Then Exit Do
ThisClose = NextOpen + 6
Loop Until Break < Misc.GlExcess
Do
NextClose = Str.IndexOf("\}\}", Close)
If NextClose = -1 OrElse NextClose > Str.Length - 8 Then Exit Do
If Not (Str.Substring(NextClose + 4, 2) = "\}" AndAlso Str.Substring(NextClose + 6, 2) <> "\}") _
Then Exit Do
Close = NextClose + 6
Loop
If NextOpen > -1 AndAlso NextOpen < NextClose Then
Level += 1
Close = NextOpen + 4
Else
Level -= 1
Close = NextClose + 4
End If
End While
Close -= 4
If Close = -1 Then Exit While
Dim TemplateText As String = Str.Substring(Open, Close - Open + 4)
Dim TemplateItem As New TemplateData(TemplateText)
Templates.Add(TemplateItem)
ParseTemplate(TemplateItem)
Source.Remove(Open, Close - Open + 4)
Source.Insert(Open, Convert.ToChar(14))
Close = Open + 1
End While
If Break >= Misc.GlExcess Then Log("Debug interrupted at Higlight.HighlightThread()")
For Each Item As TemplateData In Templates
UnpackTemplates(Item)
Dim Index As Integer = Source.ToString.IndexOf(Convert.ToChar(14))
If Index > -1 Then
Source.Remove(Index, 1)
Source.Insert(Index, Item.Text)
End If
Next Item
Dim Lists() As List(Of String) = {Nowiki, Comment}
If (Lists.Length - 1) > 0 Then
For i As Integer = 0 To Lists.Length - 1
For Each Item As String In Lists(i)
Dim Index As Integer = Source.ToString.IndexOf(Convert.ToChar(1 - i))
If Index > -1 Then
Source.Remove(Index, 1)
Source.Insert(Index, Item)
End If
Next Item
Next i
End If
Source.Replace(LF, "\par ")
Dim j As Integer = 0
While j < Source.Length
Dim a As Integer = Convert.ToInt32(Source(j))
If a > 127 Then
Source.Remove(j, 1)
Source.Insert(j, "\u" & CStr(a).PadLeft(5, "0"c) & "?")
End If
j += 1
End While
Catch ex As Exception
ExceptionForm.Show()
End Try
Source.Insert(0, RtfHeader)
Source.Append(RtfFooter)
SyncContext.Post(AddressOf HighlightDone, CObj(Source.ToString))
End Sub
Private Sub HighlightDone(ByVal SourceObject As Object)
If _Done IsNot Nothing Then _Done(CStr(SourceObject))
End Sub
Private Sub ParseTemplate(ByVal Data As TemplateData)
Debug.WriteLine("parse")
Dim Break As Integer = 0
Dim Open, Close As Integer
Close = 4
While True And Break < Misc.GlExcess
Break = Break + 1
Open = Data.Text.IndexOf("\{\{", Close)
If Open = -1 OrElse Open > Data.Text.Length - 8 Then Exit While
Close = Open + 4
If Data.Text.Substring(Open + 4, 2) = "\{" AndAlso Data.Text.Substring(Open + 6, 2) <> "\{" _
Then Continue While
Dim Level As Integer = 1
While Level > 0
Dim NextOpen, NextClose As Integer
Dim ThisClose As Integer = Close
Do
NextOpen = Data.Text.IndexOf("\{\{", ThisClose)
If NextOpen = -1 OrElse NextOpen > Data.Text.Length - 8 Then Exit Do
If Not (Data.Text.Substring(NextOpen + 4, 2) = "\{" _
AndAlso Data.Text.Substring(NextOpen + 6, 2) <> "\{") Then Exit Do
ThisClose = NextOpen + 6
Loop
Do
NextClose = Data.Text.IndexOf("\}\}", Close)
If NextClose = -1 OrElse NextClose > Data.Text.Length - 8 Then Exit Do
If Not (Data.Text.Substring(NextClose + 4, 2) = "\}" _
AndAlso Data.Text.Substring(NextClose + 6, 2) <> "\}") Then Exit Do
Close = NextClose + 6
Loop
If NextOpen > -1 AndAlso NextOpen < NextClose Then
Level += 1
Close = NextOpen + 4
Else
Level -= 1
Close = NextClose + 4
End If
End While
Close -= 4
If Close = -1 Then Exit While
Dim TemplateText As String = Data.Text.Substring(Open, Close - Open + 4)
Dim TemplateItem As New TemplateData(TemplateText)
Data.Templates.Add(TemplateItem)
ParseTemplate(TemplateItem)
Data.Text = Data.Text.Substring(0, Open) & Convert.ToChar(14) & Data.Text.Substring(Close + 4)
Close = Open + 1
End While
Data.Text = Data.Text.Substring(4, Data.Text.Length - 8)
Dim Params As String() = Data.Text.Split("|"c)
If Params(0).Contains(":") AndAlso (Params(0).Contains("#") OrElse Array.IndexOf(MagicWords, _
Params(0).Substring(0, Params(0).IndexOf(":"))) > -1) Then
Params(0) = "{\cf3\b " & Params(0).Replace(":", "\cf0 :") & "}"
ElseIf Array.IndexOf(MagicWords, Params(0)) > -1 Then
Params(0) = "{\cf3\b " & Params(0) & "}"
ElseIf Params(0).Contains("subst:") Then
Params(0) = Params(0).Replace("subst:", "{\b{\cf0 subst}:{\cf4 ") & "}}"
Dim ValueIndex As Integer = Params(0).IndexOf("{\cf4 ")
Dim Value As String = Params(0).Substring(Params(0).IndexOf("{\cf4 "))
Dim Name As String = Value.Substring(5, Value.Length - 7).Replace(Convert.ToChar(3), "").Trim(" "c)
If Value.Contains("#") AndAlso Value.Contains(":") Then
Dim Index As Integer = Value.IndexOf(":")
Value = "{\cf3 " & Value.Substring(6)
Value = Value.Substring(0, Index) & "}" & Value.Substring(Index, Value.Length - Index - 1)
Params(0) = Params(0).Substring(0, ValueIndex) & Value
ElseIf Array.IndexOf(MagicWords, Name) > -1 Then
Value = "{\cf3 " & Value.Substring(6)
Params(0) = Params(0).Substring(0, ValueIndex) & Value
End If
Else
Params(0) = "{\cf5\b " & Params(0) & "}"
End If
For i As Integer = 1 To Params.Length - 1
Dim Index2 As Integer = Params(i).IndexOf("=")
If Index2 > -1 Then Params(i) = "{\cf7 " & Params(i).Substring(0, Index2) & "}" _
& Params(i).Substring(Index2)
Next i
Data.Text = String.Join("{\b |}", Params)
Data.Text = "{\b0\cf0 \{\{" & Data.Text & "\}\}}"
End Sub
Private Sub UnpackTemplates(ByVal Data As TemplateData)
For i As Integer = 0 To Data.Templates.Count - 1
UnpackTemplates(Data.Templates(i))
Dim Index As Integer = Data.Text.IndexOf(Convert.ToChar(14))
If Index > -1 Then Data.Text = Data.Text.Substring(0, Index) _
& Data.Templates(i).Text & Data.Text.Substring(Index + 1)
Next i
End Sub
End Class
End Module