Class: Slaw::ZA::Act::Table

Inherits:
Treetop::Runtime::SyntaxNode
  • Object
show all
Defined in:
lib/slaw/za/act_nodes.rb

Instance Method Summary collapse

Instance Method Details

#to_xml(b, idprefix, i = 0) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/slaw/za/act_nodes.rb', line 370

def to_xml(b, idprefix, i=0)
  # parse the table using wikicloth
  # strip whitespace at the start of lines, to avoid wikicloth from treating it as PRE
  text = self.text_value.strip.gsub(/^[ \t]+/, '')
  html = WikiCloth::Parser.new({data: text}).to_html

  # we need to strip any surrounding p tags and add
  # an id to the table
  html = Nokogiri::HTML(html)
  table = html.css("table").first
  table['id'] = "#{idprefix}table#{i}"

  # wrap td and th content in p tags
  table.css("td, th").each do |cell|
    p = Nokogiri::XML::Node.new("p", html)
    p.children = cell.children
    p.parent = cell

    # replace newlines with <eol>
    p.search("text()").each do |text|
      lines = text.content.strip.split(/\n/)
      text.content = lines.shift

      for line in lines
        eol = text.add_next_sibling(Nokogiri::XML::Node.new("eol", html))
        text = eol.add_next_sibling(Nokogiri::XML::Text.new(line, html))
      end
    end
  end

  table.xpath('//text()[1]').each{ |t|      t.content = t.content.lstrip }
  table.xpath('//text()[last()]').each{ |t| t.content = t.content.rstrip }

  b.parent << table
end