Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 183 lines (165 sloc) 8.982 kB
847c939 @cormullion added version of newlispdoc that allows external syntax-hilite
authored
1 ; this file converts newLISP source to an HTML page
2 ; (syntax-highlight source-text title) returns HTML page of source
3 ; as used by extended version of newlispdoc, newlispdoc-ext.
4
5 ; requires newLISP parser
6 (load (string (env {HOME}) {/projects/programming/newlisp-projects/newlisp-parser.lsp}))
7
8 (define (set-up-syntax)
9 (set 'built-in-functions (map string (symbols 'MAIN)))
10 (set 'obsolete-functions (map string '(write-buffer read-buffer name parse-date assoc-set nth-set ref-set replace-assoc set-assoc set-nth)))
11 (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)))
12 (set 'parenlevel 0))
13
14 (define (nlx-to-html nlx (depth 0))
15 (when (= depth 0)
16 (set 'buff {}) ; if first pass, initialize a buffer
17 (set-up-syntax))
18 (dolist (i nlx)
19 (set 'token-type (first i) 'token-value (last i))
20 (if (atom? token-type)
21 (cond
22 ((= token-type 'Nlex:LeftParen)
23 (inc paren-level)
24 (write buff (format {<span class="open-paren%d">(<span class="plain">} paren-level )))
25 ((= token-type 'Nlex:RightParen)
26 (write buff (format {</span>)</span>}))
27 (dec paren-level))
28 ((= token-type 'Nlex:Symbol)
29 (cond
30 ((find token-value newlisp-variables)
31 (write buff (string {<span class="variable">} (escape-html token-value) {</span>})))
32 ((find token-value obsolete-functions)
33 (write buff (string {<span class="obsolete">} (escape-html token-value) {</span>})))
34 ((find token-value built-in-functions)
35 (write buff (string {<span class="built-in">} (escape-html token-value) {</span>})))
36 (true
37 (write buff (string {<span class="symbol">} (escape-html token-value) {</span>})))))
38 ((= token-type 'Nlex:WhiteSpace)
39 (write buff {<span class="white-space">})
40 (dostring (s (base64-dec (string token-value)))
41 (write buff (char s)))
42 (write buff {</span>}))
43 ((= token-type 'Nlex:BracedString)
44 (write buff (string {<span class="braced-string">} "{" (escape-html token-value) "}" {</span>})))
45 ((= token-type 'Nlex:QuotedString)
46 (write buff (string {<span class="quoted-string">} {"} (escape-html token-value) {"} {</span>})))
47 ((= token-type 'Nlex:BracketedText)
48 (write buff (string {<span class="bracketed-string">} {[text]} (escape-html token-value) {[/text]} {</span>})))
49 ((= token-type 'Nlex:Quote)
50 (write buff (string {<span class="quote">'</span>})))
51 ((= token-type 'Nlex:Comment)
52 (write buff (string {<span class="comment">} (escape-html token-value) {</span>})))
53 ((= token-type 'Nlex:Integer)
54 (write buff (string {<span class="integer">} (int token-value) {</span>})))
55 ((= token-type 'Nlex:Float)
56 (write buff (string {<span class="float">} (escape-html token-value) {</span>})))
57 ((= token-type 'Nlex:Scientific)
58 (write buff (string {<span class="scientific">} (escape-html token-value) {</span>})))
59 ((= token-type 'Nlex:Hex)
60 (write buff (string {<span class="hex-string">} (escape-html token-value) {</span>})))
61 ((= token-type 'Nlex:BracketedCommand)
62 (write buff (string {<span class="bracketed-command">} token-value {</span>})))
63 ((= token-type 'Nlex:NaN) ; not a number
64 (write buff (string {<span class="NaN">} token-value {</span>})))
65 ((= token-type 'Nlex:Octal)
66 (write buff (string {<span class="octal">} token-value {</span>})))
67 ((= token-type 'Nlex:BracketedIdentifier) ; bracketed identifier
68 (write buff (string {<span class="octal">[} token-value {]</span>}))))
69 ; not an atom, so recurse but don't initialize buffer
70 (nlx-to-html i 1)))
71 buff)
72
73 (define (escape-html txt)
74 (if txt
75 (begin
76 (replace {&} txt {&amp;} 0)
77 (replace {<} txt {&lt;} 0)
78 (replace {>} txt {&gt;} 0)))
79 txt)
80
81 (set 'HTML-header-1 [text]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
82 "http://www.w3.org/TR/html4/strict.dtd">
83 <html lang="en">
84 <head>
85 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
86 [/text])
87
88 (set 'HTML-header-2 [text]
89 <style type="text/css" media="screen">
90 body {color: #000; padding:3px ; margin:0px;}
91 a {color: #666; text-decoration: none; font-weight: bold; }
92 a:hover {color: #000; }
93 a span {display: none; }
94 pre, code { margin: 0px; white-space: pre-wrap; font-family: mono, 'Inconsolata', Monaco, 'Lucida Console'; }
95 .symbol { color: #dd3333; background: #ffffff;}
96 .built-in {color: #550000; font-weight: bold;}
97 .obsolete {color: #ffff00; background: #100;}
98 .variable { color: #ff8888; font-weight: bold;}
99 .braced-string {color: #226666; background: #fffffe;}
100 .quoted-string {color: #226666; background: #fffffe;}
101 .bracketed-string {color: #226666; background: #efffff;}
102 .quote {color: #224400; background: #efffff;}
103 .comment {color: #666666; font-family: serif; font-size: 0.9em; background: #feffff; }
104 .integer {color: #113366; background: #eeffbb; }
105 .float {color: #635533; background: #eeffcd;}
106 .hex {color: #636633; background: #eeffdc;}
107 .octal {color: #736699; }
108 .white-space {background: #ffffff;}
109 .plain {background-color: #ffffff}
110
111 span.open-paren1 { color: #666; }
112 span.open-paren1:hover {
113 -webkit-transition: background-color 0.7s linear;
114 color: #000;
115 background-color: #f90; }
116
117 span.open-paren2 { color: #666; }
118 span.open-paren2:hover {
119 -webkit-transition: background-color 0.7s linear;
120 color: #111;
121 background-color: #f20; }
122
123 span.open-paren3 { color: #666; }
124 span.open-paren3:hover {
125 -webkit-transition: background-color 0.7s linear;
126 color: #222;
127 background-color: #59f; }
128
129 span.open-paren4 { color: #666; }
130 span.open-paren4:hover {
131 -webkit-transition: background-color 0.7s linear;
132 color: #333;
133 background-color: #FFA3CF; }
134
135 span.open-paren5 { color: #666; }
136 span.open-paren5:hover {
137 -webkit-transition: background-color 0.7s linear;
138 background-color: #BCA9FF; }
139
140 span.open-paren6 { color: #666; }
141 span.open-paren6:hover {
142 -webkit-transition: background-color 0.7s linear;
143 background-color: #FFDCA1; }
144
145 span.open-paren7 { color: #666; }
146 span.open-paren7:hover {
147 -webkit-transition: background-color 0.7s linear;
148 background-color: #9DFFAA; }
149
150 span.open-paren8 { color: #666; }
151 span.open-paren8:hover {
152 -webkit-transition: background-color 0.7s linear;
153 background-color: #ACD2FF; }
154
155 span.open-paren9 { color: #666; }
156 span.open-paren9:hover {
157 -webkit-transition: background-color 0.7s linear;
158 background-color: #AFFFFB; }
159
160 span.open-paren10 { color: #666; }
161 span.open-paren10:hover {
162 -webkit-transition: background-color 0.7s linear;
163 background-color: #EBFFD6; }
164 </style>
165 </head>
166 [/text])
167
168 (define (syntax-highlight source-text title)
169 (let ((HTML-text {}))
170 (set-up-syntax)
171 ; header
172 (extend HTML-text HTML-header-1)
173 (extend HTML-text (format {<title>%s</title>} title))
174 (extend HTML-text HTML-header-2)
175 ; start body
176 (extend HTML-text {<body><pre><code>})
177 (extend HTML-text (nlx-to-html (Nlex:parse-newlisp source-text)))
178 (extend HTML-text {</code></pre>})
179 (extend HTML-text {<p>; syntax highlighting by newlisp-parser.lsp </p></body>})
180 (extend HTML-text {</html>})
181 HTML-text))
182
183 ; eof
Something went wrong with that request. Please try again.