Ruby on rails 如何将HAML帮助程序迁移到HAML 4?
嗨,我正在使用HAML来呈现我的博客文章,我决定迁移到新的Ruby版本、新的Rails版本和新的HAML版本。问题是,它似乎发生了一些变化,我无法确定我的代码出了什么问题 有人能告诉我,为了使用新版本,需要更改哪些内容吗 更新:意识到它可能与红毯有关,而不是HAML,但不确定:3 正如您将看到的,我使用这个自定义渲染器自动显示推文或Spotify歌曲的链接。 由CodeRay着色的代码块也是如此。Ruby on rails 如何将HAML帮助程序迁移到HAML 4?,ruby-on-rails,ruby,haml,helper,redcarpet,Ruby On Rails,Ruby,Haml,Helper,Redcarpet,嗨,我正在使用HAML来呈现我的博客文章,我决定迁移到新的Ruby版本、新的Rails版本和新的HAML版本。问题是,它似乎发生了一些变化,我无法确定我的代码出了什么问题 有人能告诉我,为了使用新版本,需要更改哪些内容吗 更新:意识到它可能与红毯有关,而不是HAML,但不确定:3 正如您将看到的,我使用这个自定义渲染器自动显示推文或Spotify歌曲的链接。 由CodeRay着色的代码块也是如此。 模块Haml::过滤器 需要“net/https” 需要“uri” 包含Haml::Filter
模块Haml::过滤器
需要“net/https”
需要“uri”
包含Haml::Filters::Base
类MarkdownRendererfalse,:css=>:class})
结束
def自动链接(链接,链接类型)
twitterReg=/https?:\/\/twitter\.com\/[a-zA-Z]+\/状态?\/([0-9]+)/
spotifyReg=/(https?:\/\/open.spotify.com\/(曲目|用户|艺术家|专辑)\/[a-zA-Z0-9]+(\/playlist\/[a-zA-Z0-9]+)| spotify:(曲目|用户|艺术家|专辑):[a-zA-Z0-9]+(:播放列表:[a-zA-Z0-Z0-9]+)/
如果链接类型==:url
如果link=~twitterReg
tweet=twitterReg.match(链接)
URLTeet=tweet[0]
idTweet=tweet[2]
开始
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\u mode=OpenSSL::SSL::verify\u NONE
request=Net::HTTP::Get.new(uri.request\u uri)
response=http.request(请求)
jsonTweet=ActiveSupport::JSON.decode(response.body)
jsonTweet[“html”]
救援异常=>e
""
结束
elsif link=~spotifyReg
spotify=1美元
htmlSpotify=“”
htmlSpotify
其他的
""
结束
结束
结束
def链接(链接、标题、内容)
""
结束
def后处理(完整文档)
full_document.gsub!(/true),:tables=>true,:fenced_code_blocks=>true,:autolink=>true,:删除线=>true)。呈现(文本)
结束
结束
谢谢你的帮助 看来HAML 4现在使用倾斜作为降价过滤器 我没有提到这一点,但在尝试将代码迁移到Ruby 2和HAML 4之前,我使用了lazy_-require,但在HAML 4中lazy_-require已经不存在了 相反,在重新定义自己的标记模块之前,您必须使用remove_filter方法禁用默认标记模块 以下是基本工作代码:
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),并且很难意识到:/…如果您遇到错误,您应该将其与您的问题一起发布。没有错误,只是不起作用(唯一有效的部分是后处理方法)。(链接、自动链接和块_代码不起作用)。在代码中,您直接添加到
Haml::Filters
模块。这是打字错误吗?你有降价模块吗?这帮助我解决了这个问题,我会发布答案
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