Changeset 11580

Show
Ignore:
Timestamp:
05/14/08 15:10:36 (5 years ago)
Author:
hajimehoshi
Message:

Modified document.rb

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • websites/starruby/trunk/lib/document.rb

    r11579 r11580  
    7272 
    7373  def to_html(lang = nil) 
    74     case @type 
    75     when :title 
    76       "" 
    77     when :p 
    78       "<p>#{content(@text)}</p>" 
    79     when :h 
    80       "<h3>#{content(@text)}</h3>" 
    81     when :code 
    82       "<pre class=\"code\"><code>#{content(@text)}</code></pre>" 
    83     when :pre 
    84       "<pre>#{content(@text)}</pre>" 
    85     when :image 
    86       html_lines = [] 
    87       html_lines << "<div class=\"image\">" 
    88       html_lines << "<p class=\"image\">#{content(@text)}</p>" 
    89       html_lines << children_with_lang(lang).map{|c| c.to_html(lang)}.join("\n") 
    90       html_lines << "</div>" 
    91       html_lines.join("\n") 
    92     when :ul 
    93       html_lines = [] 
    94       html_lines << "<ul>" 
    95       html_lines << @text.map{|l| "<li>#{content(l.strip)}</li>"}.join("\n") 
    96       html_lines << "</ul>" 
    97       html_lines.join("\n") 
    98     when :dt 
    99       html_lines = [] 
    100       html_lines << "<dl>" 
    101       html_lines << @text.map{|l| "<dt>#{content(l.strip)}</dt>"}.join("\n") 
    102       if 0 < children_with_lang(lang).size 
    103         html_lines << "<dd>" 
    104         html_lines << children_with_lang(lang).map{|c| c.to_html(lang)}.join("\n") 
    105         html_lines << "</dd>" 
    106       end 
    107       html_lines << "</dl>" 
    108       html_lines.join("\n") 
    109     when :table 
    110       lines = @text.split("\n").map do |line| 
    111         tds = line.split(/(\|)/) 
    112         tds.map!{|s| s.strip} 
    113         tds.delete_if{|s| s.empty?} 
    114         i = 0 
    115         while i < tds.size 
    116           if tds[i][-1].chr == "\\" and tds[i+1] == "|" 
    117             tds[i][-1] = "|" 
    118             if tds[i+2] and tds[i+2] != "|" 
    119               tds[i] += tds[i+2] 
    120               tds.delete_at(i+2) 
    121             end 
    122           end 
    123           i += 1 
    124         end 
    125         tds.delete_if{|s| s == "|"} 
    126         tds 
    127       end 
    128       html_lines = [] 
    129       html_lines << "<table summary=\"#{h(@arg)}\">" 
    130       html_lines << "<thead>" 
    131       html_lines << "<tr>" + lines[0].map{|th| "<th>#{content(th)}</th>"}.join + "</tr>" 
    132       html_lines << "</thead>" 
    133       html_lines << "<tbody>" 
    134       lines[1..-1].each do |line| 
    135         html_lines << "<tr>" + line.map{|td| "<td>#{content(td)}</td>"}.join + "</tr>" 
    136       end 
    137       html_lines << "</tbody>" 
    138       html_lines << "</table>" 
    139       html_lines.join("\n") 
    140     else 
    141       raise "invalid element: #{@type}" 
    142     end 
     74    (@htmls ||= {})[lang] ||= 
     75      (case @type 
     76       when :title 
     77         "" 
     78       when :p 
     79         "<p>#{content(@text)}</p>" 
     80       when :h 
     81         "<h3>#{content(@text)}</h3>" 
     82       when :code 
     83         "<pre class=\"code\"><code>#{content(@text)}</code></pre>" 
     84       when :pre 
     85         "<pre>#{content(@text)}</pre>" 
     86       when :image 
     87         html_lines = [] 
     88         html_lines << "<div class=\"image\">" 
     89         html_lines << "<p class=\"image\">#{content(@text)}</p>" 
     90         html_lines << children_with_lang(lang).map{|c| c.to_html(lang)}.join("\n") 
     91         html_lines << "</div>" 
     92         html_lines.join("\n") 
     93       when :ul 
     94         html_lines = [] 
     95         html_lines << "<ul>" 
     96         html_lines << @text.map{|l| "<li>#{content(l.strip)}</li>"}.join("\n") 
     97         html_lines << "</ul>" 
     98         html_lines.join("\n") 
     99       when :dt 
     100         html_lines = [] 
     101         html_lines << "<dl>" 
     102         html_lines << @text.map{|l| "<dt>#{content(l.strip)}</dt>"}.join("\n") 
     103         if 0 < children_with_lang(lang).size 
     104           html_lines << "<dd>" 
     105           html_lines << children_with_lang(lang).map{|c| c.to_html(lang)}.join("\n") 
     106           html_lines << "</dd>" 
     107         end 
     108         html_lines << "</dl>" 
     109         html_lines.join("\n") 
     110       when :table 
     111         lines = @text.split("\n").map do |line| 
     112           tds = line.split(/(\|)/) 
     113           tds.map!{|s| s.strip} 
     114           tds.delete_if{|s| s.empty?} 
     115           i = 0 
     116           while i < tds.size 
     117             if tds[i][-1].chr == "\\" and tds[i+1] == "|" 
     118               tds[i][-1] = "|" 
     119               if tds[i+2] and tds[i+2] != "|" 
     120                 tds[i] += tds[i+2] 
     121                 tds.delete_at(i+2) 
     122               end 
     123             end 
     124             i += 1 
     125           end 
     126           tds.delete_if{|s| s == "|"} 
     127           tds 
     128         end 
     129         html_lines = [] 
     130         html_lines << "<table summary=\"#{h(@arg)}\">" 
     131         html_lines << "<thead>" 
     132         html_lines << "<tr>" + lines[0].map{|th| "<th>#{content(th)}</th>"}.join + "</tr>" 
     133         html_lines << "</thead>" 
     134         html_lines << "<tbody>" 
     135         lines[1..-1].each do |line| 
     136           html_lines << "<tr>" + line.map{|td| "<td>#{content(td)}</td>"}.join + "</tr>" 
     137         end 
     138         html_lines << "</tbody>" 
     139         html_lines << "</table>" 
     140         html_lines.join("\n") 
     141       else 
     142         raise "invalid element: #{@type}" 
     143       end) 
    143144  end 
    144145