HTML

Blitz3D Forums/Blitz3D Programming/HTML

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

A HTML Parser is often requested for B3D, Its interesting that one does not exist in the code archives. However, I have always wanted to write one. Perhaps we can start one here. I'm going to write code and add to this specific post.

In parsing HTML I would read the file byte-by-byte. This makes it simple to parse the html text serially and identify html tags, attributes, special characters, etc. Next we should determine how to handle HTML tags and attributes. Heres some conditions to help:
1. HTML tag keywords are enclosed in < >
2. Formatted Text is enclosed between a HTML start tag (ie:<body>) and stop tag (ie</body>)
3. HTML attributes are followed by an equal sign (ie:<body bgcolor='#FFFFFF">)
4. HTML attribute values are optionally enclosed between single qoute or double quote.

With these conditions, we can create a flow for parsing the text. Finally, we could render the formated text and images to a ImageBuffer for display.

;HTML Parser
Type htmlpage
	Field id%						;page number% (reserved)
	Field typeid%						;format: 1=html
	Field filename$						;filename$						
	Field file%						;page file handle%
	Field byte%						;stores byte read from file
	Field buffer%						;page image buffer
	Field tagflag%						;0=text, 1=htmltag, 2=htmlattribute
	Field x%						;stores page imagebuffer x format coord
	Field y%						;stores page imagebuffer y format coord
End Type

Function htmlpageNew.htmlpage(filename$,typeid%=1)		;creates a new html page object
	this.htmlpage=New htmlpage
	this\id%=0
 	this\typeid%=typeid%
	this\filename$=filename$
	Return this
End Function

Function htmlpageRead(this.htmlpage)				;main html page read function filename$="index.html"
	this\file%=ReadFile(this\filename$)			;file handle
	If this\file%						;if no file handle then runtime error message
		While Not Eof(this\file%)			;parse until end of file
			 htmlpageParse(this\file%)		;html parse
		Wend						
		CloseFile(this\file%)				;close file after parsing
	Else
		RuntimeError(this\filename$+" does not exist!")	;if no file handle then runtime error message
	EndIf							;ditto
End Function

Function htmlpageParse(this.htmlpage)
	Select this\tagflag%					;select html text type
		Case 0;text
			this\byte%=ReadByte(file%)		;read html file byte-by-byte
			Select this\byte%
				Case 60;<
 					this\tagflag%=1
				default
					htmlpageFormatDraw(this)
			End Select
		Case 1;html keyword
			word$=htmlpageWordRead$(file%)
			Select 
				Case
			end Select 
		Case 2;html attribute
		Case 3;html attribute value
	End Select	
End Function

Function htmlpageWordRead$(file%)
	While Not Eof(file)
		byte%=ReadByte(file%)
		Select byte%
			Case 32, ;terminate characters: space
			Default
				word$=word$+Chr(byte%)
		End Select	
	Wend
End Function

Function htmlFormatDraw(this.htmlpage)
End Function