Question

I am attempting to extend the RedCarpet markdown parser to use images from a media library that I have created in my application.

I am using Cloudinary as the image store/CDN, and have a Media table that stores the public id of the image in Cloudinary. That's all good.

I have created a file under /lib called reddress.rb which contains the code below. I have been using this for some time to render markdown formatted text with no problems. However, now I am trying to use the cloudinary methods to extend the markdown with a shortcode that references the media record via its Id.

The problem is that the code cannot find the cl_image_tag method in the cloudinary helper, even with the .rb files being required.

require 'redcarpet'
require 'cloudinary'
require "cloudinary/helper"

class RedDress
    def initialize
    end

    def format_as_html(txt)
        markdown = Redcarpet::Markdown.new(ExtendedMarkdownHTML, :autolink => true, :space_after_headers => true)
        markdown.render(txt)
    end
end

class ExtendedMarkdownHTML < Redcarpet::Render::HTML
    def preprocess(full_document)

        full_document.gsub!(/\[media (\d+)\]/) { |m|
            media = Media.find($1)
            cl_image_tag(media.image_id, :crop => :fill, :width => 80, :height => 80) unless media.nil?
        }
        full_document
    end
end

The lib is being called, and without the cl_image_tag in place it works, and I have restarted the server (have to every time a change is made.)

I'm using Rails 4 with Ruby 2.0.0.

Any suggestions?

Était-ce utile?

La solution

Okay, after a bit of fiddling I've come up with a solution based on this from http://support.cloudinary.com/entries/25418221-How-do-I-use-the-Cloudinary-helpers-from-the-Ruby-GEM-using-Sinatra-

Inside the ExtendedMarkdownHTML class I included the Cloudinary Helper as so.

...
class ExtendedMarkdownHTML < Redcarpet::Render::HTML
include CloudinaryHelper

    def preprocess(full_document)
    ...

I also moved the file out of the /lib folder to /app/services which was some advice seen elsewhere.

Hope this helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top