-
Notifications
You must be signed in to change notification settings - Fork 93
/
html2h.vbs
172 lines (124 loc) · 5.09 KB
/
html2h.vbs
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
' html2h translats .html file into C .h file
'
' Bojan Jurca, 12.7.2023
' check arguments
If Wscript.Arguments.Count <> 2 Then
Wscript.Echo "Synatx error. Use: 'html2h.vbs orgFile.html newFile.h'"
WScript.Quit
End if
' both arguments exist, check if file arg 1 exists by opening it
Const ForReading = 1, ForWriting = 2 ', ForAppending = 8
inputFileName = Wscript.Arguments (0)
if InStr (inputFileName, ".") = 0 Then inputFileName = inputFileName & ".html" ' assume .html if not specified
Set fso = CreateObject ("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile (inputFileName, ForReading) ' the script will end with an error if the file does not exist
' check if file arg 2 already exists
outputFileName = Wscript.Arguments (1)
if InStr (outputFileName, ".") = 0 Then outputFileName = outputFileName & ".h" ' assume .h if not specified
If fso.FileExists (outputFileName) Then
If MsgBox (outputFileName & " already exists." & vbCrLf & vbCrLf & "Overwrite?", VbYesNo + vbQuestion, "File already exists") <> vbYes Then
WScript.Quit
End If
End If
If Not fso.FileExists (outputFileName) Then
Set outputFile = fso.CreateTextFile (outputFileName, True)
Else
Set outputFile = fso.OpenTextFile (outputFileName, ForWriting) ' the script will end with an error if the file can not be created/opened
End If
outputFile.Write "// this file has been automatically generated from " & inputFileName & " with html2h.vbs" & vbCrLf & vbCrLf
outputFile.Write "#define " & Left (outputFileName, InStr (outputFileName, ".") - 1) & " """" \" & vbCrLf
outputFile.Write " """" \" & vbCrLf
' process input file line by line
Do Until inputFile.AtEndOfStream
'wscript.echo StrData
inputLine = inputFile.ReadLine
RTrim inputLine
processLine inputLine, outputFile, 0
Loop
outputFile.Write " """"" & vbCrLf
outputFile.Close
inputFile.Close
Wscript.Echo outputFileName & " created."
Sub processLine (inputLine, outputFile, recursionLevel)
' starting "
If recursionLevel = 0 Then
outputFile.Write " """
End If
' a line with reference to picture file? This checking is not prerfectly OK since .png string can also be just a string inside .html file but it will do for now
p = InStr (inputLine, ".png")
g = InStr (inputLine, ".gif")
If g > 0 And g < p Then
p = g
End If
If p > 0 Then ' inputLine contains a reference to picture file - pictures have to be base64 encoded
' split inputLine to 3 parts: the beginning, the picture reference and the end
q = 0
For i = p To Len (inputLine)
If Mid (inputLine, i, 1) = """" Or Mid (inputLine, i, 1) = "'" Then
q = i - 1
Exit For
End If
Next
' q points to the end of picture file name, find the beginning now
If q > 0 Then
Do While p > 0
If Mid (inputLine, p, 1) = """" Or Mid (inputLine, p, 1) = "'" Then
p = p + 1
Exit Do
End If
p = p - 1
Loop
' p points to the beginning of picture file name
if p > 0 Then
' 1. recursively process the first part of inputLine
processLine Left (inputLine, p - 1), outputFile, recursionLevel + 1
' 2. add base64 encoded picture file here
processPicture Mid (inputLine, p, q - p + 1), outputFile
' 3. recursively process the last part of inputLine
processLine Mid (inputLine, q + 1), outputFile, recursionLevel + 1
End IF
End If
Else
' a normal line
For i = 1 To Len (inputLine)
Select Case Mid (inputLine, i, 1)
Case vbTab
outputFile.Write " " ' change tab to 4 spaces
Case """"
outputFile.Write "\""" ' change " to \"
Case Else
outputFile.Write Mid (inputLine, i, 1)
End Select
Next
End If
' ending \n"
If recursionLevel = 0 Then
outputFile.Write "\n"" \" & vbCrLf
End If
End Sub
Sub processPicture (pictureFileName, outputFile)
' extract only the file name
Trim pictureFileName
For i = Len (pictureFileName) to 1 Step -1
If Mid (pictureFileName, i, 1) = "/" Or Mid (pictureFileName, i, 1) = "\" Then
pictureFileName = Mid (pictureFileName, i + 1)
Exit For
End If
Next
' read the whole picture file into the buffer: https://www.motobit.com/tips/detpg_read-write-binary-files/
Const adTypeBinary = 1
Set BinaryStream = CreateObject ("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFile pictureFileName ' Load the file data from disk To stream object
buffer = BinaryStream.Read ' Open the stream And get binary data from the object
' base64 encode the buffer: https://stackoverflow.com/questions/70225780/base64-decode-xml-vba
Set objXML = CreateObject ("MSXML2.DOMDocument")
Set objNode = objXML.CreateElement ("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = buffer
base64EncodedBuffer = Replace (objNode.text, vbLf, "") ' compose the base64 encoded picture into only one line of text
Set objNode = Nothing
Set objXML = Nothing
outputFile.Write "data:image/" & Right (pictureFileName, 3) & ";base64," & base64EncodedBuffer
End Sub