Code to HTML
To display software code in a browser as what it is supposed to be, you need to do encoding according to the
HTML standard. Example, to display a "<" (less than sign), it has to be written as "<".
A CGI program is written to help my friend to put his code in the blog. BTW, I used to script this interactively in a Tcl shell and now I don't have to do that anymore :-)
The following tools and environment are used:
- Solaris 10
- Apache that comes with the OS
- Tcl (8.3.3) that comes with the OS
- Tcllib 1.9 (html package)
- cgi.tcl by Don Libes who wrote the famous Expect
The bulk of the work is done by this Tcl procedure '::html::html_entities' which it does a 'string map' dictionary mapping from special characters to their entities
variable entities {
\xa0 \xa1 ¡ \xa2 ¢ \xa3 £ \xa4 ¤
\xa5 ¥ \xa6 ¦ \xa7 § \xa8 ¨ \xa9 ©
\xaa ª \xab « \xac ¬ \xad ­ \xae ®
\xaf ¯ \xb0 ° \xb1 ± \xb2 ² \xb3 ³
\xb4 ´ \xb5 µ \xb6 ¶ \xb7 · \xb8 ¸
\xb9 ¹ \xba º \xbb » \xbc ¼ \xbd ½
\xbe ¾ \xbf ¿ \xc0 À \xc1 Á \xc2 Â
\xc3 Ã \xc4 Ä \xc5 Å \xc6 Æ \xc7 Ç
\xc8 È \xc9 É \xca Ê \xcb Ë \xcc Ì
\xcd Í \xce Î \xcf Ï \xd0 Ð \xd1 Ñ
\xd2 Ò \xd3 Ó \xd4 Ô \xd5 Õ \xd6 Ö
\xd7 × \xd8 Ø \xd9 Ù \xda Ú \xdb Û
\xdc Ü \xdd Ý \xde Þ \xdf ß \xe0 à
\xe1 á \xe2 â \xe3 ã \xe4 ä \xe5 å
\xe6 æ \xe7 ç \xe8 è \xe9 é \xea ê
\xeb ë \xec ì \xed í \xee î \xef ï
\xf0 ð \xf1 ñ \xf2 ò \xf3 ó \xf4 ô
\xf5 õ \xf6 ö \xf7 ÷ \xf8 ø \xf9 ù
\xfa ú \xfb û \xfc ü \xfd ý \xfe þ
\xff ÿ \u192 ƒ \u391 Α \u392 Β \u393 Γ
\u394 Δ \u395 Ε \u396 Ζ \u397 Η \u398 Θ
\u399 Ι \u39A Κ \u39B Λ \u39C Μ \u39D Ν
\u39E Ξ \u39F Ο \u3A0 Π \u3A1 Ρ \u3A3 Σ
\u3A4 Τ \u3A5 Υ \u3A6 Φ \u3A7 Χ \u3A8 Ψ
\u3A9 Ω \u3B1 α \u3B2 β \u3B3 γ \u3B4 δ
\u3B5 ε \u3B6 ζ \u3B7 η \u3B8 θ \u3B9 ι
\u3BA κ \u3BB λ \u3BC μ \u3BD ν \u3BE ξ
\u3BF ο \u3C0 π \u3C1 ρ \u3C2 ς \u3C3 σ
\u3C4 τ \u3C5 υ \u3C6 φ \u3C7 χ \u3C8 ψ
\u3C9 ω \u3D1 ϑ \u3D2 ϒ \u3D6 ϖ
\u2022 • \u2026 … \u2032 ′ \u2033 ″
\u203E ‾ \u2044 ⁄ \u2118 ℘ \u2111 ℑ
\u211C ℜ \u2122 ™ \u2135 ℵ \u2190 ←
\u2191 ↑ \u2192 → \u2193 ↓ \u2194 ↔ \u21B5 ↵
\u21D0 ⇐ \u21D1 ⇑ \u21D2 ⇒ \u21D3 ⇓ \u21D4 ⇔
\u2200 ∀ \u2202 ∂ \u2203 ∃ \u2205 ∅
\u2207 ∇ \u2208 ∈ \u2209 ∉ \u220B ∋ \u220F ∏
\u2211 ∑ \u2212 − \u2217 ∗ \u221A √
\u221D ∝ \u221E ∞ \u2220 ∠ \u2227 ∧ \u2228 ∨
\u2229 ∩ \u222A ∪ \u222B ∫ \u2234 ∴ \u223C ∼
\u2245 ≅ \u2248 ≈ \u2260 ≠ \u2261 ≡ \u2264 ≤
\u2265 ≥ \u2282 ⊂ \u2283 ⊃ \u2284 ⊄ \u2286 ⊆
\u2287 ⊇ \u2295 ⊕ \u2297 ⊗ \u22A5 ⊥
\u22C5 ⋅ \u2308 ⌈ \u2309 ⌉ \u230A ⌊
\u230B ⌋ \u2329 ⟨ \u232A ⟩ \u25CA ◊
\u2660 ♠ \u2663 ♣ \u2665 ♥ \u2666 ♦
\x22 " \x26 & \x3C < \x3E > \u152 Œ
\u153 œ \u160 Š \u161 š \u178 Ÿ
\u2C6 ˆ \u2DC ˜ \u2002   \u2003   \u2009  
\u200C ‌ \u200D ‍ \u200E ‎ \u200F ‏ \u2013 –
\u2014 — \u2018 ‘ \u2019 ’ \u201A ‚
\u201C “ \u201D ” \u201E „ \u2020 †
\u2021 ‡ \u2030 ‰ \u2039 ‹ \u203A ›
\u20AC €
}
}
# ::html::html_entities --
# Replaces all special characters in the text with their
# entities.
#
# Arguments:
# s The near-HTML text
#
# Results:
# The text with entities in place of specials characters.
proc ::html::html_entities {s} {
variable entities
return [string map $entities $s]
}
Here is the CGI program:
#! /usr/sfw/bin/tclsh
package require cgi
package require http
package require html
set title {Converting Code to HTML}
set htmlHeader "
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<hr>
"
set htmlForm "
<form method=post action=$env(REQUEST_URI)>
<input type=submit name=submit value=\"Convert to Raw HTML\"><br>
<textarea name=code cols=80 rows=10>
</textarea>
</form>
"
set htmlFooter {
</body>
</html>
}
puts "Content-type: text/html\n"
puts $htmlHeader
puts $htmlForm
if { $env(REQUEST_METHOD) == "POST" } {
cgi_input
set code {}
if { [catch {cgi_import code} code] } {
puts "Error in $code"
puts $htmlFooter
exit
}
puts "<hr>"
puts "<h1>Raw HTML</h1>"
puts "<pre>[string map {& &} [::html::html_entities $code]</pre>]"
puts "<hr>"
puts "<h1>Rendered HTML</h1>"
puts "<pre>"
puts [::html::html_entities $code]
puts "<pre>"
}
puts $htmlFooter



0 Comments:
Post a Comment
<< Home