Microsoft Small Basic

Program Listing: NPW862
''' SBDoc - Small Basic Document Generator
''' Version 0.2b
''' Copyright © 2016 Nonki Takahashi. The MIT License.
'
ver = "0.2b"
TextWindow.Title = "SBDoc " + ver
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
WQ = Text.GetCharacter(34)
LT = "<"
Not = "False=True;True=False;"
TextWindow.WriteLine("SBDoc - Small Basic Document Generator")
TextWindow.WriteLine("Version " + ver)
GetArgs()
If showHelp Then
Help()
Else
ofile = dest + "\index.html"
Header()
src = source
GenDoc()
If findSub Then
' The following line could be harmful and has been automatically commented.
' 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()
EndIf
Sub Footer
''' Generate footer
''' param ofile - output file
html = LT + "hr>" + CRLF
html = html + LT + "p>This document is created by SBDoc " + ver + "." + CRLF
html = html + LT + "/p>" + CRLF
html = html + LT + "/body>" + CRLF
html = html + LT + "/html>" + CRLF
' The following line could be harmful and has been automatically commented.
' File.AppendContents(ofile, html)
EndSub
Sub GetArgs
''' Get arguments
''' return source - source folder
''' return dest - destination folder
''' return findSub - "True" to search sub folders
''' return showHelp - "True" to show help
nArg = Program.ArgumentCount
showHelp = "False"
source = ""
dest = ""
findSub = "False"
For i = 1 To nArg
arg = Program.GetArgument(i)
If Text.ConvertToUpperCase(arg) = "/O" Then
If i < nArg Then
i = i + 1
dest = Program.GetArgument(i)
Else
showHelp = "True"
EndIf
ElseIf Text.ConvertToUpperCase(arg) = "/S" Then
findSub = "True"
ElseIf source = "" Then
source = arg
Else
showHelp = "True"
EndIf
EndFor
If source = "" Or dest = "" Then
showHelp = "True"
EndIf
EndSub
Sub GenDoc
''' Generate documents in a folder
''' param ofile - output filename
''' param src - source folder
' The following line could be harmful and has been automatically commented.
' 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
TextWindow.WriteLine(ifile)
html = LT + "h1>" + ifile + LT + "/h1>" + CRLF
' The following line could be harmful and has been automatically commented.
' buf = File.ReadContents(ifile)
p = 1
File_ReadLine()
While Not[eof]
IsDocument()
If match Then
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine(doc)
html = html + doc + LT + "br>" + CRLF
TextWindow.ForegroundColor = "Gray"
Else
IsSub()
If match Then
TextWindow.WriteLine(line)
html = html + LT + "h2>" + line + LT + "/h2>" + CRLF
EndIf
EndIf
File_ReadLine()
EndWhile
' The following line could be harmful and has been automatically commented.
' 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
' The following line could be harmful and has been automatically commented.
' File.WriteContents(ofile, html)
EndSub
Sub Help
''' Show help
TextWindow.WriteLine("Genarates Small Basic Document as HTML files." + CRLF)
TextWindow.WriteLine("SBDoc [/S] source /O dest" + CRLF)
TextWindow.WriteLine(" /S Searches sub folders.")
TextWindow.WriteLine(" source Source folder.")
TextWindow.WriteLine(" /O dest Destination folder." + CRLF)
EndSub
Sub IsSub
''' Check wheter the line is a subroutine
match = "False"
If Text.StartsWith(Text.ConvertToLowerCase(line), "sub ") Then
match = "True"
EndIf
EndSub
Sub IsDocument
''' Check whether the line is a document
''' return doc
match = "False"
_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 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