Tk Library Source Code

Artifact [bd45aa4eac]
Login

Artifact bd45aa4eacf03fcaeacfb80d4b2cd519657c13ae:

Attachment "e.diff" to ticket [492156ffff] added by andreas_kupries 2001-12-14 03:09:35.
? modules/fileinput
? modules/doctools.tgz
? modules/doctools.tar.gz
? modules/doctools
? modules/doctools.tar.bz2
? modules/ftp/example
? modules/ftpd/examples
Index: modules/textutil/ChangeLog
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/ChangeLog,v
retrieving revision 1.16
diff -u -r1.16 ChangeLog
--- modules/textutil/ChangeLog	2001/12/11 21:49:42	1.16
+++ modules/textutil/ChangeLog	2001/12/13 20:07:11
@@ -1,3 +1,15 @@
+2001-12-12  Andreas Kupries  <[email protected]>
+
+	* expander.n:
+	* expander.ehtml:
+	* expander.html:
+	* expander.tcl: Added 'textcmd' method which is called for all
+	  plain text encountered by the processor. Note: The textcmd is
+	  run through the evalcmd, i.e. it is treated as a special macro
+	  surrounding all plain text. It defaults to empty, meaning
+	  'identity'. Also moved the code handling errors in a macro into
+	  a separate function to make usage in multiple places easier.
+
 2001-12-11  Andreas Kupries  <[email protected]>
 
 	* textutil.n:
@@ -15,7 +27,7 @@
 	* expander.n:
 	* expander.ehtml:
 	* expander.html:
-	* expander_notes.txt: Applied update I got from William
+	* expander_notes.txt: Applied update on behalf of William
 	  implementing the 'evalcmd' feature, i.e. instead of using
 	  'uplevel #0' directly expander objects now have a configurable
 	  callback for the execution of macros. The default is still
Index: modules/textutil/expander.ehtml
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/expander.ehtml,v
retrieving revision 1.2
diff -u -r1.2 expander.ehtml
--- modules/textutil/expander.ehtml	2001/12/11 05:54:21	1.2
+++ modules/textutil/expander.ehtml	2001/12/13 20:07:11
@@ -336,9 +336,22 @@
        bracket definitions.  By default, the brackets are "[lb]" and
        "[rb]", but any non-empty string can be used; for example,
        "<" and ">" or "(*" and "*)" or even "Hello," and "World!".<p>
-<p>
-</dl>
 
+  <dt> [commanddef textcmd ?<i>newTextCmd</i>?]
+  <dd> Returns the current command for processing polain text, which
+       defaults to the empty string, meaning <i>identity</i>. If
+       specified, <i>newTextCmd</i> will be saved for future use and
+       then returned; it must be a Tcl command expecting one
+       additional argument: the text to process. The expander object
+       will this command for all plain text it encounters, giving the
+       user of the object the ability to process all plain text in
+       some standard way before writing it to the output. The object
+       expects that the command returns the processed plain text.<p>
+       <b>Note</b> that the combination of <i>textcmd plaintext</i> is
+       run through the <i>evalcmd</i> for the actual evaluation. In
+       other words, the <i>textcmd</i> is treated as a special macro
+       implicitly surrounding all plain text in the template.<p>
+</dl>
 
 [section "HISTORY"]
 
Index: modules/textutil/expander.html
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/expander.html,v
retrieving revision 1.2
diff -u -r1.2 expander.html
--- modules/textutil/expander.html	2001/12/11 05:54:21	1.2
+++ modules/textutil/expander.html	2001/12/13 20:07:11
@@ -334,7 +334,21 @@
        bracket definitions.  By default, the brackets are "[" and
        "]", but any non-empty string can be used; for example,
        "<" and ">" or "(*" and "*)" or even "Hello," and "World!".<p>
