Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Methods added to this helper will be available to all templates in the application.
- module ApplicationHelper
- def sidebar(&block)
- content_for :sidebar do
- content_tag("div", :id => "sidebar", &block)
- end
- end
- def short_section_url(section)
- "/#{section.number}"
- end
- def p(element)
- if ["informalexample", "example"].include?(element.type)
- code(element).html_safe
- elsif element.type == "figure"
- figure(element).html_safe
- else
- paragraph(element).html_safe
- end
- end
- def code(element)
- output = notes_for(element)
- output << content_tag("pre", :class => "element #{element.type}", :id => "element_#{element["id"]}") do
- lines = element.text.split("\n")
- lines_by_spaces = lines.sort_by { |l| l.blank? ? 100 : l.scan(/^\s*/).join("").length }
- spaces = lines_by_spaces.first.scan(/^\s*/).join("")
- h(lines[0..-2].map { |l| l.gsub(Regexp.new("^#{spaces}"), "") }.join("\n"))
- end.html_safe
- end
- alias_method :parse_example, :code
- alias_method :parse_informalexample, :code
- def notes_for(element)
- params[:action] == "whole" || !element.is_a?(Element) ? "" : "<div class='comment_button' id='element_#{element.id}_note_button'>#{pluralize(element.notes.count + element.sibling_notes_count, "note")} <span class='plus'>+</span> </div>"
- end
- def paragraph(element)
- output = notes_for(element)
- output << content_tag("p", :class => "element #{element.type}", :id => "element_#{element.id}") do
- temp_output = ""
- content_tag("strong") { element.title } if element.title
- Nokogiri::XML(element.text).children.each do |element|
- if element.name == "para"
- temp_output << parse_para(element)
- else
- temp_output << parse_element(element)
- end
- end
- temp_output.lstrip.gsub("\n", "<br>").gsub(/(<br>){2,}/, "<br>").html_safe
- end.html_safe
- end
- def parse_element(element)
- method = /([\w_]*)/.match("parse_#{element.name}")[1]
- if respond_to?(method)
- send(method, element)
- elsif element.name == "#cdata-section"
- parse_cdata(element)
- else
- raise "What is an #{element.name}?"
- end
- end
- def parse_text(element)
- element.text
- end
- def parse_title(element)
- "<strong>#{element.text}</strong>"
- end
- def parse_classname(element)
- "<span class='classname'>#{element.text}</span>"
- end
- def parse_emphasis(element)
- "<em>#{element.text}</em>"
- end
- alias_method :parse_em, :parse_emphasis
- def parse_code(element)
- "<span class='code'>#{h(element.text)}</span>"
- end
- def parse_uri(element)
- auto_link(element.text)
- end
- def parse_command(element)
- "<span class='command'>#{element.text}</span>"
- end
- def parse_filename(element)
- "<span class='filename'>#{element.text}</span>"
- end
- def parse_footnote(element)
- footnote(element.text)
- end
- def parse_blockquote(element)
- person = element.xpath("attribution").text
- "<blockquote>#{parse_para(element.xpath("para"))}<br><span class='attribution'>-- #{person}</span></blockquote>"
- end
- def parse_superscript(element)
- "<sup>#{element.text}</sup>"
- end
- def parse_note(element)
- content_tag(:div, :class => element.name) do
- element.children.map { |c| concat(parse_element(c).html_safe) }
- end
- end
- alias_method :parse_tip, :parse_note
- alias_method :parse_warning, :parse_note
- def parse_cdata(element)
- "<span id='code'>#{h(element.text)}</span>"
- end
- def parse_co(element)
- # OMG: Model code in a helper!
- callout_image(Callout.find_by_ref_2(element["id"]))
- end
- def parse_internalnote(element)
- "<div class='internalnote'>IN: #{element.text}</div>"
- end
- def figure(element)
- notes_for(element) + content_tag(:div, :class => "image") do
- concat("<img src=\"/images/#{element.text}\">".html_safe)
- concat(content_tag(:span, :class => "title" ) { "Figure #{@chapter.position}.x #{element.title}" })
- end
- end
- def callout_image(callout)
- image_tag("callouts/#{callout.number}.png")
- end
- def parse_para(element)
- output = ""
- element.children.each do |child|
- output << parse_element(child)
- end
- output
- end
- alias_method :parse_formalpara, :parse_para
- def parse_itemizedlist(element)
- content_tag :ul do
- for item in element.xpath("listitem/para")
- concat(content_tag("li") { parse_para(item) })
- end
- end
- end
- def parse_table(element)
- output = %Q!<table border="1" cellpadding="7" cellspacing="0">!
- if !element.xpath("tgroup/thead").empty?
- element.xpath("tgroup/thead/row").each do |row|
- output << "<tr>"
- row.xpath("entry").each do |cell|
- output << "<th>#{parse_para(cell)}</th>"
- end
- output << "</tr>"
- end
- end
- if !element.xpath("tgroup/tbody").empty?
- element.xpath("tgroup/tbody/row").each do |row|
- output << "<tr>"
- row.xpath("entry").each do |cell|
- output << "<td>#{parse_para(cell)}</td>"
- end
- output << "</tr>"
- end
- end
- # do the same fot tfoot?
- output << "</table>"
- return output
- end
- def footnote(text)
- @footnotes ||= []
- @footnotes << text
- number = @footnotes.index(text)
- content_tag("sup") { link_to number+1, "#footnote_#{number+1}" }
- end
- def footnotes
- @footnotes ||= []
- content_tag("div", :id => "footnotes") do
- @footnotes.each_with_index do |text, number|
- concat(content_tag("div", :class => "footnote") do
- concat(content_tag("a", :name => "footnote_#{number+1}") do
- content_tag("sup") { (number + 1).to_s }
- end)
- concat(auto_link(text))
- end)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment