PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/haml-3.1.4/lib/haml/html.rb

https://bitbucket.org/mulligan/extractext
Ruby | 412 lines | 312 code | 49 blank | 51 comment | 72 complexity | 897d8abdc60fca91b76b5fe8896c6eea MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-3.0, GPL-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, BSD-2-Clause, JSON
  1. require File.dirname(__FILE__) + '/../haml'
  2. require 'haml/engine'
  3. require 'rubygems'
  4. require 'cgi'
  5. require 'hpricot'
  6. # Haml monkeypatches various Hpricot classes
  7. # to add methods for conversion to Haml.
  8. # @private
  9. module Hpricot
  10. # @see Hpricot
  11. module Node
  12. # Whether this node has already been converted to Haml.
  13. # Only used for text nodes and elements.
  14. #
  15. # @return [Boolean]
  16. attr_accessor :converted_to_haml
  17. # Returns the Haml representation of the given node.
  18. #
  19. # @param tabs [Fixnum] The indentation level of the resulting Haml.
  20. # @option options (see Haml::HTML#initialize)
  21. def to_haml(tabs, options)
  22. return "" if converted_to_haml || to_s.strip.empty?
  23. text = uninterp(self.to_s)
  24. node = next_node
  25. while node.is_a?(::Hpricot::Elem) && node.name == "haml:loud"
  26. node.converted_to_haml = true
  27. text << '#{' <<
  28. CGI.unescapeHTML(node.inner_text).gsub(/\n\s*/, ' ').strip << '}'
  29. if node.next_node.is_a?(::Hpricot::Text)
  30. node = node.next_node
  31. text << uninterp(node.to_s)
  32. node.converted_to_haml = true
  33. end
  34. node = node.next_node
  35. end
  36. return parse_text_with_interpolation(text, tabs)
  37. end
  38. private
  39. def erb_to_interpolation(text, options)
  40. return text unless options[:erb]
  41. text = CGI.escapeHTML(uninterp(text))
  42. %w[<haml:loud> </haml:loud>].each {|str| text.gsub!(CGI.escapeHTML(str), str)}
  43. ::Hpricot::XML(text).children.inject("") do |str, elem|
  44. if elem.is_a?(::Hpricot::Text)
  45. str + CGI.unescapeHTML(elem.to_s)
  46. else # <haml:loud> element
  47. str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}'
  48. end
  49. end
  50. end
  51. def tabulate(tabs)
  52. ' ' * tabs
  53. end
  54. def uninterp(text)
  55. text.gsub('#{', '\#{') #'
  56. end
  57. def attr_hash
  58. attributes.to_hash
  59. end
  60. def parse_text(text, tabs)
  61. parse_text_with_interpolation(uninterp(text), tabs)
  62. end
  63. def parse_text_with_interpolation(text, tabs)
  64. text.strip!
  65. return "" if text.empty?
  66. text.split("\n").map do |line|
  67. line.strip!
  68. "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n"
  69. end.join
  70. end
  71. end
  72. end
  73. # @private
  74. HAML_TAGS = %w[haml:block haml:loud haml:silent]
  75. HAML_TAGS.each do |t|
  76. Hpricot::ElementContent[t] = {}
  77. Hpricot::ElementContent.keys.each do |key|
  78. Hpricot::ElementContent[t][key.hash] = true
  79. end
  80. end
  81. Hpricot::ElementContent.keys.each do |k|
  82. HAML_TAGS.each do |el|
  83. val = Hpricot::ElementContent[k]
  84. val[el.hash] = true if val.is_a?(Hash)
  85. end
  86. end
  87. module Haml
  88. # Converts HTML documents into Haml templates.
  89. # Depends on [Hpricot](http://github.com/whymirror/hpricot) for HTML parsing.
  90. # If ERB conversion is being used, also depends on
  91. # [Erubis](http://www.kuwata-lab.com/erubis) to parse the ERB
  92. # and [ruby_parser](http://parsetree.rubyforge.org/) to parse the Ruby code.
  93. #
  94. # Example usage:
  95. #
  96. # Haml::HTML.new("<a href='http://google.com'>Blat</a>").render
  97. # #=> "%a{:href => 'http://google.com'} Blat"
  98. class HTML
  99. # @param template [String, Hpricot::Node] The HTML template to convert
  100. # @option options :erb [Boolean] (false) Whether or not to parse
  101. # ERB's `<%= %>` and `<% %>` into Haml's `=` and `-`
  102. # @option options :xhtml [Boolean] (false) Whether or not to parse
  103. # the HTML strictly as XHTML
  104. def initialize(template, options = {})
  105. @options = options
  106. if template.is_a? Hpricot::Node
  107. @template = template
  108. else
  109. if template.is_a? IO
  110. template = template.read
  111. end
  112. template = Haml::Util.check_encoding(template) {|msg, line| raise Haml::Error.new(msg, line)}
  113. if @options[:erb]
  114. require 'haml/html/erb'
  115. template = ERB.compile(template)
  116. end
  117. method = @options[:xhtml] ? Hpricot.method(:XML) : method(:Hpricot)
  118. @template = method.call(template.gsub('&', '&amp;'))
  119. end
  120. end
  121. # Processes the document and returns the result as a string
  122. # containing the Haml template.
  123. def render
  124. @template.to_haml(0, @options)
  125. end
  126. alias_method :to_haml, :render
  127. TEXT_REGEXP = /^(\s*).*$/
  128. # @see Hpricot
  129. # @private
  130. class ::Hpricot::Doc
  131. # @see Haml::HTML::Node#to_haml
  132. def to_haml(tabs, options)
  133. (children || []).inject('') {|s, c| s << c.to_haml(0, options)}
  134. end
  135. end
  136. # @see Hpricot
  137. # @private
  138. class ::Hpricot::XMLDecl
  139. # @see Haml::HTML::Node#to_haml
  140. def to_haml(tabs, options)
  141. "#{tabulate(tabs)}!!! XML\n"
  142. end
  143. end
  144. # @see Hpricot
  145. # @private
  146. class ::Hpricot::CData
  147. # @see Haml::HTML::Node#to_haml
  148. def to_haml(tabs, options)
  149. content = parse_text_with_interpolation(
  150. erb_to_interpolation(self.content, options), tabs + 1)
  151. "#{tabulate(tabs)}:cdata\n#{content}"
  152. end
  153. end
  154. # @see Hpricot
  155. # @private
  156. class ::Hpricot::DocType
  157. # @see Haml::HTML::Node#to_haml
  158. def to_haml(tabs, options)
  159. attrs = public_id.nil? ? ["", "", ""] :
  160. public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
  161. raise Haml::SyntaxError.new("Invalid doctype") if attrs == nil
  162. type, version, strictness = attrs.map { |a| a.downcase }
  163. if type == "html"
  164. version = ""
  165. strictness = "strict" if strictness == ""
  166. end
  167. if version == "1.0" || version.empty?
  168. version = nil
  169. end
  170. if strictness == 'transitional' || strictness.empty?
  171. strictness = nil
  172. end
  173. version = " #{version.capitalize}" if version
  174. strictness = " #{strictness.capitalize}" if strictness
  175. "#{tabulate(tabs)}!!!#{version}#{strictness}\n"
  176. end
  177. end
  178. # @see Hpricot
  179. # @private
  180. class ::Hpricot::Comment
  181. # @see Haml::HTML::Node#to_haml
  182. def to_haml(tabs, options)
  183. content = self.content
  184. if content =~ /\A(\[[^\]]+\])>(.*)<!\[endif\]\z/m
  185. condition = $1
  186. content = $2
  187. end
  188. if content.include?("\n")
  189. "#{tabulate(tabs)}/#{condition}\n#{parse_text(content, tabs + 1)}"
  190. else
  191. "#{tabulate(tabs)}/#{condition} #{content.strip}\n"
  192. end
  193. end
  194. end
  195. # @see Hpricot
  196. # @private
  197. class ::Hpricot::Elem
  198. # @see Haml::HTML::Node#to_haml
  199. def to_haml(tabs, options)
  200. return "" if converted_to_haml
  201. if name == "script" &&
  202. (attr_hash['type'].nil? || attr_hash['type'] == "text/javascript") &&
  203. (attr_hash.keys - ['type']).empty?
  204. return to_haml_filter(:javascript, tabs, options)
  205. elsif name == "style" &&
  206. (attr_hash['type'].nil? || attr_hash['type'] == "text/css") &&
  207. (attr_hash.keys - ['type']).empty?
  208. return to_haml_filter(:css, tabs, options)
  209. end
  210. output = tabulate(tabs)
  211. if options[:erb] && name[0...5] == 'haml:'
  212. case name[5..-1]
  213. when "loud"
  214. lines = CGI.unescapeHTML(inner_text).split("\n").
  215. map {|s| s.rstrip}.reject {|s| s.strip.empty?}
  216. lines.first.gsub!(/^[ \t]*/, "= ")
  217. if lines.size > 1 # Multiline script block
  218. # Normalize the indentation so that the last line is the base
  219. indent_str = lines.last[/^[ \t]*/]
  220. indent_re = /^[ \t]{0,#{indent_str.count(" ") + 8 * indent_str.count("\t")}}/
  221. lines.map! {|s| s.gsub!(indent_re, '')}
  222. # Add an extra " " to make it indented relative to "= "
  223. lines[1..-1].each {|s| s.gsub!(/^/, " ")}
  224. # Add | at the end, properly aligned
  225. length = lines.map {|s| s.size}.max + 1
  226. lines.map! {|s| "%#{-length}s|" % s}
  227. if next_sibling && next_sibling.is_a?(Hpricot::Elem) && next_sibling.name == "haml:loud" &&
  228. next_sibling.inner_text.split("\n").reject {|s| s.strip.empty?}.size > 1
  229. lines << "-#"
  230. end
  231. end
  232. return lines.map {|s| output + s + "\n"}.join
  233. when "silent"
  234. return CGI.unescapeHTML(inner_text).split("\n").map do |line|
  235. next "" if line.strip.empty?
  236. "#{output}- #{line.strip}\n"
  237. end.join
  238. when "block"
  239. return render_children("", tabs, options)
  240. end
  241. end
  242. if self.next && self.next.text? && self.next.content =~ /\A[^\s]/
  243. if self.previous.nil? || self.previous.text? &&
  244. (self.previous.content =~ /[^\s]\Z/ ||
  245. self.previous.content =~ /\A\s*\Z/ && self.previous.previous.nil?)
  246. nuke_outer_whitespace = true
  247. else
  248. output << "= succeed #{self.next.content.slice!(/\A[^\s]+/).dump} do\n"
  249. tabs += 1
  250. output << tabulate(tabs)
  251. end
  252. end
  253. output << "%#{name}" unless name == 'div' &&
  254. (static_id?(options) ||
  255. static_classname?(options) &&
  256. attr_hash['class'].split(' ').any?(&method(:haml_css_attr?)))
  257. if attr_hash
  258. if static_id?(options)
  259. output << "##{attr_hash['id']}"
  260. remove_attribute('id')
  261. end
  262. if static_classname?(options)
  263. leftover = attr_hash['class'].split(' ').reject do |c|
  264. next unless haml_css_attr?(c)
  265. output << ".#{c}"
  266. end
  267. remove_attribute('class')
  268. set_attribute('class', leftover.join(' ')) unless leftover.empty?
  269. end
  270. output << haml_attributes(options) if attr_hash.length > 0
  271. end
  272. output << ">" if nuke_outer_whitespace
  273. output << "/" if empty? && !etag
  274. if children && children.size == 1
  275. child = children.first
  276. if child.is_a?(::Hpricot::Text)
  277. if !child.to_s.include?("\n")
  278. text = child.to_haml(tabs + 1, options)
  279. return output + " " + text.lstrip.gsub(/^\\/, '') unless text.chomp.include?("\n")
  280. return output + "\n" + text
  281. elsif ["pre", "textarea"].include?(name) ||
  282. (name == "code" && parent.is_a?(::Hpricot::Elem) && parent.name == "pre")
  283. return output + "\n#{tabulate(tabs + 1)}:preserve\n" +
  284. innerText.gsub(/^/, tabulate(tabs + 2))
  285. end
  286. elsif child.is_a?(::Hpricot::Elem) && child.name == "haml:loud"
  287. return output + child.to_haml(tabs + 1, options).lstrip
  288. end
  289. end
  290. render_children(output + "\n", tabs, options)
  291. end
  292. private
  293. def render_children(so_far, tabs, options)
  294. (self.children || []).inject(so_far) do |output, child|
  295. output + child.to_haml(tabs + 1, options)
  296. end
  297. end
  298. def dynamic_attributes
  299. @dynamic_attributes ||= begin
  300. Haml::Util.map_hash(attr_hash) do |name, value|
  301. next if value.empty?
  302. full_match = nil
  303. ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
  304. full_match = $`.empty? && $'.empty?
  305. CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
  306. end
  307. next if ruby_value == value
  308. [name, full_match ? ruby_value : %("#{ruby_value}")]
  309. end
  310. end
  311. end
  312. def to_haml_filter(filter, tabs, options)
  313. content =
  314. if children.first.is_a?(::Hpricot::CData)
  315. children.first.content
  316. else
  317. CGI.unescapeHTML(self.innerText)
  318. end
  319. content = erb_to_interpolation(content, options)
  320. content.gsub!(/\A\s*\n(\s*)/, '\1')
  321. original_indent = content[/\A(\s*)/, 1]
  322. if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
  323. content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
  324. end
  325. "#{tabulate(tabs)}:#{filter}\n#{content}"
  326. end
  327. def static_attribute?(name, options)
  328. attr_hash[name] && !dynamic_attribute?(name, options)
  329. end
  330. def dynamic_attribute?(name, options)
  331. options[:erb] and dynamic_attributes.key?(name)
  332. end
  333. def static_id?(options)
  334. static_attribute?('id', options) && haml_css_attr?(attr_hash['id'])
  335. end
  336. def static_classname?(options)
  337. static_attribute?('class', options)
  338. end
  339. def haml_css_attr?(attr)
  340. attr =~ /^[-:\w]+$/
  341. end
  342. # Returns a string representation of an attributes hash
  343. # that's prettier than that produced by Hash#inspect
  344. def haml_attributes(options)
  345. attrs = attr_hash.sort.map do |name, value|
  346. value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
  347. name = name.index(/\W/) ? name.inspect : ":#{name}"
  348. "#{name} => #{value}"
  349. end
  350. "{#{attrs.join(', ')}}"
  351. end
  352. end
  353. end
  354. end