-<p>
+
+  <dt> <code><a name="textcmd">textcmd ?<i>newTextCmd</i>?]</a></code>
+  <dd> Returns the current command for processing polain text, which
+       defaults to the empty string, meaning <i>identity</i>. If
+       specified, <i>newTextCmd</i> will be saved for future use and
+       then returned; it must be a Tcl command expecting one
+       additional argument: the text to process. The expander object
+       will this command for all plain text it encounters, giving the
+       user of the object the ability to process all plain text in
+       some standard way before writing it to the output. The object
+       expects that the command returns the processed plain text.<p>
+       <b>Note</b> that the combination of <i>textcmd plaintext</i> is
+       run through the <i>evalcmd</i> for the actual evaluation. In
+       other words, the <i>textcmd</i> is treated as a special macro
+       implicitly surrounding all plain text in the template.<p>
 </dl>
 
 
Index: modules/textutil/expander.n
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/expander.n,v
retrieving revision 1.2
diff -u -r1.2 expander.n
--- modules/textutil/expander.n	2001/12/11 05:54:21	1.2
+++ modules/textutil/expander.n	2001/12/13 20:07:12
@@ -178,6 +178,21 @@
 definitions.  By default, the brackets are \fB[\fR and \fB]\fR, but
 any non-empty string can be used; for example, \fB<\fR and \fB>\fR or
 \fB(*\fR and \fB*)\fR or even \fBHello,\fR and \fBWorld!\fR.
+.TP
+\fIexpanderName\fR \fBtextcmd\fR ?\fInewTxtCmd\fR?
+Returns the current command for processing polain text, which defaults
+to the empty string, meaning \fIidentity\fR. If specified,
+\fInewTextCmd\fR will be saved for future use and then returned; it
+must be a Tcl command expecting one additional argument: the text to
+process. The expander object will this command for all plain text it
+encounters, giving the user of the object the ability to process all
+plain text in some standard way before writing it to the output. The
+object expects that the command returns the processed plain text.
+.sp
+\fBNote\fR that the combination of \fItextcmd plaintext\fR is run through
+the \fIevalcmd\fR for the actual evaluation. In other words, the
+\fItextcmd\fR is treated as a special macro implicitly surrounding all
+plain text in the template.
 .SH TUTORIAL
 .PP
 To begin, create an expander object:
Index: modules/textutil/expander.tcl
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/expander.tcl,v
retrieving revision 1.2
diff -u -r1.2 expander.tcl
--- modules/textutil/expander.tcl	2001/12/11 05:54:21	1.2
+++ modules/textutil/expander.tcl	2001/12/13 20:07:12
@@ -36,6 +36,7 @@
 	# errmode	    How to handle macro errors: 
 	#		    nothing, macro, error, fail.
         # evalcmd           The evaluation command.
+	# textcmd           The plain text processing command.
 	# level		    The context level
 	# output-$level     The accumulated text at this context level.
 	# name-$level       The tag name of this context level
@@ -138,6 +139,7 @@
         setbrackets -
         errmode -
         evalcmd -
+	textcmd -
         cpush -
         cis -
         cname -
@@ -361,6 +363,31 @@
 
 #---------------------------------------------------------------------
 # FUNCTION:
+# 	Op_textcmd ?newTextCmd?
+#
+# INPUTS:
+#	newTextCmd		If given, the new text command.
+#
+# RETURNS:
+#	The current text command
+#
+# DESCRIPTION:
+#	Returns the current text command.  This is the command used to
+#	process plain text. It defaults to {}, meaning identity.
+
+proc ::textutil::expander::Op_textcmd {name args} {
+    switch -exact [llength $args] {
+	0 {}
+	1 {Set textcmd [lindex $args 0]}
+	default {
+	    return -code error "wrong#args for textcmd: name ?newTextcmd?"
+	}
+    }
+    return [Get textcmd]
+}
+
+#---------------------------------------------------------------------
+# FUNCTION:
 # 	Op_reset
 #
 # INPUTS:
