class Parade::Renderers::UpdateImagePaths

UpdateImagePaths is used to ensure the img source paths in the HTML content is properly prefaced with the “image” path as that is necessary for the Sinatra server to properly know that it is to return an image.

Additional processing of the image is provided if RMagick has been installed. Namely it sets the size correctly.

Attributes

root_path[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/parade/renderers/update_image_paths.rb, line 16
def initialize(params = {})
  params.each {|k,v| send("#{k}=",v) if respond_to? "#{k}=" }
end
render(content,options = {}) click to toggle source

@param [String] content HTML content that is parsed for image srcs @param [Hash] options additional parameters, at the moment it is unused.

# File lib/parade/renderers/update_image_paths.rb, line 24
def self.render(content,options = {})
  self.new(options).render(content)
end

Public Instance Methods

get_image_size(path,options={}) click to toggle source
# File lib/parade/renderers/update_image_paths.rb, line 48
def get_image_size(path,options={}) ; end
render(content,options = {}) click to toggle source
# File lib/parade/renderers/update_image_paths.rb, line 28
def render(content,options = {})
  # puts "UpdateImagePaths: #{options}"
  render_root_path = options[:root_path] || root_path || "."

  content.gsub(/img src=["'](?!https?:\/\/)\/?([^\/].*?)["']/) do |image_source|
    image_name = Regexp.last_match(1)

    presentation_path_prefix = options[:presentation_path_prefix].to_s

    html_image_path = File.join(presentation_path_prefix,"/","image",image_name)
    updated_image_source = %{img src="#{html_image_path}"}

    html_asset_path = File.join(render_root_path,image_name)
    width, height = get_image_size(html_asset_path,options)
    updated_image_source << %( width="#{width}" height="#{height}") if width and height

    updated_image_source
  end
end