2013-05-30 135 views
1

您好我正在使用HAML來呈現我的博客文章,我決定遷移到新的Ruby版本,新的Rails版本和新的HAML版本。問題是它看起來有些變化,我無法確定我的代碼有什麼問題。如何將HAML helper遷移到HAML 4?

有人可以解釋我需要改變什麼才能使用新版本?

UPDATE:意識到這可能與隆重的接待,而不是HAML但不知道:3

正如你會看到我用這個自定義渲染,從他們的鏈接自動顯示推文或Spotify的歌曲。 與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 

感謝您的幫助;)!

+0

如果您遇到錯誤,應該將其與您的問題一起發佈。 – toro2k

+0

沒有錯誤,它只是不工作(唯一工作的部分是後處理方法)。 (鏈接,自動鏈接和block_code不起作用)。 – Fantattitude

+1

在您的代碼中,您直接添加到Haml :: Filters模塊。這是一個錯字 - 你有一個Markdown模塊嗎? – matt

回答

2

看來HAML 4現在使用Tilt作爲其降價過濾器。

我沒有提到它,但在嘗試將我的代碼遷移到Ruby 2之前使用lazy_require HAML 4,但是在HAML 4中,lazy_require不再存在。

相反,您必須使用remove_filter方法在重新定義自己的Markdown模塊之前禁用默認的Markdown模塊。

這是一個基本的工作代碼:

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 

我遇到的另一個問題的解決,是因爲我是用隆重的接待,而不是隆重的接待(NameError)後都很難意識到這一點:/ ...