我将 Maruku 与我的 RoR3 应用程序一起使用。但问题是,当我h(text)
在使用 Maruku 之前使用该方法从数据库中转义文本时,它会转义>
,>
因此 Maruku 不会将其视为块引用。
但我仍然想逃避文本的其余部分,所以我的问题是我怎样才能完成这项工作?
我不想禁用转义,但我不想让它逃脱>
Rails 3默认转义所有字符串。您需要使用“some_string.html_safe”将它们标记为安全,或者如果您想避免这种情况,请在模板中使用 <%= raw some_string %>。
如果您设置sanitize 帮助程序以允许您想要通过的 HTML 标记,您可以执行以下操作:
<%= sanitize(@maruku_content.to_html) %>
Sanitize 将清理您的内容并将输出标记为 html_safe,同时保持所需的标签完好无损。此选项在此处的 rails_xss 插件文档中进行了讨论。他们使用的例子是纺织品。
以下方法采用 html_encoded 多行字符串并将所有已转换为 html 实体代码的 maruku blockquote 元素替换回 >
出于此实现的目的,maruku 块引用行被定义为以一个或多个 > 序列开头的行,并用可选的空格分隔。
def maruku_escape(text)
text.gsub(/^([\s]*\>)+/) {|match| match.gsub(/\>/, '>')}
end
使用了以下测试字符串
test_text = "<b>A bold tag</b>
<span>Some text in a span</span>
Some Markdown
> Blockquote 1
> > nested blockquote 1
> > nested blockquote 2
>> nested blockquote 3 with no spaces
Some plain text with an invalid blockquote > Some blockquote text
<i>The end in italics<i>"
并使用它如下maruku_text = maruku_escape(ERB::Util.html_escape(test_text))
给出了以下结果
result = "<b>A bold tag</b>
<span>Some text in a span</span>
Some Markdown
> Blockquote 1
> > nested blockquote 1
> > nested blockquote 2
>> nested blockquote 3 with no spaces
Some plain text with an invalid blockquote > Some blockquote text
<i>The end in italics<i>
"