質問

Hi I'm using HAML to render my blog articles and I decided to migrate to new Ruby version, new Rails version and new HAML version. The problem is that it seems something changed and I can't identify what's wrong with my code.

Could someone explain me what needs to be changed in order to work with the new version ?

UPDATE : Realized it may be related to Redcarpet and not HAML but not sure :3

As you will see I use this custom renderer to automatically display Tweets or Spotify songs from their links. Same for code blocks colored by CodeRay.

module Haml::Filters
  require "net/https"
  require "uri"

  include Haml::Filters::Base

  class MarkdownRenderer < Redcarpet::Render::HTML
    def block_code(code, language)
        CodeRay.highlight(code, language, {:line_number_anchors => false, :css => :class})
    end

    def autolink(link, link_type)
      twitterReg = /https?:\/\/twitter\.com\/[a-zA-Z]+\/status(es)?\/([0-9]+)/
      spotifyReg = /(https?:\/\/open.spotify.com\/(track|user|artist|album)\/[a-zA-Z0-9]+(\/playlist\/[a-zA-Z0-9]+|)|spotify:(track|user|artist|album):[a-zA-Z0-9]+(:playlist:[a-zA-Z0-9]+|))/
      if link_type == :url
        if link =~ twitterReg
          tweet = twitterReg.match(link)
          urlTweet = tweet[0]
          idTweet = tweet[2]
          begin
            uri = URI.parse("https://api.twitter.com/1/statuses/oembed.json?id=#{idTweet}")
            http = Net::HTTP.new(uri.host, uri.port)
            http.use_ssl = true
            http.verify_mode = OpenSSL::SSL::VERIFY_NONE
            request = Net::HTTP::Get.new(uri.request_uri)
            response = http.request(request)
            jsonTweet = ActiveSupport::JSON.decode(response.body)
            jsonTweet["html"]
          rescue Exception => e
            "<a href='#{link}'><span data-title='#{link}'>#{link}</span></a>"
          end
        elsif link =~ spotifyReg
          spotify = $1

          htmlSpotify = "<iframe style=\"width: 80%; height: 80px;\" src=\"https://embed.spotify.com/?uri=#{spotify}\" frameborder=\"0\" allowtransparency=\"true\"></iframe>"
          htmlSpotify
        else
          "<a href='#{link}'><span data-title='#{link}'>#{link}</span></a>"
        end
      end
    end

    def link(link, title, content)
      "<a href='#{link}'><span data-title='#{content}'>#{content}</span></a>"
    end

    def postprocess(full_document)
      full_document.gsub!(/<p><img/, "<p class='images'><img")
      full_document.gsub!(/<p><iframe/, "<p class='iframes'><iframe")
      full_document
    end

  end

  def render(text)
    Redcarpet::Markdown.new(MarkdownRenderer.new(:hard_wrap => true), :tables => true, :fenced_code_blocks => true, :autolink => true, :strikethrough => true).render(text)
  end
end

Thanks for helping ;) !

役に立ちましたか?

解決

It seems HAML 4 is now using Tilt as its Filter for Markdown.

I didn't mention it but I was using lazy_require prior to try to migrate my code to Ruby 2 & HAML 4 but in HAML 4 lazy_require doesn't exist anymore.

Instead you have to use remove_filter method to disable default Markdown module prior redefining your own Markdown module.

Here is a basic working code :

module Haml::Filters

  include Haml::Filters::Base

  remove_filter("Markdown") # Removes basic filter (lazy_require is dead)

  module Markdown
    def render text
      markdown.render text
    end

    private

    def markdown
      @markdown ||= Redcarpet::Markdown.new Redcarpet::Render::HTML, {
        autolink:         true,
        fenced_code:      true,
        generate_toc:     true,
        gh_blockcode:     true,
        hard_wrap:        true,
        no_intraemphasis: true,
        strikethrough:    true,
        tables:           true,
        xhtml:            true
      }
    end
  end
end

I encountered another problem after solving this because I was using RedCarpet instead of Redcarpet (NameError) and had a hard time realizing it :/…

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top