Class: Docgenerator::Rubycode::CodeTest
- Defined in:
- lib/docgenerator/packages/rubycode4doc.rb
Defined Under Namespace
Constant Summary collapse
- @@all =
{}
Class Method Summary collapse
-
.[](name) ⇒ Object
Get the code fragment.
-
.mk_document(title) ⇒ Object
Build a Document with required head-definitions (usepackage…).
Instance Method Summary collapse
-
#<<(c) ⇒ Object
Add one coding line.
-
#code ⇒ Object
Get code.
-
#code_evaluation(max = @codelines.map{|f|f.code.size}.max + 3) ⇒ Object
Get code and results in a verbatim env.
-
#code_evaluation_lstlisting(max = @codelines.map{|f|f.code.size}.max + 3) ⇒ Object
Get code and results in a lstlistings environment.
-
#code_in_tab ⇒ Object
return tabular with code, result and output.
-
#initialize(name, code = '') ⇒ CodeTest
constructor
Get some executable ruby code and analyse it.
-
#output ⇒ Object
Get ‘plain’ output.
-
#sourcecode ⇒ Object
Get ‘plain’ sourcecode.
Constructor Details
#initialize(name, code = '') ⇒ CodeTest
Get some executable ruby code and analyse it. If a name is given, it is stored for later reuse (see CodeTest[])
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 33 def initialize( name, code = '' ) @name = name if @@all[@name] raise "Double definition of CodeTest #{name.inspect}" else @@all[@name] = self end if name #Keep binding for code execution @bind = binding #Collection of all coding lines (elements are Code_line-struct) @codelines = [] code.split(/\n/).each{|c| self << c } end |
Class Method Details
.[](name) ⇒ Object
Get the code fragment
184 185 186 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 184 def self.[](name) @@all[name] end |
.mk_document(title) ⇒ Object
Build a Document with required head-definitions (usepackage…)
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 191 def self.mk_document( title ) doc = Document.new( :title => title ) doc.head << element(:usepackage,{ }, 'listings').cr doc.head << element(:usepackage,{ }, 'color').cr doc.head << element(:lstloadlanguages,{}, 'ruby' ).cr doc.head << element(:lstset, { :language => 'ruby', :commentstyle => '\color{blue}', } ).cr return doc end |
Instance Method Details
#<<(c) ⇒ Object
Add one coding line. -Each line is executed, -The result o fthe line is added to #result -The output is added to #output
Restrictions: -One line, one command (only last command returns its result)
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 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 57 def << ( c ) #fixme: command mit Strings "#... command, comment = c.chomp.split(/#/, 2 ) #~ if comment #~ puts '--'*10 + 'comment' #~ puts c.chomp #~ puts command #~ puts comment.inspect #~ end begin $stdout = output = MyIO.new codeline = Code_line.new( c.chomp, @codelines.size + 1, eval(c, @bind) ) rescue SyntaxError $stdout = STDOUT DOCGENERATOR_LOGGER.error("CodeTest #{@name}: Syntax Error with: '#{c}'") if DOCGENERATOR_LOGGER.error? return end $stdout = STDOUT codeline.outputtab = [] if output.size > 0 codeline.output = output.inspect codeline.outputtab = output.split("\n") end codeline.comment = comment @codelines << codeline end |
#code ⇒ Object
Get code
140 141 142 143 144 145 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 140 def code() element(:lstlisting,{ :numbers => 'left', :numberstyle => '\tiny', :class => 'rubycode', }, @codelines.map{|codeline| codeline.code}.join("\n")).cR end |
#code_evaluation(max = @codelines.map{|f|f.code.size}.max + 3) ⇒ Object
Get code and results in a verbatim env. The comments starts after the column max. (default: longest code line + 3 )
Example:
1 + 1
becomes
1 + 1 #2
fixme: define scope, from-to line
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 111 def code_evaluation( max = @codelines.map{|f|f.code.size}.max + 3 ) evaluated_code = [] @codelines.each{|codeline| #Prepare coding line with result of the line if not done before evaluated_line = "%-#{max}s" % [ codeline.code ] if codeline.result or codeline.output evaluated_line << " # " end if codeline.result evaluated_line << '-> %-10s' % [ codeline.result ] end if codeline.output evaluated_line << '=> %-10s' % [ codeline.output ] end evaluated_code << evaluated_line } evaluated_code.join("\n") end |
#code_evaluation_lstlisting(max = @codelines.map{|f|f.code.size}.max + 3) ⇒ Object
Get code and results in a lstlistings environment.
131 132 133 134 135 136 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 131 def code_evaluation_lstlisting( max = @codelines.map{|f|f.code.size}.max + 3 ) element(:lstlisting,{ :numbers => 'left', :numberstyle => '\tiny', :class => 'rubycode_with_result', }, self.code_evaluation( max) ).cR end |
#code_in_tab ⇒ Object
return tabular with code, result and output.
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 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 149 def code_in_tab() #~ element(:verbatim,{}, @code) #~ puts 'tabularx mag kein verbatim' #~ tabular = element(:tabularx, { #~ :columndescription => 'lXllXX', #~ :width => '\\textwidth', tabular = element(:tabular, { :columndescription => 'lp{3cm}llp{3cm}', :columns => 5, :border => 1, }).Cr tabular << row = element(:row).cr.hline row << element(:col, {}, 'No' ) row << element(:col, {}, 'Code' ) row << element(:col, {}, 'Result' ) row << element(:col, {}, 'Output' ) row << element(:col, {}, 'Output2' ) @codelines.each{|c| tabular << row = element(:row).cr row << element(:col, {}, c.line_number ) if c.code.include?("\n") row << element(:col, {}, element(:lstlisting,{:class => 'rubycode' }, ["\n", c.code]) ).Cr else row << element(:col, {}, element(:lstinline,{:class => 'rubycode' }, c.code) ) end row << element(:col, {}, c.result ) row << element(:col, {}, c.output ? element(:verb,{ :class => 'ruby_result_string' }, c.output) : nil ) #~ row << element(:col, {}, c.outputtab.map{|i| [i, element(:br)]}.flatten[0..-2] ) row << element(:col, {}, element(:verbatim,{:class => 'ruby_result'}, c.outputtab.join("\n") ) ) } tabular end |
#output ⇒ Object
Get ‘plain’ output
92 93 94 95 96 97 98 99 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 92 def output() output = [] @codelines.each{|codeline| #~ output << codeline.output output << codeline.outputtab } output.join("\n") end |
#sourcecode ⇒ Object
Get ‘plain’ sourcecode
84 85 86 87 88 89 90 |
# File 'lib/docgenerator/packages/rubycode4doc.rb', line 84 def sourcecode() code = [] @codelines.each{|codeline| code << codeline.code } code.join("\n") end |