REBOL [
Title: "Color REBOL Code in HTML"
Author: "Carl Sassenrath"
File: %color-code.r
Date: 28-May-2000
Purpose: {Color source code based on datatype.}
Category: [script util text 3]
]
color-coder: make object! [
; Set the color you want for each datatype:
colors: [
char! 0.120.40
date! 0.120.150
decimal! 0.120.150
email! 0.120.40
file! 0.120.40
integer! 0.120.150
issue! 0.120.40
money! 0.120.150
pair! 0.120.150
string! 0.120.40
tag! 0.120.40
time! 0.120.150
tuple! 0.120.150
url! 0.120.40
refinement! 160.120.40
cmt 10.10.160
]
out: none
emit: func [data] [repend out data]
to-color: func [tuple][
result: copy "#"
repeat n 3 [append result back back tail to-hex pick tuple n]
result
]
emit-color: func [value start stop /local color][
either none? :value [color: select colors 'cmt][
if path? :value [value: first :value]
color: either word? :value [
any [
all [value? :value any-function? get :value 140.0.0]
all [value? :value datatype? get :value 120.60.100]
]
][
any [select colors type?/word :value]
]
]
either color [
emit [build-tag [font color (to-color color)]
copy/part start stop </font>]
][
emit copy/part start stop
]
]
set 'color-code func [
"Return color source code as HTML."
text [string!] "Source code text"
/local str new
][
out: make string! 3 * length? text
emit {<html><body bgcolor="#ffffff"><pre>}
; Escape special HTML, but keep them valid REBOL (no ;)
foreach [from to] ["&" "&" "<" "<" ">" ">"][
replace/all text from to
]
parse/all detab text blk-rule: [
some [
str:
some [" " | tab] new: (emit copy/part str new) |
newline (emit newline)|
#";" thru newline new: (emit-color none str new) |
[#"[" | #"("] (emit first str) blk-rule |
[#"]" | #")"] (emit first str) |
skip (
set [value new] load/next str
emit-color :value str new
) :new
]
]
emit {</pre></body></html>}
foreach [from] ["&" "<" ">"][
replace/all out from join from ";"
]
out
]
]
;Example: write %color-script.html color-code read %color-script.r