1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
#
# This file defines and implements a very primitive macro language for
# documents using the Tcl interpreter as it's parser. It is designed for
# use with "man-page" documents. Source documents can be formatted as
# either nroff (for the man command) or html (for browser and windows
# users).
#
# The following "macros" (really Tcl procs) are available:
#
# TH
# SQ
# Section
# Subsection
# Option
# Bulletlist
# Subcommand
# Code
#
###########################################################################
# Utility procedures
#
proc lshift {lvar} {
upvar $lvar list
set ret [lindex $list 0]
set list [lrange $list 1 end]
return $ret
}
proc strip_leading_tabs {text} {
set ret ""
foreach line [split $text "\n"] {
append ret "[regsub {^\t} $line {}]\n"
}
return $ret
}
proc usage {} {
puts stderr "Usage: $::argv0 \[-nroff|-html] <input-file>"
exit -1
}
#
# End utility procedures:
###########################################################################
###########################################################################
# Generic document macros.
proc SQ {args} {
return "\[$args\]"
}
# End generic document macros
###########################################################################
###########################################################################
# The following procedures interpret macros used in document files for
# nroff output.
#
namespace eval NroffMacros {
namespace export TH Section Option Bulletlist Code
namespace export Subcommand Subsection process_text
proc process_text {input {nocommands 0}} {
if {$nocommands} {
set output [subst -novariables -nocommands $input]
} else {
set output [subst -novariables $input]
}
set output [
regsub -all {_([^ \n]*)_} $output {\1}
]
set output [string map { " -" " \\-" "\t-" "\t\\-"
"\t." "\t ." "'" "\""} $output ]
set output [string trim [strip_leading_tabs $output]]
set output [regsub -all {[\n]*REMOVELINEBREAK\n} $output \n]
return $output
}
proc TH {name section} {
return "\n.TH \"$name\" \"3tcl\" \"Sat Feb 25 2006\"\n"
}
proc Section {args} {
set heading [string toupper $args]
return ".SH \"$heading\""
}
proc Option {name description} {
set dbname [string toupper [string range $name 0 0]][string range $name 1 end]
return [string trim [subst {
Command-Line Name:-$name
.br
Database Name: $name
.br
Database Class: $dbname
.br
.RS
.PP
[process_text $description]
.RE
}]]
}
proc Bulletlist {args} {
set nroff "REMOVELINEBREAK\n"
append nroff ".RS\n"
foreach a $args {
append nroff ".IP * 2\n"
append nroff "[string trim [strip_leading_tabs $a]]\n"
}
append nroff ".RE"
return $nroff
}
proc Code {args} {
set nroff "REMOVELINEBREAK\n"
if {[llength $args] == 1} {set args [lindex $args 0]}
append nroff ".RS\n"
append nroff "\n.nf\n"
append nroff [process_text $args 1]
append nroff "\n.fi\n"
append nroff ".RE"
return $nroff
}
proc Subcommand {n {text {}}} {
set nroff "REMOVELINEBREAK\n"
if {$text == {}} {
set text $n
set n 1
} else {
set n [expr int(abs($n))]
}
set i 0
foreach line [split [process_text $text] "\n"] {
if {$i == $n} {
append nroff "\n.RS\n"
}
if {$i < $n} {
append nroff "\n$line\n.br"
} else {
append nroff "$line\n"
}
incr i
}
if {$i > $n} {
append nroff ".RE"
}
return $nroff
}
proc Subsection {args} {
set heading [string toupper $args]
return ".SS \"$heading\""
}
}; # namespace eval NroffMacros...
#
# End nroff document macros.
###########################################################################
###########################################################################
# Html document macros.
#
namespace eval HtmlMacros {
namespace export TH Section Option Bulletlist Code Subcommand Subsection
namespace export process_text process_index
proc process_index {} {
set state 1
set html {<a name="index"></a><ul>}
set i 0
foreach s $::HtmlMacros::Index {
foreach {level title} $s {}
set I [incr i]
switch $level {
SECTION {
if {$state == 2} {
append html </ul>
set state 1
}
append html "<li><a href=\"#section$I\">$title</a>"
}
SUBSECTION {
if {$state == 1} {
append html <ul>
set state 2
}
append html "<li><a href=\"#section$I\">$title</a>"
}
}
}
if {$state == 2} {
append html </ul>
}
append html </ul>
return $html
}
proc process_text {input {nocommands 0} {noparagraphs 0}} {
if {$nocommands} {
set output [subst -novariables -nocommands $input]
} else {
set output [subst -novariables $input]
}
set output [string trim [strip_leading_tabs $output]]
set output [
regsub -all {_([^ \n]*)_} $output {<i>\1</i>}
]
set blocknest 0
set paraopen 0
set out ""
foreach o [split $output "\n"] {
set isstart [expr [string match $o $::HtmlMacros::START]]
set isend [expr [string match $o $::HtmlMacros::END]]
if {$isstart} {
if {$paraopen} {
append out "</p>\n"
set paraopen 0
}
incr blocknest
} elseif {$isend} {
incr blocknest -1
} else {
if {0 == $blocknest} {
set isempty [regexp {^[:space:]*$} $o]
if {$isempty && $paraopen} {
append out "</p>\n"
set paraopen 0
} elseif {!$noparagraphs && !$isempty && !$paraopen} {
append out "<p>\n"
set paraopen 1
}
}
append out "$o\n"
}
}
return $out
}
set START STARTSTARTSTARTSTART
set END ENDENDENDENDEND
proc Block {code} {
return "\n$::HtmlMacros::START\n$code\n$::HtmlMacros::END\n"
}
proc TH {name section} {
set ::HtmlMacros::Title "$name ($section)"
return ""
}
set N 0
proc Section {args} {
set n [incr ::HtmlMacros::N]
lappend ::HtmlMacros::Index [list SECTION $args]
return [Block "<a name=\"section$n\"><h2>$args</h2></a>"]
}
proc Subsection {args} {
set n [incr ::HtmlMacros::N]
lappend ::HtmlMacros::Index [list SUBSECTION $args]
return [Block "<a name=\"section$n\"><h3>$args</h3></a>"]
}
proc Code {args} {
if {[llength $args] == 1} {set args [lindex $args 0]}
set text [process_text $args 1 1]
return [Block "<div style=\"margin-left:8ex\"><pre>$text</pre></div>"]
}
proc Option {name description} {
set dbname [string toupper [string range $name 0 0]][string range $name 1 end]
return [Block [subst {
<p>Command-Line Name: -$name<br>
Database Name: $name<br>
Database Class: $dbname
</p>
<div style="margin-left:8ex">
[process_text $description]
</div>
}]]
}
proc Bulletlist {args} {
set items {}
foreach a $args {
append items "<li>$a"
}
return [Block "<ul>$items</ul>"]
}
proc Subcommand {n {text {}}} {
if {$text == {}} {
set text $n
set n 1
} else {
set n [expr int(abs($n))]
}
set i 0
set description {}
set lines [split [string trim $text] "\n"]
append html [subst {
<p>
[join [lrange $lines 0 [expr $n - 1]] <br>]
</p>
}]
append html {<div style="margin-left:8ex">}
append html [process_text [join [lrange $lines $n end] "\n"]]
append html {</div>}
return [Block $html]
}
}
# End html document macros
###########################################################################
if {[llength $argv] != 2} usage
set mode [lindex $argv 0]
set file [lindex $argv 1]
if {$mode != "-html" && $mode != "-nroff"} usage
set fd [open $file]
set input [strip_leading_tabs [read $fd]]
close $fd
set ::TABS ""
catch {
source [file join [file dirname $file] .. webpage common.tcl]
if {[string match *hv3* $file]} {
set ::TABS [getTabs 4]
} else {
set ::TABS [getTabs 2]
}
} msg
# puts stderr "ERROR $msg"
switch -- $mode {
-nroff {
namespace import NroffMacros::*
puts [process_text $input]
}
-html {
namespace import HtmlMacros::*
set input [regsub -all {>} $input {\>}]
set input [regsub -all {<} $input {\<}]
set text [process_text $input]
puts [subst {
<html>
<head>
<title>$::HtmlMacros::Title</title>
<link rel="stylesheet" href="tkhtml_tcl_tk.css"</link>
</head>
<body>
$::TABS
<div id="body">
<h1>$::HtmlMacros::Title</h1>
<div class="sidebox"><div id="toc">
<a name="TOC"><h3>Table Of Contents</h3></a>
[process_index]
</div></div>
<div id="text">
$text
</div></div>
</body>
</html>
}]
}
}
|