Module: Spreadsheetkit::StyleParser
- Included in:
- Base
- Defined in:
- lib/spreadsheetkit/style_parser.rb
Instance Method Summary collapse
- #absolutize_image_sources(document) ⇒ Object
-
#absolutize_url(url, base_path = '') ⇒ Object
Absolutize URL (Absolutize? Seriously?).
- #css_parser(html) ⇒ Object
- #parse_html(html) ⇒ Object
-
#update_image_urls(style) ⇒ Object
Update image urls.
Instance Method Details
#absolutize_image_sources(document) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/spreadsheetkit/style_parser.rb', line 63 def absolutize_image_sources(document) document.css('img').each do |img| src = img['src'] img['src'] = src.gsub(src, absolutize_url(src)) end document end |
#absolutize_url(url, base_path = '') ⇒ Object
Absolutize URL (Absolutize? Seriously?)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/spreadsheetkit/style_parser.rb', line 73 def absolutize_url(url, base_path = '') ############# Refactor # original_url = url #unless original_url[URI::regexp(%w[http https])] # protocol = default_url_options[:protocol] # protocol = "http://" if protocol.blank? # protocol+= "://" unless protocol.include?("://") # host = default_url_options[:host] # [host,protocol].each{|r| original_url.gsub!(r,"") } # host = protocol+host unless host[URI::regexp(%w[http https])] # url = URI.join host, base_path, original_url #end #url.to_s url end |
#css_parser(html) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/spreadsheetkit/style_parser.rb', line 52 def css_parser(html) parser = CssParser::Parser.new html.xpath("//head/link[@type='text/css']" ).each do |style| parser.load_uri! "#{Rails.root}/public#{style[:href]}" end parser end |
#parse_html(html) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spreadsheetkit/style_parser.rb', line 7 def parse_html(html) # Parse original html html_document = absolutize_image_sources(html) # Write inline styles element_styles = {} css_parser(html_document).each_selector do |selector, declaration, specificity| next if selector.include?(':') html_document.css(selector).each do |element| declaration.to_s.split(';').each do |style| # Split style in attribute and value attribute, value = style.split(':').map(&:strip) # Set element style defaults element_styles[element] ||= {} element_styles[element][attribute] ||= { :specificity => 0, :value => '' } # Update attribute value if specificity is higher than previous values if element_styles[element][attribute][:specificity] <= specificity element_styles[element][attribute] = { :specificity => specificity, :value => value } end end end end # Loop through element styles element_styles.each_pair do |element, attributes| # Elements current styles current_style = element['style'].to_s.split(';').sort # Elements new styles new_style = attributes.map{|attribute, style| "#{attribute}: #{update_image_urls(style[:value])}"} # Concat styles style = (current_style + new_style).compact.uniq.map(&:strip).sort # Set new styles element['style'] = style.join(';') end # Return HTML html_document end |
#update_image_urls(style) ⇒ Object
Update image urls
95 96 97 98 99 100 101 102 103 |
# File 'lib/spreadsheetkit/style_parser.rb', line 95 def update_image_urls(style) ############# Refactor #if default_url_options[:host].present? # Replace urls in stylesheets # style.gsub!($1, absolutize_url($1, 'stylesheets')) if style[/url\(['"]?(.*?)['"]?\)/i] #end style end |