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
感謝您的幫助;)!
如果您遇到錯誤,應該將其與您的問題一起發佈。 – toro2k
沒有錯誤,它只是不工作(唯一工作的部分是後處理方法)。 (鏈接,自動鏈接和block_code不起作用)。 – Fantattitude
在您的代碼中,您直接添加到Haml :: Filters模塊。這是一個錯字 - 你有一個Markdown模塊嗎? – matt