| 1 |
% |
| 2 |
% \iffalse |
| 3 |
%<*driver> |
| 4 |
\documentclass{tclldoc} |
| 5 |
\begin{document} |
| 6 |
\DocInput{sourcedtx.dtx} |
| 7 |
\end{document} |
| 8 |
%</driver> |
| 9 |
% \fi |
| 10 |
% |
| 11 |
% \title{The \textsf{sourcedtx} package} |
| 12 |
% \author{Lars Hellstr\"om} |
| 13 |
% \date{18 July 2003} |
| 14 |
% \maketitle |
| 15 |
% |
| 16 |
% \begin{abstract} |
| 17 |
% The \textsf{sourcedtx} package provides a \Tcllogo\ command |
| 18 |
% |dtx::source| that makes it possible to |source| \Tcllogo\ code |
| 19 |
% from a \textsf{doc}-style \texttt{.dtx} file without docstripping |
| 20 |
% it first. |
| 21 |
% \end{abstract} |
| 22 |
% |
| 23 |
% |
| 24 |
% \begin{tcl} |
| 25 |
%<*pkg> |
| 26 |
namespace eval dtx {} |
| 27 |
% \end{tcl} |
| 28 |
% \setnamespace{dtx} |
| 29 |
% |
| 30 |
% \begin{proc}{strip_string} |
| 31 |
% The |strip_string| procedure does the actual docstripping for |
| 32 |
% |source_dtx|. The syntax is |
| 33 |
% \begin{quote} |
| 34 |
% |dtx::strip_string| \word{text} \word{option list} |
| 35 |
% \word{meta prefix}\regopt\ \word{interaction}\regopt |
| 36 |
% \end{quote} |
| 37 |
% where \word{text} is the string to docstrip and \word{option list} |
| 38 |
% is the list of options to use. The \word{meta prefix} (by default |
| 39 |
% two percent signs) is the string to use for the \textsc{docstrip} |
| 40 |
% parameter \verb|\MetaPrefix|. The \word{interation} (by default 1) |
| 41 |
% is a flag for the ``interaction'' level to use. |0| means error |
| 42 |
% messages are simply written to |stderr|, whereas |1| means an error |
| 43 |
% will be raised. (It is possible that \word{interaction} will be |
| 44 |
% extended to a ``flag word'' where each bit controls some aspect of |
| 45 |
% the interaction.) |
| 46 |
% |
| 47 |
% When errors are raised, the |errorCode| is set to a list with the |
| 48 |
% format |
| 49 |
% \begin{quote} |
| 50 |
% |DOCSTRIP| \word{situation} \word{lineno} |
| 51 |
% \end{quote} |
| 52 |
% where \word{lineno} is the line number (starting at one) of the line |
| 53 |
% where the error was detected. The \word{situation}s are described |
| 54 |
% below, at the positions in the code where they can occur. |
| 55 |
% |
| 56 |
% \begin{tcl} |
| 57 |
proc dtx::strip_string {text options {metaprefix %%} {interaction 1}} { |
| 58 |
% \end{tcl} |
| 59 |
% The |O| array has entires for precisely the specified options, so |
| 60 |
% that an opion can be tested using |info exists|. |
| 61 |
% \begin{tcl} |
| 62 |
foreach option $options {set O($option) ""} |
| 63 |
% \end{tcl} |
| 64 |
% |stripped| is where the text that passes docstripping is collected. |
| 65 |
% \begin{tcl} |
| 66 |
set stripped "" |
| 67 |
% \end{tcl} |
| 68 |
% |block_stack| is the list of modules inside which the current line |
| 69 |
% lies. |offlevel| is the number of modules that must be exited |
| 70 |
% before code lines should once again be included. |verbatim| is a |
| 71 |
% flag for whether verbatim mode is in force. |
| 72 |
% \begin{tcl} |
| 73 |
set block_stack [list] |
| 74 |
set offlevel 0 |
| 75 |
set verbatim 0 |
| 76 |
% \end{tcl} |
| 77 |
% |lineno| is the input line number counter, for use in error |
| 78 |
% messages. |
| 79 |
% \begin{tcl} |
| 80 |
set lineno 1 |
| 81 |
% \end{tcl} |
| 82 |
% Here starts the main loop over lines in the \word{text}. It |
| 83 |
% constitutes the majority of the procedure and is split in two |
| 84 |
% parts. The smaller part handles lines in verbatim mode (unusual), |
| 85 |
% the large part handles lines in normal mode (with comment lines, |
| 86 |
% code lines, guard lines, and so on). |
| 87 |
% \begin{tcl} |
| 88 |
foreach line [split $text \n] { |
| 89 |
if {$verbatim} then { |
| 90 |
if {$line eq $endverbline} then { |
| 91 |
set verbatim 0 |
| 92 |
} elseif {!$offlevel} then { |
| 93 |
append stripped $line \n |
| 94 |
} |
| 95 |
} else { |
| 96 |
switch -glob -- $line %%* { |
| 97 |
if {!$offlevel} then { |
| 98 |
append stripped $metaprefix\ |
| 99 |
[string range $line 2 end] \n |
| 100 |
} |
| 101 |
} %<<* { |
| 102 |
set endverbline "%[string range $line 3 end]" |
| 103 |
set verbatim 1 |
| 104 |
} %<* { |
| 105 |
if {[regexp -- {^%<([*/+-]?)([^>]*)>(.*)$} $line ""\ |
| 106 |
modifier expression line]} then { |
| 107 |
% \end{tcl} |
| 108 |
% There is a well-formed guard line. First the expression is |
| 109 |
% evaluated, by converting it to an |expr| expression. |
| 110 |
% \begin{tcl} |
| 111 |
regsub -all -- {\\|\{|\}|\$|\[|\]| |;} $expression\ |
| 112 |
{\\&} E |
| 113 |
regsub -all -- {,} $E {|} E |
| 114 |
regsub -all -- {[^()|&!]+} $E {[info exists O(&)]} E |
| 115 |
set val [expr $E] |
| 116 |
switch -exact -- $modifier * { |
| 117 |
lappend block_stack $expression |
| 118 |
if {$offlevel || !$val} then {incr offlevel} |
| 119 |
} / { |
| 120 |
if {![llength $block_stack]} then { |
| 121 |
% \end{tcl} |
| 122 |
% In this case there was no open block for this guard to end. That |
| 123 |
% is a \describestring[error situation]{SPURIOUS}|SPURIOUS| |
| 124 |
% \word{situation}. |
| 125 |
% \begin{tcl} |
| 126 |
if {$interaction} then { |
| 127 |
error "Spurious end block </$expression>\ |
| 128 |
ignored." ""\ |
| 129 |
[list DOCSTRIP SPURIOUS $lineno] |
| 130 |
} else { |
| 131 |
puts stderr "docstrip: Spurious end\ |
| 132 |
block </$expression> ignored on line\ |
| 133 |
$lineno." |
| 134 |
} |
| 135 |
} else { |
| 136 |
if {[string compare $expression\ |
| 137 |
[lindex $block_stack end]]} then { |
| 138 |
% \end{tcl} |
| 139 |
% In this case the expression of the block being closed does not match |
| 140 |
% the expression on the block on top of the stack. That is a |
| 141 |
% \describestring[error situation]{MISMATCH}|MISMATCH| |
| 142 |
% \word{situation}. \textsc{docstrip} by default raises an error and |
| 143 |
% recovers by treating this situation as a typo. |
| 144 |
% \begin{tcl} |
| 145 |
if {$interaction} then { |
| 146 |
error "Found </$expression> instead of\ |
| 147 |
</[lindex $block_stack end]>." ""\ |
| 148 |
[list DOCSTRIP MISMATCH $lineno] |
| 149 |
} |
| 150 |
puts stderr "docstrip:\ |
| 151 |
Found </$expression> instead of\ |
| 152 |
</[lindex $block_stack end]> on line\ |
| 153 |
$lineno." |
| 154 |
} |
| 155 |
% \end{tcl} |
| 156 |
% All that error processing makes it easy to lose track, but the |
| 157 |
% following two lines are what does the real work for an end of block |
| 158 |
% guard: pop a block off the stack and decrement the |offlevel|. |
| 159 |
% \begin{tcl} |
| 160 |
if {$offlevel} then {incr offlevel -1} |
| 161 |
set block_stack [lreplace $block_stack end end] |
| 162 |
} |
| 163 |
} - { |
| 164 |
if {!$offlevel && !$val} then { |
| 165 |
append stripped $line \n |
| 166 |
} |
| 167 |
} default { |
| 168 |
if {!$offlevel && $val} then { |
| 169 |
append stripped $line \n |
| 170 |
} |
| 171 |
} |
| 172 |
} else { |
| 173 |
% \end{tcl} |
| 174 |
% In this case the line looks like a guard line, but there is no |>| |
| 175 |
% terminating the guard expression. This is a |
| 176 |
% \describestring[error situation]{BADGUARD}|BADGUARD| |
| 177 |
% \word{situation}. |
| 178 |
% \begin{tcl} |
| 179 |
if {$interaction} then { |
| 180 |
error "Malformed guard on line $lineno." ""\ |
| 181 |
[list DOCSTRIP BADGUARD $lineno] |
| 182 |
} else { |
| 183 |
puts stderr "docstrip: Malformed guard\ |
| 184 |
on line $lineno:" |
| 185 |
puts stderr $line |
| 186 |
} |
| 187 |
} |
| 188 |
} %* {}\ |
| 189 |
% \end{tcl} |
| 190 |
% With comment lines, nothing is done. A line being the exact string |
| 191 |
% |\endinput| terminates the stripping. |
| 192 |
% \begin{tcl} |
| 193 |
{\\endinput} { |
| 194 |
break |
| 195 |
} default { |
| 196 |
% \end{tcl} |
| 197 |
% Other lines are code lines. These are included or not, depending on |
| 198 |
% the |offlevel|. |
| 199 |
% \begin{tcl} |
| 200 |
if {!$offlevel} then {append stripped $line \n} |
| 201 |
} |
| 202 |
} |
| 203 |
incr lineno |
| 204 |
} |
| 205 |
return $stripped |
| 206 |
} |
| 207 |
% \end{tcl} |
| 208 |
% \end{proc} |
| 209 |
% |
| 210 |
% |
| 211 |
% \begin{proc}{source} |
| 212 |
% This procedure behaves as a docstripping |source| command: it reads |
| 213 |
% a file, docstrips its contents in memory, and evaluates the result |
| 214 |
% as a \Tcllogo\ script in the context of the caller. The syntax is |
| 215 |
% \begin{quote} |
| 216 |
% |dtx::source| \word{dtx-file} \word{options} |
| 217 |
% \end{quote} |
| 218 |
% where \word{dtx-file} is the file name and \word{options} is the |
| 219 |
% list of options. |
| 220 |
% \begin{tcl} |
| 221 |
proc dtx::source {name options} { |
| 222 |
set F [open $name r] |
| 223 |
set text [read $F] |
| 224 |
close $F |
| 225 |
uplevel 1 [dtx::strip_string $text $options #] |
| 226 |
} |
| 227 |
%</pkg> |
| 228 |
% \end{tcl} |
| 229 |
% \end{proc} |
| 230 |
% |
| 231 |
% |
| 232 |
\endinput |
| 233 |
|
| 234 |
|