在带有Redcarpet的Markdown中使用ERB [英] Using ERB in Markdown with Redcarpet

查看:123
本文介绍了在带有Redcarpet的Markdown中使用ERB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Markdown与.erb配合使用.我想使用high_voltage渲染Markdown页面(或带有markdown部分的普通.html.erb文件),这些页面已被Redcarpet解析,并且正在努力使其全部协同工作.

I'm trying to get Markdown to play nicely with .erb. I'd like to use high_voltage to render markdown pages (or normal .html.erb files with markdown partials) that are parsed with Redcarpet and am struggling to get it to all work together.

此刻,我有一个名为markdown_template_handler.rb的初始化程序,其中包含以下代码:

At the moment I have an initializer called markdown_template_handler.rb that contains the following code:

class MarkdownTemplateHandler
  def erb
    @erb ||= ActionView::Template.registered_template_handler(:erb)
  end

  def call(template)
    compiled_source = erb.call(template)
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)

    "#{markdown.render(compiled_source.source).inspect}.html_safe;"
  end
end

ActionView::Template.register_template_handler(:md, MarkdownTemplateHandler.new)

但是,它在第​​7行失败,compiled_source = erb.call(template),错误代码为参数数量错误(给定1,预期为2)"

However it is failing at line 7, compiled_source = erb.call(template) with the error code saying "wrong number of arguments (given 1, expected 2)"

我查看了 ERB Ruby文档,但据我了解,call方法是新方法的派生形式,它仅需要1个参数,即文本.但是,当我尝试仅在快速Rails控制台会话中使用它时,它也需要两个参数.

I looked at the ERB Ruby documentation but from what I understood, the call method is a derivative of the new method, which only requires 1 argument, the text. However, when I tried to use it just in a quick rails console session, it also required two arguments.

当我从上面的代码中删除了解析erb的要求时,一切都会按预期进行,因此我认为这与Redcarpet无法正常工作没有任何关系.

When I remove the requirement to parse erb from the above code, everything works as expected, so I don't think it has anything to do with Redcarpet not working.

我正在使用Rails v6.0.0.rc1& Ruby v2.5.3p105

I am using Rails v6.0.0.rc1 & Ruby v2.5.3p105

感谢您的帮助.

修改

进一步的研究使我找到了 Rails 6.0 ERB ActionView模板处理程序.此处理程序的调用方法确实需要两个参数,即模板和源.也就是说,在 Rails 5.2.3中, ERB动作视图模板处理程序调用方法仅需要一个参数,即模板.

Further research has led me to finding the Rails 6.0 ERB ActionView template handler. The call method of this handler does indeed require two arguments, the template and the source. That said, in Rails 5.2.3, the ERB Action View template handler call method only requires one argument, the template.

有人可以指出我在这种情况下是什么来源的方向吗?我找不到它的文档.

Could someone please point me in the direction of figuring out what source is in this context? There is no documentation for it that I can find.

推荐答案

在这种情况下,调用处理程序时,ActionView会将源传递给call.

In this case, source would be passed to the call by ActionView when the handler is called.

您将像这样重写call函数:

def call(template, source)
  compiled_source = erb.call(template, source)
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)

  "#{markdown.render(compiled_source).inspect}.html_safe;"
end

在Rails 6之前,source值是从template.source提取的,但是现在作为单独的参数传递.

Before Rails 6, the source value was extracted from template.source, but it is now passed as a separate parameter.

这篇关于在带有Redcarpet的Markdown中使用ERB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