MaxGUI: Create a HTML page on the fly?

BlitzMax Forums/BlitzMax Programming/MaxGUI: Create a HTML page on the fly?

Something quick to get you going ..

This will let you write text in different colors as well as BOLD , ITALICS, UNDERLINE

The example shows a simple HTML-generated file.
After clicking the NOTIFY button a modified version is shown.

I have used Blitz forum codes for text formatting.
See the ParseLine() function and example text.

The basics:
htm:HTML_Type

Methods/Functions
------------------------------------------------------------
htm.Clear               - clears the text (and creates a header)
htm.AddText t$          - adds an entry
htm.AddLink url$,desc$  - adds a link to url/file etc..
htm.Change f$,r$        - searches for f$ and replaces with r$
htm.Show htmviewgadget  - show results into a view gadget
htm.Cleanup             - removes temporary htm file and text



The source:
' Basic HTML source generator
' Jim Brown
' v0.01


Type HTML_Type
	Global htmlsource:TList=New TList
	Global templist:TList=New TList
	' add text entry
	Method New()
		Clear
	End Method
	' add a line if text
	Method AddText(t$,LineBreak%=True)
		Local lb$=""
		If LineBreak lb$="<br>"
		htmlsource.AddLast t$+lb$
	End Method
	' add a link to a url/file etc ..
	Method AddLink(url$,desc$,lb%=False)
		AddText "<a href=~q" + url$ + "~q>" + desc$ + "</a>" , lb
	End Method
	' create top header file for results.htm file
	Method Clear()
		htmlsource.Clear
		AddText "<html>",False
		AddText "<head>",False
		AddText "<title>HTML Generated Text</title>",False
		AddText "</head>",False
		AddText "<body bgcolor=~q#FFFFFF~q>",False
	End Method
	' search for and change a text entry
	Method Change(f$,r$)
		For Local l$=EachIn htmlsource
			l$=l.Replace(f$,r$)
			templist.AddLast l$
		Next
		htmlsource.Clear
		SwapLists htmlsource,templist
		templist.clear
	End Method
	' convert tags to HTML compatible tags
	Function ParseLine:String(a:String)
		a$=a.Replace("<i>","<em>")				' ITALICS ON
		a$=a.Replace("</i>","</em>")				' ITALICS OFF
		a$=a.Replace("<b>","<strong>")			' BOLD ON
		a$=a.Replace("</b>","</strong>")		' BOLD OFF
		a$=a.Replace("<u>","<u>")				' UNDERLINE ON
		a$=a.Replace("</u>","</u>")				' UNDERLINE OFF
		a$=a.Replace("[rgb]","<font color=~q#")	' COLOR ON
		a$=a.Replace("[/rgb]","~q>")				' COLOR OFF
		a$=a.Replace("[bar]","<hr>")				' HORIZONAL BAR
		't$=t.Replace("~q" , Chr$(34))
		Return a$
	End Function
	' show the resulting text into a HTML view gadget
	Method Show(htmviewgadget:TGadget)
		AddText "</body>",False
		AddText "</html>",False
		Local file:TStream=WriteFile("temp.htm")
		If file
			' write the file
			For Local t$=EachIn htmlsource
				WriteLine file,ParseLine(t$)
			Next
			CloseFile file
			' open the file for viewing
			Local htmfile$=AppDir$+"/temp.htm"
			htmfile$=htmfile.Replace("//","/")
			HtmlViewGo htmviewgadget,htmfile$
		Else
			Notify "HTML file error."
		EndIf
	End Method
	' remove text and temporary file
	Method Cleanup()
		htmlsource.Clear
		If FileSize("temp.htm")>-1 DeleteFile "temp.htm"
	End Method
End Type


' *********************************************************

' basic GUI frontend for viewing generated page

Global htmlview:TGadget
Const winW%=660 , WinH%=460
Const winflags%=WINDOW_RESIZABLE|WINDOW_TITLEBAR|WINDOW_STATUS
Global win:TGadget = CreateWindow("HTML Example",180,70,winW,winH,Null,winflags)
htmlview=CreateHTMLView(8,40,ClientWidth(win)-16,ClientHeight(win)-48,win)
SetGadgetLayout htmlview,1,1,1,1

' *********************************************************


' create a simple HTML test page
' Note: newly created pages are automatically
' initiallised with a header

Global htm:HTML_Type=New HTML_Type

htm.AddText "Hello World!"
htm.AddText "[rgb]8080ff[/rgb]This text should be in blue."
htm.AddText "[rgb]ee0000[/rgb]This line color will change shortly!!"
htm.AddText "[rgb]000000[/rgb]Black (default)"
htm.AddText "Example of <i>Italics</i> and <b>bold</b> too."
htm.AddText "[bar]"
htm.AddText "After clicking the NOTIFY button the RED text should change"
htm.AddText "to GREEN and the new results displayed."
' show the generated page into the htmlview gadget
htm.Show htmlview

Notify "Click me after reading the HTML code"

' search for and change the RED to GREEN
htm.Change "[rgb]ee0000[/rgb]","[rgb]00ff00[/rgb]"
htm.Change "will change shortly","has changed"
' show new results
htm.Show htmlview

Notify "Finally, how to clear the page and start again .."

htm.Clear
htm.AddText "Game over .. "
htm.AddText "Or, you might want to vist the ",False ' (False=suspend line break)
htm.AddLink "http://www.blitzmax.com","BlitzMax"
htm.AddText "site!!"
htm.Show htmlview



' *********************************************************

Repeat
	Local event%=WaitEvent()
	DebugLog CurrentEvent.ToString()
	Select event
		' closed via [X] or other shutdown method
		Case EVENT_WINDOWCLOSE , EVENT_APPTERMINATE
		Exit
	End Select
Forever

htm.Cleanup
End