;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       Kix2Htm() 
;; 
;;ACTION         Formats a Kixtart Function as a colorized HTML document for web presentation. 
;; 
;;AUTHOR         Glenn Barnas 
;;		 Rewrite of original postprep code by Jochen Polster & Jooel Nieminen 
;; 
;;VERSION        1.2  - 2008/07/16 
;; 
;;HISTORY        1.0  - 2006/12/15 - Initial Release 
;;               1.1  - 2008/01/02 - "B_Table" is now used for text attributes, 
;;                                   renamed to "A_Table" 
;;               1.2  - 2008/07/16 - Will insert a global var $NOTICE at the top, 
;;                                   which should be an HTML comment 
;; 
;;SYNTAX         Kix2HTM(SrcTxt, C_Table, A_Table, F_Table, C_Table, Border, Body, LLW) 
;; 
;;PARAMETERS     ALL PARAMETERS ARE REQUIRED! 
;; 		 SrcTxt	  String  - all text from source file 
;;		 C_Table  Array   - color values for each text class 
;;		 A_Table  Array   - 0 - no attribute, 1 - Bold, 2 - Italic, 3 - BoldItalic 
;;		 funcs 	  Array   - List of Kix Functions 
;;		 cmds     Array   - List of Kix Commands 
;;		 Border   String  - HTML markup to define the Border / background 
;;		 Body	  String  - HTML markup to define the body style 
;;		 LLW      Number  - column # if Long Line Wrapping is enabled 
;; 
;;REMARKS        Text Classes (for B_Table and C_Table) are 
;;		  'Comments', 'Strings', 'Numbers', 'Commands', 'Functions', 'Macros',  
;;		  'Operators', & 'Variables' 
;; 
;;		 This UDF requires significant preparation prior to calling! 
;; 
;;RETURNS        String - source text formatted as HTML 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES       See the documentation! 
;; 
Function Kix2HTM($SrcTxt, $C_Table, $A_Table, $Fn_Table, $Cm_Table, $_Border, $_Body, $LLW)
 
  Dim $1,$2,$3,$4,$5				; special tag characters 
  Dim $I					; Index var 
  Dim $LineCt					; count of lines in source text 
  Dim $Lp					; LinePointer - index of SrcTxt array 
  Dim $Line					; current working line text 
  Dim $Ll					; Current working line length 
  Dim $Cp					; character position in current working line 
  Dim $Wp					; Wrap pointer 
  Dim $OBuf					; output buffer 
  Dim $TBuf					; Temp buffer for checking words 
  Dim $T1, $T2, $N				; list of terminating chars, numbers 
  Dim $C					; current character 
  Dim $Indent					; Long-Line Breaker indent value 
  Dim $Spaces					; string of 60 spaces 
  Dim $OutBuffer				; Output Buffer for the function 
  Dim $Fs_Co,$Fe_Co				; Format string for Comments 
  Dim $Fs_St,$Fe_St				; Format string for Strings 
  Dim $Fs_No,$Fe_No				; Format string for Numbers 
  Dim $Fs_CM,$Fe_CM				; Format string for Commands 
  Dim $Fs_Fn,$Fe_Fn				; Format string for Functions 
  Dim $Fs_Ma,$Fe_Ma				; Format string for Macros 
  Dim $Fs_Op,$Fe_Op				; Format string for Operators 
  Dim $Fs_Va,$Fe_Va				; Format string for Variables 
 
  ; special character definitions 
  $1 = chr(1) $2 = chr(2) $3 = chr(3) $4 = chr(4) $5 = chr(5)
  For $I = 1 to 60
    $Spaces = $Spaces + Chr(32)
  Next
 
  ; Define special characters and number strings 
  ; $n defines numeric characters 
  ; $m defines characters not allowed in a variable name (denoting the end of a var!) 
  $T1 = "-+,.'?&=@()[]" + Chr(32) + Chr(34) + Chr(36) + Chr(9) + $1 + $2 + $3
  $T2 = "-+,'?&=@()[]" + Chr(32) + Chr(34) + Chr(36) + Chr(9) + $1 + $2 + $3
  $N  = "0123456789"
 
  ; Define the ATTRIBUTE tags for each element in the table 
  ; This is an array of 2-element arrays, containing the opening and closing tags for various format attributes 
  ; The attribute code (0-3) is replaced by the appropriate attribute string containing the HTM tags 
  For $I = 0 to UBound($A_Table)
    Select
     Case $A_Table[$I] = 1	; Bold 
      $A_Table[$I] = '<b>','</b>'
     Case $A_Table[$I] = 2	; Italic 
      $A_Table[$I] = '<i>','</i>'
     Case $A_Table[$I] = 3	; Bold-Italic 
      $A_Table[$I] = '<b><i>','</i></b>'
     Case 1			; Normal (default) 
      $A_Table[$I] = '',''
    EndSelect
  Next
 
  ; Format definition string for COMMENTS - k/ke 
  $Fs_Co = '<font color="' + $C_Table[0] + '">' + $A_Table[0][0]
  $Fe_Co = ' ' + $A_Table[0][1] + '</font>'
 
  ; Font definition string for STRINGS - t/te 
  $Fs_St = '<font color="' + $C_Table[1] + '">' + $A_Table[1][0]
  $Fe_St = $A_Table[1][1] + '</font>'
 
  ; Font definition string for NUMBERS - u/ue 
  $Fs_No = '<font color="' + $C_Table[2] + '">' + $A_Table[2][0]
  $Fe_No = $A_Table[2][1] + '</font>'
 
  ; Font definition string for COMMANDS - q/qe 
  $Fs_Cm = '<font color="' + $C_Table[3] + '">' + $A_Table[3][0]
  $Fe_Cm = $A_Table[3][1] + '</font>'
 
  ; Font definition string for FUNCTIONS - r/re 
  $Fs_Fn = '<font color="' + $C_Table[4] + '">' + $A_Table[4][0]
  $Fe_Fn = $A_Table[4][1] + '</font>'
 
  ; Font definition string for MACROS - p/pe 
  $Fs_Ma = '<font color="' + $C_Table[5] + '">' + $A_Table[5][0]
  $Fe_Ma = $A_Table[5][1] + '</font>'
 
  ; Font definition string for OPERATORS - o/oe 
  $Fs_Op = '<font color="' + $C_Table[6] + '">' + $A_Table[6][0]
  $Fe_Op = $A_Table[6][1] + "</font>"
 
  ; Font definition string for VARIABLES - v/ve 
  $Fs_Va = '<font color="' + $C_Table[7] + '">' + $A_Table[7][0]
  $Fe_Va = $A_Table[7][1] + '</font>'
 
  ; Init the output buffer - wrapping the content in a table preserves the original post format 
  $OutBuffer = $NOTICE + '<table width="100%">' + '<tr>' + '<td>' + @CRLF
  $OutBuffer = $OutBuffer + '<fieldset style="' + $_Border + '">' + @CRLF
  $OutBuffer = $OutBuffer + '<pre>' + $_Body + @CRLF
 
 
  ; $SrcTxt contains all file text as a string.  
  ; Translate "&" to Chr(1) 
  ; Translate "<" to Chr(2) 
  ; Translate ">" to Chr(3) 
  ; Translate "" to Chr(4) 
  ; Translate "" to Chr(5) 
  ; Split into array of Lines 
  ; Hiding the replaced chars prevents treating them during the HTML processing 
  $SrcTxt = Split(Join(Split(Join(Split(Join(Split(Join(Split(Join(Split($SrcTxt,'&'),$1),'<'),$2),'>'),$3),''),$4),''),$5),@CRLF)
  ; determine the number of lines in the fileset data 
  $LineCt = UBound($SrcTxt)
 
  For $Lp = 0 to $LineCt			; process each line 
    $Line = $SrcTxt[$Lp]			; the working line 
    If $Line = '' $Line = ' ' EndIf		; Preserve blank lines 
    $Ll = len($Line)				; $Ll is the working line length 
    $Cp = 0 $Wp = 0				; initialize the pointers 
    $OBuf = '' $TBuf = ''			; Clear the buffers 
    Do
      $Cp = $Cp + 1 $Wp = $Wp + 1
      $C = SubStr($Line, $Cp ,1)		; get a character from the working line 
      $Tbuf = ""				; Clear the TEMP buffer 
 
      Select
 
	;====================================================================== 
        ; This process wraps formatting around DOUBLE-QUOTED STRINGS 
        Case $C = '"'				; Found a leading double-quote 
          $OBuf = $OBuf + $Fs_St + $C		; Add the STRING format code and char to Output buffer 
          Do
            If $Cp >= $Ll			; If current pointer > working line length 
              $SrcTxt[$Lp] = $OBuf		; write output string to file text array 
              $Lp = $Lp + 1			; Increment the line pointer 
              $Line = $SrcTxt[$Lp]		; get the next line 
              $OBuf = ''			; init the output buffer 
              $Cp = 0 $Wp = 0			; init the Line character pointer 
              $Ll = Len($Line)			; set the new line length 
            EndIf
            Do					; read input Line and copy to Output buffer until 
              $Cp = $Cp + 1 $Wp = $Wp + 1	; trailing quote OR end of input line is reached 
              $C = SubStr($Line, $Cp, 1)
              $OBuf = $OBuf + $C
              $Indent = WC($C, $Wp, $Line, $LLW)
              If $Indent			; Long line! Break here & indent next 
                $Wp = $Indent			; Account for the indent to setup next wrap pointer 
                $OBuf = $OBuf + @CRLF + Left($Spaces, $Indent)
              EndIf
            Until $C = '"' or $Cp >= $Ll
          Until $C = '"' or $Lp = $LineCt	; continue until trailing quote OR end of data 
          $OBuf = $OBuf + $Fe_St		; Add STRING end format code to output buffer 
 
	;====================================================================== 
        ; This process wraps formatting around SINGLE-QUOTED STRINGS 
        Case $C = "'"				; Found a leading double-quote 
          $OBuf = $OBuf + $Fs_St + $C		; Add the STRING format code and char to Output buffer 
          Do
            If $Cp >= $Ll			; If current pointer > working line length 
              $SrcTxt[$Lp] = $OBuf		; write output string to file text array 
              $Lp = $Lp + 1			; Increment the line pointer 
              $Line = $SrcTxt[$Lp]		; get the next line 
              $OBuf = ''			; init the output buffer 
              $Cp = 0 $Wp = 0			; init the Line character pointer 
              $Ll = Len($Line)			; set the new line length 
            EndIf
            Do					; read input Line and copy to Output buffer until trailing quote OR 
              $Cp = $Cp + 1 $Wp = $Wp + 1	; end of input line is reached 
              $C = SubStr($Line, $Cp, 1)
              $OBuf = $OBuf + $C
              $Indent = WC($C, $Wp, $Line, $LLW)
              If $Indent			; Long line! Break here & indent next 
                $Wp = $Indent			; Account for the indent to setup next wrap pointer 
                $OBuf = $OBuf + @CRLF + Left($Spaces, $Indent)
              EndIf
            Until $C = "'" or $Cp >= $Ll
          Until $C = "'" or $Lp = $LineCt	; continue until trailing quote OR end of data 
          $OBuf = $OBuf + $Fe_St		; Add STRING end format code to output buffer 
 
	;====================================================================== 
        ; This process wraps formatting around BLOCK COMMENTS 
        Case $C = Chr(4)			; Found an initial block comment tag 
          $OBuf = $OBuf + $Fs_co + ''		; Add the COMMENT format code and "" to Output buffer 
          Do
            If $Cp >= $Ll			; If current pointer > working line length 
              $SrcTxt[$Lp] = $OBuf		; write output string to file text array 
              $Lp = $Lp + 1			; Increment the line pointer 
              $Line = $SrcTxt[$Lp]		; get the next line 
              $OBuf = ''			; init the output buffer 
              $Cp = 0 $Wp = 0			; init the Line character pointer 
              $Ll = Len($Line)			; set the new line length 
            EndIf
            Do					; read input Line and copy to Output buffer until trailing quote OR 
              $Cp = $Cp + 1 $Wp = $Wp + 1	; end of input line is reached 
              $C = SubStr($Line, $Cp, 1)
              If $C = Chr(5)
                $OBuf = $OBuf + ''
              Else
                $OBuf = $OBuf + $C
              EndIf
              $Indent = WC($C, $Wp, $Line, $LLW)
              If $Indent			; Long line! Break here & indent next 
                $Wp = $Indent			; Account for the indent to setup next wrap pointer 
                $OBuf = $OBuf + @CRLF + Left($Spaces, $Indent)
              EndIf
            Until $C = Chr(5) or $Cp >= $Ll
          Until $C = Chr(5) or $Lp = $LineCt	; continue until trailing quote OR end of data 
          $OBuf = $OBuf + $Fe_St		; Add STRING end format code to output buffer 
 
	;====================================================================== 
        ; Wrap format codes around a COMMENT 
        Case $C = ";"				; found a comment char 
          $OBuf = $OBuf + $Fs_Co + $C		; Add the COMMENT format code and char to Output buffer 
          Do					; read input Line and copy to Output buffer until  
            $Cp = $Cp + 1 $Wp = $Wp + 1		; end of input line is reached 
            $C = SubStr($Line, $Cp, 1)
            $OBuf = $OBuf + $C
            $Indent = WC($C, $Wp, $Line, $LLW)
            If $Indent				; Long line! Break here & indent next 
              $Wp = $Indent			; Account for the indent to setup next wrap pointer 
              $OBuf = $OBuf + @CRLF + Left($Spaces, $Indent - 2) + '; '
            EndIf
          Until $Cp >= $Ll			; continue until end of line 
        $OBuf = $OBuf + $Fe_Co			; Add COMMENT end format code to output buffer 
 
	;====================================================================== 
        ; Wrap format codes around a VARIABLE 
        Case $C = Chr(36)
          Do
            $TBuf = $TBuf + $C			; add next char to TEMP buffer 
            $Cp = $Cp + 1 $Wp = $Wp + 1		; increment line pointer 
            $C = SubStr($Line, $Cp ,1)		; Get next char from input Line 
          Until InStr($T1, $C) Or $Cp > Len($Line)			; until VarName terminating char is found 
          $OBuf = $OBuf + $Fs_Va + $TBuf + $Fe_Va	; add String and format codes to output buffer 
          $Cp = $Cp - 1 $Wp = $Wp - 1		; reset (decrement) line pointer 
          $TBuf = ''
 
          ; handle KForms $var.x.y type declarations 
          ; If the terminating character was a ".", read the remaining chars until another 
          ; non="." terminating char is found 
          If $C = '.'
            $Cp = $Cp + 1 $Wp = $Wp + 1		; reset (decrement) line pointer 
            Do
              $TBuf = $TBuf + $C		; add next char to TEMP buffer 
              $Cp = $Cp + 1 $Wp = $Wp + 1	; increment line pointer 
              $C = SubStr($Line, $Cp ,1)	; Get next char from input Line 
            Until InStr($T2, $C) Or $Cp > Len($Line)		; until terminating char is found 
            $OBuf = $OBuf + $TBuf		; add String to output buffer 
            $Cp = $Cp - 1 $Wp = $Wp - 1		; reset (decrement) line pointer 
          $TBuf = ''
 
          EndIF
 
	;====================================================================== 
        ; Wrap format codes around a MACRO 
        ; Same process as above, except for Macros - inserts Macro format tags 
        Case $C = '@'
          Do
            $TBuf = $TBuf + $C			; add next char to TEMP buffer 
            $Cp = $Cp + 1			; increment line pointer 
            $C = SubStr($Line, $Cp ,1)		; Get next char from input Line 
          Until InStr($T1, $C) Or $Cp > Len($Line)			; until VarName terminating char is found 
          $OBuf = $OBuf + $Fs_Ma + $TBuf + $Fe_Ma	; add String and format codes to output buffer 
          $Cp = $Cp - 1				; reset (decrement) line pointer 
          $TBuf = ''
 
	;====================================================================== 
        ; Wrap format codes around a NUMBER 
        Case InStr($N, $C)			; current char found in number string ($n) 
          Do
            $TBuf = $TBuf + $C			; add next char to TEMP buffer 
            $Cp = $Cp + 1			; increment line pointer 
            $C = SubStr($Line, $Cp ,1)		; Get next char from input Line 
          Until InStr($T2, $C) Or $Cp > Len($Line)			; until terminating char is found (no ".") 
          $OBuf = $OBuf + $Fs_No + $TBuf + $Fe_No	; add String and format codes to output buffer 
          $Cp = $Cp - 1				; reset (decrement) line pointer 
          $TBuf = ''
 
	;====================================================================== 
        ; Terminating character found - just add to output buffer and check for long line break 
        Case InStr($N + $T1, $C)			; current char found in number string ($n) 
          $OBuf = $OBuf + $C
          $Indent = WC($C, $Wp, $Line, $LLW)
          If $Indent				; Long line! Break here & indent next 
            $Wp = $Indent			; Account for the indent to setup next wrap pointer 
            $OBuf = $OBuf + @CRLF + Left($Spaces, $Indent)
          EndIf
 
 
	;====================================================================== 
        ; Not a comment, string, macro, or variable - might be a command or function 
        Case 1
          Do
            $TBuf = $TBuf + $C			; Add char to TEMP buffer 
            $Cp = $Cp + 1 $Wp = $Wp + 1		; Increment line pointer 
            $C = SubStr($Line, $Cp ,1)		; Get next char 
          Until InStr($T1, $C) Or $Cp > Len($Line)			; until number-terminating char is found 
          $Cp = $Cp - 1 $Wp = $Wp - 1		; reset (decrement) character pointer 
 
          ; Examine the TEMP buffer and apply formatting for FUNCTION or COMMAND 
          Select
 
            ; Found in list of commands - apply COMMAND format tags to output buffer 
            Case Ascan($Cm_Table, $TBuf) > -1
              $OBuf = $OBuf + $Fs_Cm + $TBuf + $Fe_Cm
 
            ; Found in list of functions - apply FUNCTION format tags to output buffer 
            Case Ascan($Fn_Table, $TBuf) > -1
              $OBuf = $OBuf + $Fs_Fn + $TBuf + $Fe_Fn
 
            ; conjunction - simply output the text 
            case $TBuf = "not" or $TBuf = "and" or $TBuf = "or" or $TBuf = "mod"
            $OBuf = $OBuf + $TBuf
 
            ; catch all - just add to output  
            case 1
              $OBuf = $OBuf + $TBuf
 
          EndSelect
 
      EndSelect
    Until $Cp >= $Ll				; keep processing until end of line 
    $SrcTxt[$Lp] = $OBuf			; write output buffer back to array 
 
  Next ; Lp 
 
  ; $SrcTxt is array of all file text.  
  ; Join into a single string 
  ; Translate Chr(1) to "&" 
  ; Translate Chr(2) to "&lt;" 
  ; Translate Chr(3) to "&gt;" 
  ; Hiding the replaced chars prevents treating them during the HTML processing 
  $OutBuffer = $OutBuffer + Join(Split(Join(Split(Join(Split(Join($SrcTxt,@CRLF),$1),'&amp;'),$2),'&lt;'),$3),'&gt;')
  $OutBuffer = $OutBuffer + '</pre>' + @CRLF + '</fieldset>' + @CRLF
  $OutBuffer = $OutBuffer + '</td>' + '</tr>' + '</table>' + @CRLF
  $Kix2HTM = $OutBuffer
 
