【发布时间】:2013-05-30 11:13:47
【问题描述】:
您好,我正在使用 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
感谢您的帮助;)!
【问题讨论】:
-
如果您遇到错误,您应该将其与您的问题一起发布。
-
没有错误,只是不起作用(唯一起作用的是后处理方法)。 (链接、自动链接和 block_code 不起作用)。
-
在您的代码中,您将直接添加到
Haml::Filters模块。这是一个错字吗?你有Markdown模块吗? -
这帮助我解决了问题我会发布答案
标签: ruby-on-rails ruby haml helper redcarpet