Class: Docgenerator::Tables::Row

Inherits:
Element show all
Defined in:
lib/docgenerator/tabular.rb

Overview

May only be used inside a tabular. Tabular must set the columns-value.

If not, zero columns is expected and an error is thrown. In this cases you can mock a columns definition via an attribute. There si now check, if the row is added later to a tabular with the same size.

You can add rows in an array.

Constant Summary

Constants inherited from Element

Element::SUPPORTED_TARGETS

Constants included from Docgenerator

ATTR_LANG, CSS_BORDER, CSS_COLORS, CSS_WIDTH, DOCGENERATOR_DEFAULT_LOGGER, DOCGENERATOR_LOGGER, ENDTAG, HTML_ATTR_ALIGN, HTML_ATTR_ALL, HTML_ATTR_CORE, HTML_ATTR_EVENTS, HTML_ATTR_I18N, HTML_ATTR_VALIGN, VERSION

Instance Attribute Summary collapse

Attributes inherited from Element

#attr, #content, #log

Instance Method Summary collapse

Methods inherited from Element

#CR, #Cr, #[], add, add_attribute, add_attributes, add_context_output, add_creole_output, add_html_output, add_html_tag, add_id, add_latex_output, add_output, add_text_output, add_wiki_output, attributes, #cR, #content?, #cr, create, create_convert_to_code, #delete, element_ids, #element_ids, #empty?, get, get_attribute_list, has_no_content, #ids, inherited, #insert, #insertafter, #insertbefore, #inspect_creation_caller, #linebreak, overview, #prepare_tracing_info, #restrict_to, #texkeyval, #texoptional, #to_context, #to_doc, #to_s, trace=

Methods included from Docgenerator

#set_option_defaults, set_option_defaults, trace_off, trace_on, trace_on?

Constructor Details

#initialize(attr = {}, content = nil) ⇒ Row

Returns a new instance of Row.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/docgenerator/tabular.rb', line 232

def initialize( attr={}, content = nil)
  local_content = content #store for later analyze
  content = nil   #default Element will give an error -> remove content
  super
  #For Ruby 1.9: Initialize instance variable 
  @toprule = false
  @midrule = false
  @bottomrule = false
  @endfirsthead = false
  @endhead = false
  @endfoot = false
  @endlastfoot = false
  @cline = false
  @cmidrule = false
  @hline = false
  @columns = attr[:columns] #if set, we can suppress error messages 
  case local_content
    when nil  #ok, no content
    when Array
      local_content.each{|col| self << col }
    else
      @log.error("Row allows no %s as parameter" % local_content.class)
  end        
end

Instance Attribute Details

#columnsObject

Set from tabular or by attribute columns



257
258
259
# File 'lib/docgenerator/tabular.rb', line 257

def columns
  @columns
end

Instance Method Details

#<<(column) ⇒ Object

Only Colums are allowed to be added. In combination with longtable, captions are also allowed.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/docgenerator/tabular.rb', line 294

def << ( column )
  #Check on allowed elements inside a row
  allowed = false
  [ :col, :multicolumn, :caption].each{|el| 
    if column.is_a?( Element.get(el) )
      allowed = true
      break
    end
  }
  if ! allowed
    @log.warn("Add non-column to tabular (#{column.class})") if @log.warn?
  end
  @content << column
  #Does not work with multicolumns.
  if !@columns
    @log.warn("Add row to tabular with undefined columns number (#{column.inspect})" ) if @log.warn?
  elsif @content.size > @columns 
    @log.warn("Tabular with #{@columns} columns get #{@content.size} columns" ) if @log.warn?
  end
end

#bottomruleObject

Bottomrule requires booktabs.sty



283
# File 'lib/docgenerator/tabular.rb', line 283

def bottomrule(); 	@bottomrule	= element(:bottomrule);	self; end

#cline(val) ⇒ Object

Set line sequences under the fields



261
262
263
264
265
266
267
268
# File 'lib/docgenerator/tabular.rb', line 261

def cline( val )
  @cline = [] if ! defined?( @cline )
  if ! ( /\A\d+-\d+\z/  =~ val )
    @log.warn("\\cline with wrong parameter: #{val.inspect}") if @log.warn?
  end
  @cline	<< val
  return self
end

#cmidrule(val) ⇒ Object



273
274
275
276
277
278
279
280
# File 'lib/docgenerator/tabular.rb', line 273

def cmidrule( val )
  @cmidrule = [] if ! defined?( @cmidrule )
  if ! ( /\A\d+-\d+\z/  =~ val )
    @log.warn("\\cmidrule with wrong parameter: #{val.inspect}") if @log.warn?
  end
  @cmidrule	<< val
  return self
end

#endfirstheadObject

Only inside a longtable



289
# File 'lib/docgenerator/tabular.rb', line 289

def endfirsthead();	@endfirsthead	= element(:endfirsthead);	self; end

#endfootObject

Only inside a longtable



287
# File 'lib/docgenerator/tabular.rb', line 287

def endfoot();		@endfoot		= element(:endfoot);		self; end

#endheadObject

Only inside a longtable



285
# File 'lib/docgenerator/tabular.rb', line 285

def endhead();	@endhead		= element(:endhead);	self; end

#endlastfootObject

