【问题标题】:How to migrate HAML helper to HAML 4?如何将 HAML 助手迁移到 HAML 4?
【发布时间】: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


【解决方案1】:

似乎 HAML 4 现在正在使用 Tilt 作为其 Markdown 过滤器。

我没有提到它,但我在尝试将我的代码迁移到 Ruby 2 和 HAML 4 之前使用了lazy_require,但在 HAML 4 中,lazy_require 不再存在。

在重新定义自己的 Markdown 模块之前,您必须使用 remove_filter 方法禁用默认的 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

解决此问题后我遇到了另一个问题,因为我使用的是 RedCarpet 而不是 Redcarpet (NameError) 并且很难意识到它:/...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多