Class: Kramdown::Converter::Preview

Inherits:
Html
  • Object
show all
Defined in:
lib/services/kramdown_service.rb

Overview

A custom kramdown HTML converter for getting the HTML preview for a post

Instance Method Summary collapse

Instance Method Details

#convert_img(element, indent) ⇒ Object

An override of the convert_img tag which converts all image sources to pull from the CarrierWare cache location if an uploader exists with the image’s filename. Or the Base64 contents of a downloaded image are replaced in the src attribute if the image was downloaded for the post

Params: el::the image element to convert to html indent::the indent of the HTML



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/services/kramdown_service.rb', line 21

def convert_img(element, indent)
  formatted_filename = File.basename(element.attr['src']).tr(' ', '_')
  uploader = PostImageManager.instance.uploaders.find { |x| x.filename == formatted_filename }
  if uploader
    element.attr['src'] = "/uploads/tmp/#{uploader.preview.cache_name}"
  else
    downloaded_image = PostImageManager.instance.downloaded_images
                                       .find { |x| File.basename(x.filename) == File.basename(element.attr['src']) }
    if downloaded_image
      extension = File.extname(downloaded_image.filename)
      extension[0] = ''
      element.attr['src'] = "data:image/#{extension};base64,#{downloaded_image.contents}"
    end
  end

  super(element, indent)
end