嗨,我正在使用 HAML 呈现我的博客文章,我决定迁移到新的 Ruby 版本、新的 Rails 版本和新的 HAML 版本。问题是它似乎发生了一些变化,我无法确定我的代码有什么问题。
有人可以解释一下为了使用新版本需要更改哪些内容吗?
更新:意识到它可能与 Redcarpet 而不是 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
感谢您的帮助;)!