-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBDoc.sb
340 lines (326 loc) · 9.26 KB
/
SBDoc.sb
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
' SBDoc - Small Basic Document Generator
' Version 0.5b
' Copyright © 2016-2017 Nonki Takahashi. The MIT License.
' Last update 2017-05-21
' Program ID NPW862-0
' Repository https://github.com/nonkit/SBDoc
ver = "0.5b"
GraphicsWindow.Title = "SBDoc " + ver
Init()
ReadSettings()
Form()
While "True"
If buttonClicked Then
msg = ""
settings["src"] = Controls.GetTextBoxText(tboxSrc)
settings["out"] = Controls.GetTextBoxText(tboxOut)
dir = settings["out"]
File_DirectoryExists()
If Not[exists] Then
msg = "Folder " + dir + " created." + CRLF
File.CreateDirectory(dir)
EndIf
ofile = settings["out"] + "\index.html"
Header()
src = settings["src"]
GenDoc()
If findSub Then
folders = File.GetDirectories(src)
nFolders = Array.GetItemCount(folders)
iFolders = Array.GetAllIndices(folders)
For j = 1 To nFolders
src = folders[iFolders[j]]
GenDoc()
EndFor
EndIf
Footer()
msg = msg + "Output " + ofile + " generated." + CRLF
WriteSettings()
msg = msg + "Settings " + settingsFile + " saved."
Shapes.SetText(txtMsg, msg)
buttonClicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile
Sub Footer
' Generate footer
' param ofile - output file
yyyy = Clock.Year
mm = Clock.Month
If mm < 10 Then
mm = Text.Append(0, mm)
EndIf
dd = Clock.Day
If dd < 10 Then
dd = Text.Append(0, dd)
EndIf
html = LT + "hr>" + CRLF
html = html + LT + "p>This document is created by "
html = html + LT + "a href=" + WQ + "https://nonkit.github.io/SBDoc/"
html = html + WQ + ">SBDoc" + LT + "/a> " + ver
html = html + " on " + yyyy + "-" + mm + "-" + dd + "." + CRLF
html = html + LT + "/p>" + CRLF
html = html + LT + "/body>" + CRLF
html = html + LT + "/html>" + CRLF
File.AppendContents(ofile, html)
EndSub
Sub Form
' Draw input form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
fs = 16
GraphicsWindow.FontSize = fs
y = 10
GraphicsWindow.DrawText(10, y, "SBDoc - Small Basic Document Generator")
y = y + fs * 2
fs = 12
GraphicsWindow.FontSize = fs
GraphicsWindow.DrawText(10, y, "Version " + ver)
y = y + fs * 2
GraphicsWindow.DrawText(10, y, "Source Folder")
tboxSrc = Controls.AddTextBox(100, y - 3)
Controls.SetSize(tboxSrc, 480, 20)
y = y + fs * 2
Controls.SetTextBoxText(tboxSrc, settings["src"])
GraphicsWindow.DrawText(10, y, "Output Folder")
tboxOut = Controls.AddTextBox(100, y - 3)
Controls.SetSize(tboxOut, 480, 20)
Controls.SetTextBoxText(tboxOut, settings["out"])
y = y + fs * 2
Controls.AddButton("Run", 10, y)
Controls.ButtonClicked = OnButtonClicked
findSub = "False"
msg = "Usage:" + CRLF
msg = msg + "Input source and output folder names and push [Run] button."
txtMsg = Shapes.AddText(msg)
y = y + fs * 3
Shapes.Move(txtMsg, 10, y)
EndSub
Sub GenDoc
' Generate documents in a folder
' param ofile - output filename
' param src - source folder
files = File.GetFiles(src)
nFiles = Array.GetItemCount(files)
iFiles = Array.GetAllIndices(files)
For i = 1 To nFiles
ifile = files[iFiles[i]]
lfile = Text.ConvertToLowerCase(ifile)
If Text.EndsWith(lfile, ".sb") Or Text.EndsWith(lfile, ".smallbasic") Then
html = LT + "h1>" + ifile + LT + "/h1>" + CRLF
buf = File.ReadContents(ifile)
p = 1
File_ReadLine()
top = "True"
info = "True"
While Not[eof] And info
IsKeyword()
If match Then
If top Then
html = html + LT + "h2>Program Information" + LT + "/h2>" + CRLF
html = html + LT + "table>" + CRLF
top = "False"
EndIf
html = html + LT + "tr>" + LT + "td>"
html = html + LT + "b>
html = html + keyword[k]
html = html + LT + "/b>" + LT + "/td>"
html = html + LT + "td>"
If keyword[k] = "Repository" Then
html = html + LT + "a href=" + WQ
html = html + doc + WQ + ">"
EndIf
If keyword[k] = "Program ID" Then
html = html + LT + "a href=" + WQ
html = html + "http://smallbasic.com/program/?" + doc + WQ + ">"
EndIf
html = html + doc
If keyword[k] = "Last update" Or keyword[k] = "Program ID" Then
html = html + LT + "/a>"
EndIf
html = html + LT + "/td>" + LT + "tr>" + CRLF
Else
info = "False"
EndIf
File_ReadLine()
EndWhile
If Not[top] Then
html = html + LT + "/table>" + CRLF
EndIf
html = html + LT + "h2>Subroutines" + LT + "/h2>" + CRLF
top = "True"
While Not[eof]
IsDocument()
If match Then
html = html + doc + LT + "br>" + CRLF
Else
IsSub()
If match Then
If top Then
top = "False"
Else
html = html + LT + "hr>" + CRLF
EndIf
html = html + LT + "img src=" + WQ + "img/IntellisenseSubroutine.png"
html = html + WQ + " width=" + WQ + "64" + WQ + ">"
html = html + LT + "h3> " + name + "()" + LT + "/h3>" + CRLF
EndIf
EndIf
File_ReadLine()
EndWhile
File.AppendContents(ofile, html)
EndIf
EndFor
EndSub
Sub Header
' Generate header
' param ofile - output file
html = LT + "!DOCTYPE html>" + CRLF
html = html + LT + "html>" + CRLF
html = html + LT + "head>" + CRLF
html = html + LT + "meta charset=" + WQ + "utf-8" + WQ + ">" + CRLF
html = html + LT + "link rel=" + WQ + "stylesheet" + WQ
html = html + " href=" + WQ + "style.css" + WQ + ">" + CRLF
html = html + LT + "/head>" + CRLF
html = html + LT + "body>" + CRLF
result = File.WriteContents(ofile, html)
EndSub
Sub Init
' Initialize varibles
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
WQ = Text.GetCharacter(34)
LT = "<"
Not = "False=True;True=False;"
keyword = "1=Title;2=Version;3=Copyright;4=Repository;5=Challenge;"
keyword = keyword + "6=Program ID;7=Last update;8=param;9=return;"
EndSub
Sub IsDocument
' Check whether the line is a document
' return doc
match = "False"
doc = ""
_p = 1
While Text.GetSubText(line, _p, 1) = " "
_p = _p + 1
EndWhile
cmark = "'"
If Text.StartsWith(Text.GetSubTextToEnd(line, _p), cmark) Then
_p = _p + Text.GetLength(cmark)
While Text.GetSubText(line, _p, 1) = "'"
_p = _p + 1
EndWhile
While Text.GetSubText(line, _p, 1) = " "
_p = _p + 1
EndWhile
doc = Text.GetSubTextToEnd(line, _p)
match = "True"
EndIf
EndSub
Sub IsKeyword
' Check whether the line starts with a keyword
' return k - index of the keyword
' return doc
' return match - "True" if keyword
match = "False"
doc = ""
_p = 1
While Text.GetSubText(line, _p, 1) = " "
_p = _p + 1
EndWhile
cmark = "'"
If Text.StartsWith(Text.GetSubTextToEnd(line, _p), cmark) Then
_p = _p + Text.GetLength(cmark)
While Text.GetSubText(line, _p, 1) = "'"
_p = _p + 1
EndWhile
While Text.GetSubText(line, _p, 1) = " "
_p = _p + 1
EndWhile
ldoc = Text.ConvertToLowerCase(Text.GetSubTextToEnd(line, _p))
_n = Array.GetItemCount(keyword)
For _k = 1 To _n
lkw = Text.ConvertToLowerCase(keyword[_k])
If Text.StartsWith(ldoc, lkw) Then
_p = _p + Text.GetLength(lkw)
While Text.GetSubText(line, _p, 1) = " "
_p = _p + 1
EndWhile
k = _k
_k = _n
doc = Text.GetSubTextToEnd(line, _p)
match = "True"
EndIf
EndFor
If Not[match] And top Then
k = 1 ' Title
doc = Text.GetSubTextToEnd(line, _p)
match = "True"
EndIf
EndIf
EndSub
Sub IsSub
' Check whether the line is a subroutine
' return name - subroutine name
' return match - "True" if subroutine
match = "False"
If Text.StartsWith(Text.ConvertToLowerCase(line), "sub ") Then
name = Text.GetSubTextToEnd(line, 5)
match = "True"
EndIf
EndSub
Sub OnButtonClicked
' Button event handler
buttonClicked = "True"
EndSub
Sub ReadSettings
' Read settings
' return settingsFile - settings file path
' return settings - array for settings
settingsFile = File.GetSettingsFilePath()
settings = File.ReadContents(settingsFile)
If settings["src"] = "" Then
settings["src"] = Program.Directory
EndIf
If settings["out"] = "" Then
settings["out"] = Program.Directory + "\out"
EndIf
EndSub
Sub WriteSettings
' Write settings
' param settingsFile - settings file path
' param settings - array for settings
File.WriteContents(settingsFile, settings)
EndSub
Sub File_DirectoryExists
' File | check if the directory exists
' param dir
' return exists
files = File.GetFiles(dir)
If files = "FAILED" Then
exists = "False"
Else
exists = "True"
EndIf
EndSub
Sub File_ReadLine
' File | read line from buf
' param buf - buffer
' param p - pointer to buffer
' param eof - "True" if end of file
' return line - line
' return p - updated pointer to buffer
nl = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), CRLF)
If 0 < nl Then
line = Text.GetSubText(buf, p, nl - 1)
p = p + nl + 1
eof = "False"
Else
line = Text.GetSubTextToEnd(buf, p)
eof = "True"
EndIf
EndSub