It can be made more flexible, but this is how I use it for what I need to right now.
I haven't expanded on it too much, but it is pretty easy to do so and I'm sure I'll continue to add to it as I use more features.
*note: this does not handle the beginning or ending <html>, <body> or other tags or things like CSS (although it does support class within the td tags). this is for a simple table ONLY.
*note2: due to this arraysize bug with PB 4.5b2, the output will not be complete
Code: Select all
Enumeration
#FT_Align_Right
#FT_Align_Center
#FT_Align_Left
EndEnumeration
Structure FormattedText
text.s
bgColor.s
fontColor.s
fontSize.i
font.s
align.i ;left/right/center
EndStructure
Procedure.s HTMLcomment(txt.s)
;returns text commented out the HTML way
ProcedureReturn "<!-- " + txt + " -->"
EndProcedure
Procedure HTML_PrintTable(Array arr.FormattedText(2), nFile.i, nWidth.i = 35, class.s="", firstRowIsHdr.i = 0)
Protected.i nr, nc, Padding=1
Protected line.s
If Not nFile : ProcedureReturn : EndIf
line = "<table"
If class
line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
EndIf
line = line + " border=1 width=" + Str(nWidth) + "%>"
WriteStringN(nFile,line)
line = ""
WriteStringN(nFile,HTMLcomment("Start loop for rows"))
For nr = 0 To ArraySize(arr(),2)
If Not firstRowIsHdr
WriteStringN(nFile,"<tr>")
Else
WriteStringN(nFile,"<th>")
EndIf
WriteStringN(nFile,HTMLcomment("loop for column cells"))
For nc = 0 To ArraySize(arr(),1)
line = "<td padding=" + Str(Padding)
If class
line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
EndIf
If arr(nc,nr)\bgColor
line = line + " bgcolor=" + #DQUOTE$ + arr(nc,nr)\bgColor + #DQUOTE$
EndIf
Select arr(nc,nr)\align
Case #FT_Align_Center
line = line + " align=" + #DQUOTE$ + "center" + #DQUOTE$
Case #FT_Align_Left
line = line + " align=" + #DQUOTE$ + "left" + #DQUOTE$
Case #FT_Align_Right
line = line + " align=" + #DQUOTE$ + "right" + #DQUOTE$
Default
EndSelect
line = line + ">"
If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
line = line + "<font"
If arr(nc,nr)\font
line = line + " " + arr(nc,nr)\font
EndIf
If arr(nc,nr)\fontSize
line = line + " size=" + Str(arr(nc,nr)\fontSize)
EndIf
If arr(nc,nr)\fontColor
line = line + " color=" + arr(nc,nr)\fontColor
EndIf
line = line + ">"
EndIf
line = line + arr(nc,nr)\text
If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
line = line + "</font>"
EndIf
line = line + "</td>"
WriteStringN(nFile,line)
line = ""
Next nc
If Not firstRowIsHdr
WriteStringN(nFile,"</tr>")
Else
WriteStringN(nFile,"</th>")
EndIf
Next nr
WriteStringN(nFile,"</table>")
EndProcedure
Dim html.FormattedText(9,9)
For c.i = 0 To 9
For r.i = 0 To 9
With html(c,r)
\text = "row: " + Str(r) + ", col: " + Str(c)
\align = #FT_Align_Center
\fontColor = "blue"
EndWith
Next r
Next c
fname.s = GetTemporaryDirectory() + "html_output.htm"
fnum.i = CreateFile(#PB_Any, fname)
If fnum
WriteStringN(fnum,"<html><title>test</title><body>")
HTML_PrintTable(html(),fnum)
WriteStringN(fnum,"</body></html>")
CloseFile(fnum)
RunProgram("notepad.exe",fname,GetTemporaryDirectory())
EndIf