; this file converts newLISP source to an HTML page ; (syntax-highlight source-text title) returns HTML page of source ; as used by extended version of newlispdoc, newlispdoc-ext. ; requires newLISP parser (load (string (env {HOME}) {/projects/programming/newlisp-projects/newlisp-parser.lsp})) (define (set-up-syntax) (set 'built-in-functions (map string (symbols 'MAIN))) (set 'obsolete-functions (map string '(write-buffer read-buffer name parse-date assoc-set nth-set ref-set replace-assoc set-assoc set-nth))) (set 'newlisp-variables (map string '(ostype $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $args $idx $it $main-args))) (set 'parenlevel 0)) (define (nlx-to-html nlx (depth 0)) (when (= depth 0) (set 'buff {}) ; if first pass, initialize a buffer (set-up-syntax)) (dolist (i nlx) (set 'token-type (first i) 'token-value (last i)) (if (atom? token-type) (cond ((= token-type 'Nlex:LeftParen) (inc paren-level) (write buff (format {(} paren-level ))) ((= token-type 'Nlex:RightParen) (write buff (format {)})) (dec paren-level)) ((= token-type 'Nlex:Symbol) (cond ((find token-value newlisp-variables) (write buff (string {} (escape-html token-value) {}))) ((find token-value obsolete-functions) (write buff (string {} (escape-html token-value) {}))) ((find token-value built-in-functions) (write buff (string {} (escape-html token-value) {}))) (true (write buff (string {} (escape-html token-value) {}))))) ((= token-type 'Nlex:WhiteSpace) (write buff {}) (dostring (s (base64-dec (string token-value))) (write buff (char s))) (write buff {})) ((= token-type 'Nlex:BracedString) (write buff (string {} "{" (escape-html token-value) "}" {}))) ((= token-type 'Nlex:QuotedString) (write buff (string {} {"} (escape-html token-value) {"} {}))) ((= token-type 'Nlex:BracketedText) (write buff (string {} {[text]} (escape-html token-value) {[/text]} {}))) ((= token-type 'Nlex:Quote) (write buff (string {'}))) ((= token-type 'Nlex:Comment) (write buff (string {} (escape-html token-value) {}))) ((= token-type 'Nlex:Integer) (write buff (string {} (int token-value) {}))) ((= token-type 'Nlex:Float) (write buff (string {} (escape-html token-value) {}))) ((= token-type 'Nlex:Scientific) (write buff (string {} (escape-html token-value) {}))) ((= token-type 'Nlex:Hex) (write buff (string {} (escape-html token-value) {}))) ((= token-type 'Nlex:BracketedCommand) (write buff (string {} token-value {}))) ((= token-type 'Nlex:NaN) ; not a number (write buff (string {} token-value {}))) ((= token-type 'Nlex:Octal) (write buff (string {} token-value {}))) ((= token-type 'Nlex:BracketedIdentifier) ; bracketed identifier (write buff (string {[} token-value {]})))) ; not an atom, so recurse but don't initialize buffer (nlx-to-html i 1))) buff) (define (escape-html txt) (if txt (begin (replace {&} txt {&} 0) (replace {<} txt {<} 0) (replace {>} txt {>} 0))) txt) (set 'HTML-header-1 [text] [/text]) (set 'HTML-header-2 [text] [/text]) (define (syntax-highlight source-text title) (let ((HTML-text {})) (set-up-syntax) ; header (extend HTML-text HTML-header-1) (extend HTML-text (format {%s} title)) (extend HTML-text HTML-header-2) ; start body (extend HTML-text {
})        
        (extend HTML-text (nlx-to-html (Nlex:parse-newlisp source-text)))
        (extend HTML-text {
}) (extend HTML-text {

; syntax highlighting by newlisp-parser.lsp

}) (extend HTML-text {}) HTML-text)) ; eof