Index: TODO =================================================================== diff -u -r21a0283e3b67fb56e9dc782b628db602e21c9ac7 -r1617c4f080e984ae2725d8337f994d5e5fa3d4cc --- TODO (.../TODO) (revision 21a0283e3b67fb56e9dc782b628db602e21c9ac7) +++ TODO (.../TODO) (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -3308,13 +3308,11 @@ * all examples are pretty-printed via asciidoc * added example rosetta-abstract-type.tcl * added example rosetta-unknown-method.tcl + * added ./apps/utils/source-doc-beautifier.tcl + * fixed the file-handle output/formatting in rosetta-serialization.tcl; using proc "!" TODO: - - example-scripts: - * add ./apps/utils/source-doc-beautifier.tcl to the repository - * fix the file-handle output/formatting in rosetta-serialization.tcl - - nx: * maybe provide a replacement for -attributes, but without the magic variable. Index: apps/utils/source-doc-beautifier.tcl =================================================================== diff -u --- apps/utils/source-doc-beautifier.tcl (revision 0) +++ apps/utils/source-doc-beautifier.tcl (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -0,0 +1,85 @@ +#!/usr/bin/env tclsh +# +# Script to beautify scripted documentations. +# +# The script receives a nx script as an argument and +# outputs to the same directory a asciidoc scipt. +# +# Gustaf Neumann, Dez 2010 + +package req nx + +nx::Object create output { + set :state "" + set :text "" + :public method line {kind string} { + if {${:state} ne $kind} { + if {${:state} ne ""} {:flush} + set :state $kind + set :text "" + } + append :text $string \n + } + :public method flush {} { + set trimmed [string trim ${:text} \n] + if {$trimmed ne ""} { + :${:state} $trimmed + } + } + :public method postprocess {block} { + set result "" + set cmd "" + foreach l [split $block \n] { + append cmd $l \n + if {[info complete $cmd]} then { + set w0 [lindex $cmd 0] + if { ($w0 eq "?" && [llength $cmd] == 3) || + ($w0 eq "!" && [llength $cmd] == 2) } { + append result "% [lindex $cmd 1]\n" + set cmdresult [lindex $cmd 2] + if {$cmdresult ne "" && ![string match ::nsf::__* $cmdresult]} {append result $cmdresult \n} + } else { + append result $cmd + } + set cmd "" + } + } + return [string trimright $result \n] + } + :public method prog {block} { + puts $::out {[source,tcl]} + puts $::out -------------------------------------------------- + puts $::out [:postprocess $block] + puts $::out --------------------------------------------------\n + } + :public method doc {block} { + #puts $::out "=====doc\n$block\n=====" + puts $::out $block\n + } +} + +#puts stderr "find -L $opt(-path) -type f -name '$opt(-name)'" +foreach file $argv { + set F [open $file]; set content [read $F]; close $F + set outfn [file rootname $file].txt + set out [open $outfn w] + + foreach line { + "package req nx::test" + "package require nx::test" + "nx::Test parameter count 1" + "proc ! args.*?" + } { + regsub "$line\s?\n" $content "" content + } + + puts $::out "= Listing of $file\n" + foreach line [split $content \n] { + if {[regexp {^# ?(.*)$} $line _ comment]} { + output line doc $comment + } else { + output line prog $line + } + } + output flush +} \ No newline at end of file Index: doc/example-scripts/rosetta-serialization.html =================================================================== diff -u -r5693145107c55b5f64bf0fb487aa43e0f2238f1a -r1617c4f080e984ae2725d8337f994d5e5fa3d4cc --- doc/example-scripts/rosetta-serialization.html (.../rosetta-serialization.html) (revision 5693145107c55b5f64bf0fb487aa43e0f2238f1a) +++ doc/example-scripts/rosetta-serialization.html (.../rosetta-serialization.html) (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -790,7 +790,6 @@ .nx-variable {color: #AF663F; font-weight: normal; font-style: normal;}
% set f [open /tmp/dump w]
-file5
 % foreach i [Animal info instances] { puts $f [$i serialize] }
 % close $f

Destroy all animal instances:

@@ -854,7 +853,7 @@

Index: doc/example-scripts/rosetta-serialization.tcl =================================================================== diff -u -r21a0283e3b67fb56e9dc782b628db602e21c9ac7 -r1617c4f080e984ae2725d8337f994d5e5fa3d4cc --- doc/example-scripts/rosetta-serialization.tcl (.../rosetta-serialization.tcl) (revision 21a0283e3b67fb56e9dc782b628db602e21c9ac7) +++ doc/example-scripts/rosetta-serialization.tcl (.../rosetta-serialization.tcl) (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -4,6 +4,7 @@ # package req nx package req nx::test +proc ! args {uplevel {*}$args} package req nx::serializer nx::Class create Being { @@ -32,7 +33,7 @@ # +i am Fido alive true+ # # Serialize the animals to a file -set f [open /tmp/dump w] +! {set f [open /tmp/dump w]} ? {foreach i [Animal info instances] { puts $f [$i serialize] }} "" ? {close $f} "" Index: doc/example-scripts/rosetta-unknown-method.html =================================================================== diff -u --- doc/example-scripts/rosetta-unknown-method.html (revision 0) +++ doc/example-scripts/rosetta-unknown-method.html (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -0,0 +1,797 @@ + + + + + +Listing of doc/example-scripts/rosetta-unknown-method.tcl + + + + + +
+
+

Rosetta Example: Respond to an unknown method call

+
+ +
+
+
package req nx
+

Modelled after the Python version:

+
+
+
nx::Class create Example {
+
+  :public method foo {} {return "This is foo."}
+  :public method bar {} {return "This is bar."}
+
+  :method unknown {method args} {
+    set result "Tried to handle unknown method '$method'."
+    if {[llength $args] > 0} {
+      append result " It had arguments '$args'."
+    }
+    return $result
+  }
+}
+
+

Demonstrating the behavior in a shell:

+

Create an instance of Example

+
+
+
% set e [Example new]
+
+% $e foo
+This is foo.
+
+% $e bar
+This is bar.
+
+% $e grill
+Tried to handle unknown method 'grill'.
+
+% $e ding dong
+Tried to handle unknown method 'ding'. It had arguments 'dong'.
+
+
+
+
+

+ + + Index: doc/example-scripts/rosetta-unknown-method.tcl =================================================================== diff -u -r21a0283e3b67fb56e9dc782b628db602e21c9ac7 -r1617c4f080e984ae2725d8337f994d5e5fa3d4cc --- doc/example-scripts/rosetta-unknown-method.tcl (.../rosetta-unknown-method.tcl) (revision 21a0283e3b67fb56e9dc782b628db602e21c9ac7) +++ doc/example-scripts/rosetta-unknown-method.tcl (.../rosetta-unknown-method.tcl) (revision 1617c4f080e984ae2725d8337f994d5e5fa3d4cc) @@ -6,13 +6,13 @@ package req nx::test # -# Modelled after the Python version +# Modelled after the Python version: # nx::Class create Example { - :public method foo {} {return "This is foo"} - :public method bar {} {return "This is bar"} + :public method foo {} {return "This is foo."} + :public method bar {} {return "This is bar."} :method unknown {method args} { set result "Tried to handle unknown method '$method'." @@ -28,7 +28,10 @@ # Create an instance of Example ? {set e [Example new]} "::nsf::__#0" -? {$e foo} "This is foo" -? {$e bar} "This is bar" +? {$e foo} "This is foo." + +? {$e bar} "This is bar." + ? {$e grill} "Tried to handle unknown method 'grill'." + ? {$e ding dong} "Tried to handle unknown method 'ding'. It had arguments 'dong'."