-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlPreprocessor.pb
More file actions
111 lines (72 loc) · 2.13 KB
/
HtmlPreprocessor.pb
File metadata and controls
111 lines (72 loc) · 2.13 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
EnableExplicit
#AppName = "HtmlPreprocessor"
Structure sSearchReplaceItem
search.s
replace.s
EndStructure
NewList SearchReplaceItems.sSearchReplaceItem()
Define File.s, TempFile.s, Executable.s
Define FF
Define HtmlContent.s
Define P1, P2
Define SbContent.s
Define JsonString.s
Define Json
File = ProgramParameter(0) ; -> SB-File (if saved)
TempFile = ProgramParameter(1) ; -> SB-File (if not saved)
Executable = ProgramParameter(2) ; -> HTML-File
If File = ""
File = TempFile
EndIf
; read sb-file:
FF = ReadFile(#PB_Any, File)
If FF = 0
MessageRequester(#AppName, "Couldn't load file '" + File + "'")
End
EndIf
SbContent = ReadString(FF, #PB_File_IgnoreEOL)
CloseFile(FF)
; search for inject-instructions:
P1 = FindString(SbContent, "<HtmlPreprocessor>", 1, #PB_String_NoCase)
P2 = FindString(SbContent, "</HtmlPreprocessor>", 1, #PB_String_NoCase)
If P1 = 0 Or P2 = 0 ; no HtmlPreprocessor-Tags found
End ; Cheerio, nothing to do...
EndIf
P1 + Len("<HtmlPreprocessor>")
JsonString = Mid(SbContent, P1, P2 - P1)
JsonString = RemoveString(JsonString, ";!")
JsonString = RemoveString(JsonString, #CRLF$)
JsonString = ReplaceString(JsonString, "#CRLF#", "\n")
; MessageRequester(#AppName, JsonString)
Json = ParseJSON(#PB_Any, JsonString)
If Json = 0
MessageRequester(#AppName, "Json-Error in HtmlPreprocessor - Block:" + #CRLF$ + JSONErrorMessage())
End
EndIf
ExtractJSONList(JSONValue(Json), SearchReplaceItems())
If ListSize(SearchReplaceItems()) = 0
End ; Cheerio, nothing to do...
EndIf
; now read the Html-File
FF = ReadFile(#PB_Any, Executable)
If FF = 0
MessageRequester(#AppName, "Couldn't load file '" + Executable + "'")
End
EndIf
HtmlContent.s = ReadString(FF, #PB_File_IgnoreEOL)
CloseFile(FF)
; now Search & Replace
ForEach SearchReplaceItems()
If SearchReplaceItems()\Search
HtmlContent = ReplaceString(HtmlContent, SearchReplaceItems()\Search, SearchReplaceItems()\Replace)
EndIf
Next
; and fnially save HTML
FF = CreateFile(#PB_Any, Executable)
If FF = 0
MessageRequester(#AppName, "Couldn't save file '" + Executable + "'")
End
EndIf
WriteString(FF, HtmlContent)
CloseFile(FF)
; over and out