EndFunction
 
 
 
 
; Check for line wrap opportunity 
; $_C = current char, $_Wp = Wrap Pointer, $_LBuf = Line, $_Wf = Wrap Flag/Column # 
Function WC($_C, $_Wp, $_Lbuf, $_Wf)
 
  Dim $_Wc			; wrapping char 
  Dim $_P			; char pointer 
 
  $WC = 0
 
  ; do we have a reason to exit - wrap not enabled? Short line? Not a wrappable character? 
  If $_Wf = 0 Or $_Wp < $_Wf Or Not InStr(Chr(9) + Chr(32) + ',+-', $_C)
    Exit 0
  EndIf
 
  ; long line, wrappable char, wrap enabled! 
  ; need to check the original line and determine how far to indent, +2 
 
  $WC = 2
 
  ; find original line indent 
  $_P = 1
  While $_P < Len($_LBuf)
    $_Wc = SubStr($_Lbuf,$_P,1)
    Select
      Case $_Wc = ' '
        $WC = $WC + 1		; add a space 
      Case $_Wc = Chr(9)
        $WC = $WC + 8		; add 8 spaces 
      Case 1
        $_P = 99999		; done! 
    EndSelect
    $_P = $_P + 1
  Loop
 
  Exit 0
  
 
EndFunction