. There is an export to HTML.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Syntax hilightning dll example
;
; (c) 2010 - Fantaisie Software
;
; ------------------------------------------------------------
;
; The SyntaxHilighting.dll provides the syntax parser of the
; PureBasic IDE in form of a dll, so it can be easily reused
; to do other tasks as well.
;
; Some notes:
;
; - For speed reasons, the dll is not threadsave.
;
; - The hilighter does not handle unicode. It does however handle UTF8, so if
; Unicode text should be parsed, convert it to UTF8 first.
;
; The dll exports only one function:
;
; SyntaxHighlight(*Buffer, Length, @Callback(), EnableAsm)
;
; *Buffer and Length specify the text buffer to parse.
;
; Callback() must have the parameters as in the example below and will be called
; for each parsed token.
;
; If EnableAsm is set to nonzero, the parser will report asm keywords also outside of
; the special ! lines, just as the InlineAsm parser does.
;
; Color values returned in the Dll callback
;
Enumeration
#SYNTAX_Text
#SYNTAX_Keyword
#SYNTAX_Comment
#SYNTAX_Constant
#SYNTAX_String
#SYNTAX_Function
#SYNTAX_Asm
#SYNTAX_Operator
#SYNTAX_Structure
#SYNTAX_Number
#SYNTAX_Pointer
#SYNTAX_Separator
#SYNTAX_Label
#SYNTAX_Module
EndEnumeration
#Dll = 0
#Input = 0
#Output = 1
; Callback that is called from the dll.
;
; NOTE: For performance reasons, whitespace characters (space, tab, newline)
; are returned together with the tokens they surround to reduce the number
; of required callback calls. If this is not desired, you must separate them
; here in the callback manually.
;
; The original buffer is not modified. The *Position parameter points to the
; current position in the original buffer.
;
;
Procedure Callback(*Position, Length, Color)
; In this example, we simply write the data as it is to the output
; buffer, and just apply bold to keywords, and colors to functions and comments.
;
Select Color
Case #SYNTAX_Comment
WriteString(#Output, "<span class='S2'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Number
WriteString(#Output, "<span class='S3'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Function
WriteString(#Output, "<span class='S4'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Constant
WriteString(#Output, "<span class='S5'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Keyword
WriteString(#Output, "<span class='S6'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Operator
WriteString(#Output, "<span class='S9'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_String
WriteString(#Output, "<span class='S16'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Asm
WriteString(#Output, "<span class='S15'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Text
WriteString(#Output, "<span class='S7'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Structure
WriteString(#Output, "<span class='S8'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Pointer
WriteString(#Output, "<span class='S10'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Separator
WriteString(#Output, "<span class='S11'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Label
WriteString(#Output, "<span class='S14'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Case #SYNTAX_Module
WriteString(#Output, "<span class='S13'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
Default
WriteString(#Output, "<span class='S0'>")
WriteData(#Output, *Position, Length)
WriteString(#Output, "</span>")
EndSelect
EndProcedure
; Simple example code. It loads a PB file and outputs a HTML file withs some
; coloring for functions, keywords and comments
;
If OpenLibrary(#Dll, #PB_Compiler_Home + "SDK\Syntax Highlighting\SyntaxHighlighting.dll")
InputFile$ = OpenFileRequester("Select PB File", "*.pb", "PB Files|*.pb|All Files|*.*", 0)
If InputFile$
OutputFile$ = SaveFileRequester("Select Target file", InputFile$+".html", "Html Files|*.html|All Files|*.*", 0)
If ReadFile(#Input, InputFile$) And CreateFile(#Output, OutputFile$)
Length = Lof(#Input)
*Buffer = AllocateMemory(Length)
If *Buffer
ReadData(#Input, *Buffer, Length)
WriteStringN(#Output, "<html><body><pre>")
CallFunction(#Dll, "SyntaxHighlight", *Buffer, Length, @Callback(), 0)
WriteStringN(#Output, "</pre></html></body>")
EndIf
CloseFile(#Input)
CloseFile(#Output)
EndIf
EndIf
CloseLibrary(#Dll)
EndIf