Class: HParser::Block::Table

Inherits:
Object
  • Object
show all
Includes:
Collectable
Defined in:
lib/hparser/block/table.rb,
lib/hparser/html.rb,
lib/hparser/text.rb,
lib/hparser/latex.rb,
lib/hparser/hatena.rb

Overview

Table parser.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*rows) ⇒ Table

Returns a new instance of Table.



26
27
28
# File 'lib/hparser/block/table.rb', line 26

def initialize(*rows)
  @rows = rows
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/hparser/block/table.rb', line 10

def rows
  @rows
end

Class Method Details

.parse(scanner, context, inlines) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hparser/block/table.rb', line 12

def self.parse(scanner,context,inlines)
  rows = []
  while scanner.scan(/\A\|/)
    rows.push scanner.matched[1..-1].split('|').select{|l| l[0]}.map{|label|
      if label[0].chr == '*' then
        Th.new inlines.parse(label[1..-1].strip, context)
      else
        Td.new inlines.parse(label.strip, context)
      end
    }
  end
  rows == [] ? nil : Table.new(*rows)
end

Instance Method Details

#==(o) ⇒ Object



30
31
32
# File 'lib/hparser/block/table.rb', line 30

def ==(o)
  o.class == self.class and o.rows == self.rows
end

#each_row(&f) ⇒ Object

:yield: tr



38
39
40
# File 'lib/hparser/block/table.rb', line 38

def each_row(&f) # :yield: tr
  @row.each(&f)
end

#map_row(&f) ⇒ Object

:yield: tr



34
35
36
# File 'lib/hparser/block/table.rb', line 34

def map_row(&f) # :yield: tr
  @rows.map(&f)
end

#row_sizeObject



131
132
133
134
135
# File 'lib/hparser/latex.rb', line 131

def row_size
  self.map_row do |tr|
    return tr.size
  end
end

#to_hatenaObject



79
80
81
82
83
# File 'lib/hparser/hatena.rb', line 79

def to_hatena
  map_row{|row|
    '|'+ row.map{|cell| cell.to_hatena.chomp}.join('|')+'|'
  }.join("\n")+"\n"
end

#to_htmlObject



191
192
193
194
195
# File 'lib/hparser/html.rb', line 191

def to_html
  '<table>'+self.map_row{|tr|
    '<tr>'+tr.map{|cell| cell.to_html }.join + '</tr>'
  }.join+'</table>'
end

#to_latexObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hparser/latex.rb', line 117

def to_latex
  row_size = self.row_size

  output  = "\\begin{table}\n"
  output << "  \\centering\n"
  output << "  \\begin{tabular}{ #{"l " * row_size }}\n"
  self.map_row do |row|
    output << "    #{row.map{|cell| cell.to_latex }.join(" & ")} \\\\\n"
  end
  output << "  \\end{tabular}\n"
  output << "\\end{table}\n"
  output
end

#to_textObject



72
73
74
75
76
# File 'lib/hparser/text.rb', line 72

def to_text
  map_row do |tr|
    '|'+tr.map{|cell| cell.to_text}.join('|')+'|'
  end.join("\n")
end