Only inside a longtable



291
# File 'lib/docgenerator/tabular.rb', line 291

def endlastfoot();	@endlastfoot	= element(:endlastfoot);	self; end

#hlineObject

Set a line after the row.



259
# File 'lib/docgenerator/tabular.rb', line 259

def hline();		@hline		= element(:hline);		self; end

#htmltagObject



340
341
342
# File 'lib/docgenerator/tabular.rb', line 340

def htmltag()
  return 'tr'
end

#inspectObject



315
316
317
# File 'lib/docgenerator/tabular.rb', line 315

def inspect()
  return "<Class Row columns=#{@content.size}#{inspect_creation_caller}>"
end

#midruleObject

Midrule requires booktabs.sty



272
# File 'lib/docgenerator/tabular.rb', line 272

def midrule();		@midrule		= element(:midrule);		self; end

#to_creole(options = {}) ⇒ Object



384
# File 'lib/docgenerator/tabular.rb', line 384

def to_creole(options={}); return "\n|-\n#{@content.to_creole(options).strip}"; end

#to_html(options = {}) ⇒ Object



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
# File 'lib/docgenerator/tabular.rb', line 343

def to_html(options={})
  o = set_option_defaults(options)
  o[:log].debug("enter to_html for row") if o[:log].debug?
  cmd	=	''
  cmd	<<	"\n" if @crbefore
  cmd	<< '<tr '
  css = @attr[:style].content[0]
  o[:log].error( "docgenerator_tabular: style is no CSS #{css.inspect}" ) if css and ! css.is_a?(CSS) and o[:log].error?
  @attr[:style] << css = CSS.new if ! css		
  
  #Doesn't work. Style sheet setting in tr doesn't go to td
  #See http://stackoverflow.com/questions/1384823/how-to-specify-the-bottom-border-of-a-tr, comment of deceze 
  #  You can't style a <tr>, as it's only a "logical grouping element", but it's not actually rendered. 
  #   You can only style the <td> or <th> elements inside the <tr>
  if @hline
    css[:border_bottom_style]	= 'solid'  
    css[:border_bottom_width] = 'medium'  
  end
  
  # cmd	<< @toprule.to_s	if @toprule
  # cmd	<< @midrule.to_s	if @midrule
  # cmd	<< @bottomrule.to_s	if @bottomrule
  # @cline.each{|cline|	cmd	<< "\\cline{#{cline}}"	} if @cline
  # cmd	<< "[#{@attr[:add_vspace].content.to_s}]" if @attr[:add_vspace].content.size > 0
  
  # cmd	<< @endfirsthead.to_s	if @endfirsthead
  # cmd	<< @endhead.to_s	if @endhead
  # cmd	<< @endfoot.to_s	if @endfoot
  # cmd	<< @endlastfoot.to_s	if @endlastfoot		
  @attr.sort_by{|k,v|	 v.sortkey }.each{|k,v|
    cmd << "#{k} = \"#{v}\" " if v.to_s != '' and v.html?
  }
  cmd	<< '>'
  cmd	<< "#{@content.to_html(o)}"
  cmd	<< "\n" if @crmid
  cmd	<< '</tr>'
  cmd	<<	"\n" if @crafter
  return cmd
end

#to_latex(options = {}) ⇒ Object

Each cell is separated by ‘&’.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/docgenerator/tabular.rb', line 319

def to_latex(options={})
  o = set_option_defaults(options)
  o[:log].debug("enter to_latex for row") if o[:log].debug?
  cmd	=	''
  cmd	<<	"\n" if @crbefore
  cmd	<< @content.map{|cell| cell.to_latex(o)}.join(' & ') 
  cmd << Regexp.escape('\\')  #escape needed to keep the double \\
  cmd	<< "[#{@attr[:add_vspace].content.join}]" if @attr[:add_vspace].content.size > 0
  cmd	<< @hline.to_latex(o)		if @hline
  cmd	<< @toprule.to_latex(o)	if @toprule
  cmd	<< @midrule.to_latex(o)	if @midrule
  cmd	<< @bottomrule.to_latex(o)	if @bottomrule
  cmd	<< @endfirsthead.to_latex(o)	if @endfirsthead
  cmd	<< @endhead.to_latex(o)	if @endhead
  cmd	<< @endfoot.to_latex(o)	if @endfoot
  cmd	<< @endlastfoot.to_latex(o)	if @endlastfoot		
  @cline.each{|cline|	cmd	<< "\\cline{#{cline}}"	} if @cline
  @cmidrule.each{|cmidrule|	cmd	<< "\\cmidrule{#{cmidrule}}"	} if @cmidrule
  cmd	<<	"\n" if @crafter
  return cmd
end

#to_text(options = {}) ⇒ Object



382
# File 'lib/docgenerator/tabular.rb', line 382

def to_text(options={}); return "#{@content.to_text(options).strip}\n"; end

#to_wiki(options = {}) ⇒ Object



383
# File 'lib/docgenerator/tabular.rb', line 383

def to_wiki(options={}); return "\n|-\n#{@content.to_wiki(options).strip}"; end

#topruleObject

Toprule requires booktabs.sty



270
# File 'lib/docgenerator/tabular.rb', line 270

def toprule();		@toprule		= element(:toprule);		self; end