@@ -383,6 +410,7 @@
     set Info($name-rb) "\]"
     set Info($name-errmode) "fail"
     set Info($name-evalcmd) "uplevel #0"
+    set Info($name-textcmd) ""
     set Info($name-level) 0
     set Info($name-output-0) ""
     set Info($name-name-0) ":0"
@@ -610,6 +638,16 @@
         # FIRST, If there was plain text, append it to the output, and 
         # continue.
         if {$plainText != ""} {
+	    set tc [Get textcmd]
+	    if {[string length $tc] > 0} {
+		lappend tc $plainText
+
+		if {![catch "[Get evalcmd] [list $tc]" result]} {
+		    set plainText $result
+		} else {
+		    HandleError $name {plain text} $tc $result
+		}
+	    }
             Op_cappend $name $plainText
             if {[string length $inputString] == 0} {
                 break
@@ -628,30 +666,52 @@
             continue
         } 
 
-        switch [Get errmode] {
-            nothing { }
-            macro { 
-                Op_cappend $name "[Get lb]$macro[Get rb]" 
-            }
-            error {
-                Op_cappend $name "\n=================================\n"
-                Op_cappend $name "*** Error in macro:\n"
-                Op_cappend $name "*** [Get lb]$macro[Get rb]\n--> $result\n"
-                Op_cappend $name "=================================\n"
-            }
-            fail   { 
-                error "Error in macro:\n[Get lb]$macro[Get rb]\n--> $result"
-            }
-            default {
-                error "Unknown error mode: [Get errmode]"
-            }
-        }
+	HandleError $name macro $macro $result
     }
     
     Op_lb $name [Op_cget $name lb]
     Op_rb $name [Op_cget $name rb]
 
     return [Op_cpop $name expand]
+}
+
+#---------------------------------------------------------------------
+# FUNCTION
+#	HandleError name title command errmsg
+#
+# INPUTS:
+#	name		The name of the expander object in question.
+#	title		A title text
+#	command		The command which caused the error.
+#	errmsg		The error message to report
+#
+# RETURNS:
+#	Nothing
+#
+# DESCRIPTIONS
+#	Is executed when an error in a macro or the plain text handler
+#	occurs. Generates an error message according to the current
+#	error mode.
+
+proc ::textutil::expander::HandleError {name title command errmsg} {
+    switch [Get errmode] {
+	nothing { }
+	macro { 
+	    Op_cappend $name "[Get lb]$command[Get rb]" 
+	}
+	error {
+	    Op_cappend $name "\n=================================\n"
+	    Op_cappend $name "*** Error in $title:\n"
+	    Op_cappend $name "*** [Get lb]$command[Get rb]\n--> $errmsg\n"
+	    Op_cappend $name "=================================\n"
+	}
+	fail   { 
+	    return -code error "Error in $title:\n[Get lb]$command[Get rb]\n--> $errmsg"
+	}
+	default {
+	    return -code error "Unknown error mode: [Get errmode]"
+	}
+    }
 }
 
 #---------------------------------------------------------------------
Index: modules/textutil/expander.test
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/expander.test,v
retrieving revision 1.3
diff -u -r1.3 expander.test
--- modules/textutil/expander.test	2001/12/11 05:54:21	1.3
+++ modules/textutil/expander.test	2001/12/13 20:07:12
@@ -328,6 +328,21 @@
 } {{uplevel #0} identity {+++Bogus Macro+++}}
 
 #---------------------------------------------------------------------
+# Test cases 5.x: Replacing the textcmd.
+
+proc count {text} {
+    return [string length $text]
+}
+
+test expander-6.1 {new evalcmd} {} {
+    catch {::textutil::expander exp}
+    exp reset
+    set oldcmd [exp textcmd]
+    exp textcmd count
+    list $oldcmd [exp textcmd] [exp expand {++++++}]
+} {{} count 6}
+
+#---------------------------------------------------------------------
 # Clean up
 
 ::tcltest::cleanupTests