Guest User

Untitled

a guest
Jul 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. # Methods added to this helper will be available to all templates in the application.
  2. module ApplicationHelper
  3. def sidebar(&block)
  4. content_for :sidebar do
  5. content_tag("div", :id => "sidebar", &block)
  6. end
  7. end
  8.  
  9. def short_section_url(section)
  10. "/#{section.number}"
  11. end
  12.  
  13. def p(element)
  14. if ["informalexample", "example"].include?(element.type)
  15. code(element).html_safe
  16. elsif element.type == "figure"
  17. figure(element).html_safe
  18. else
  19. paragraph(element).html_safe
  20. end
  21. end
  22.  
  23. def code(element)
  24. output = notes_for(element)
  25. output << content_tag("pre", :class => "element #{element.type}", :id => "element_#{element["id"]}") do
  26. lines = element.text.split("\n")
  27. lines_by_spaces = lines.sort_by { |l| l.blank? ? 100 : l.scan(/^\s*/).join("").length }
  28. spaces = lines_by_spaces.first.scan(/^\s*/).join("")
  29. h(lines[0..-2].map { |l| l.gsub(Regexp.new("^#{spaces}"), "") }.join("\n"))
  30. end.html_safe
  31. end
  32.  
  33. alias_method :parse_example, :code
  34. alias_method :parse_informalexample, :code
  35.  
  36. def notes_for(element)
  37. 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>"
  38. end
  39.  
  40. def paragraph(element)
  41. output = notes_for(element)
  42. output << content_tag("p", :class => "element #{element.type}", :id => "element_#{element.id}") do
  43. temp_output = ""
  44. content_tag("strong") { element.title } if element.title
  45. Nokogiri::XML(element.text).children.each do |element|
  46. if element.name == "para"
  47. temp_output << parse_para(element)
  48. else
  49. temp_output << parse_element(element)
  50. end
  51. end
  52. temp_output.lstrip.gsub("\n", "<br>").gsub(/(<br>){2,}/, "<br>").html_safe
  53. end.html_safe
  54. end
  55.  
  56. def parse_element(element)
  57. method = /([\w_]*)/.match("parse_#{element.name}")[1]
  58. if respond_to?(method)
  59. send(method, element)
  60. elsif element.name == "#cdata-section"
  61. parse_cdata(element)
  62. else
  63. raise "What is an #{element.name}?"
  64. end
  65. end
  66.  
  67. def parse_text(element)
  68. element.text
  69. end
  70.  
  71. def parse_title(element)
  72. "<strong>#{element.text}</strong>"
  73. end
  74.  
  75. def parse_classname(element)
  76. "<span class='classname'>#{element.text}</span>"
  77. end
  78.  
  79. def parse_emphasis(element)
  80. "<em>#{element.text}</em>"
  81. end
  82.  
  83. alias_method :parse_em, :parse_emphasis
  84.  
  85. def parse_code(element)
  86. "<span class='code'>#{h(element.text)}</span>"
  87. end
  88.  
  89. def parse_uri(element)
  90. auto_link(element.text)
  91. end
  92.  
  93. def parse_command(element)
  94. "<span class='command'>#{element.text}</span>"
  95. end
  96.  
  97. def parse_filename(element)
  98. "<span class='filename'>#{element.text}</span>"
  99. end
  100.  
  101. def parse_footnote(element)
  102. footnote(element.text)
  103. end
  104.  
  105. def parse_blockquote(element)
  106. person = element.xpath("attribution").text
  107. "<blockquote>#{parse_para(element.xpath("para"))}<br><span class='attribution'>-- #{person}</span></blockquote>"
  108. end
  109.  
  110. def parse_superscript(element)
  111. "<sup>#{element.text}</sup>"
  112. end
  113.  
  114. def parse_note(element)
  115. content_tag(:div, :class => element.name) do
  116. element.children.map { |c| concat(parse_element(c).html_safe) }
  117. end
  118. end
  119.  
  120. alias_method :parse_tip, :parse_note
  121. alias_method :parse_warning, :parse_note
  122.  
  123. def parse_cdata(element)
  124. "<span id='code'>#{h(element.text)}</span>"
  125. end
  126.  
  127. def parse_co(element)
  128. # OMG: Model code in a helper!
  129. callout_image(Callout.find_by_ref_2(element["id"]))
  130. end
  131.  
  132. def parse_internalnote(element)
  133. "<div class='internalnote'>IN: #{element.text}</div>"
  134. end
  135.  
  136. def figure(element)
  137. notes_for(element) + content_tag(:div, :class => "image") do
  138. concat("<img src=\"/images/#{element.text}\">".html_safe)
  139. concat(content_tag(:span, :class => "title" ) { "Figure #{@chapter.position}.x #{element.title}" })
  140. end
  141. end
  142.  
  143. def callout_image(callout)
  144. image_tag("callouts/#{callout.number}.png")
  145. end
  146.  
  147. def parse_para(element)
  148. output = ""
  149. element.children.each do |child|
  150. output << parse_element(child)
  151. end
  152. output
  153. end
  154.  
  155. alias_method :parse_formalpara, :parse_para
  156.  
  157. def parse_itemizedlist(element)
  158. content_tag :ul do
  159. for item in element.xpath("listitem/para")
  160. concat(content_tag("li") { parse_para(item) })
  161. end
  162. end
  163. end
  164.  
  165.  
  166. def parse_table(element)
  167. output = %Q!<table border="1" cellpadding="7" cellspacing="0">!
  168. if !element.xpath("tgroup/thead").empty?
  169. element.xpath("tgroup/thead/row").each do |row|
  170. output << "<tr>"
  171. row.xpath("entry").each do |cell|
  172. output << "<th>#{parse_para(cell)}</th>"
  173. end
  174. output << "</tr>"
  175. end
  176. end
  177. if !element.xpath("tgroup/tbody").empty?
  178. element.xpath("tgroup/tbody/row").each do |row|
  179. output << "<tr>"
  180. row.xpath("entry").each do |cell|
  181. output << "<td>#{parse_para(cell)}</td>"
  182. end
  183. output << "</tr>"
  184. end
  185. end
  186. # do the same fot tfoot?
  187. output << "</table>"
  188. return output
  189. end
  190.  
  191. def footnote(text)
  192. @footnotes ||= []
  193. @footnotes << text
  194. number = @footnotes.index(text)
  195. content_tag("sup") { link_to number+1, "#footnote_#{number+1}" }
  196. end
  197.  
  198. def footnotes
  199. @footnotes ||= []
  200. content_tag("div", :id => "footnotes") do
  201. @footnotes.each_with_index do |text, number|
  202. concat(content_tag("div", :class => "footnote") do
  203. concat(content_tag("a", :name => "footnote_#{number+1}") do
  204. content_tag("sup") { (number + 1).to_s }
  205. end)
  206. concat(auto_link(text))
  207. end)
  208. end
  209. end
  210.  
  211. end
  212.  
  213. end
Add Comment
Please, Sign In to add comment