2

我将 Maruku 与我的 RoR3 应用程序一起使用。但问题是,当我h(text)在使用 Maruku 之前使用该方法从数据库中转义文本时,它会转义>>因此 Maruku 不会将其视为块引用。

但我仍然想逃避文本的其余部分,所以我的问题是我怎样才能完成这项工作?

我不想禁用转义,但我不想让它逃脱>

4

2 回答 2

3

Rails 3默认转义所有字符串。您需要使用“some_string.html_safe”将它们标记为安全,或者如果您想避免这种情况,请在模板中使用 <%= raw some_string %>。

如果您设置sanitize 帮助程序以允许您想要通过的 HTML 标记,您可以执行以下操作:

<%= sanitize(@maruku_content.to_html) %>

Sanitize 将清理您的内容并将输出标记为 html_safe,同时保持所需的标签完好无损。此选项在此处的 rails_xss 插件文档中进行了讨论。他们使用的例子是纺织品。

于 2010-06-06T23:13:32.540 回答
0

以下方法采用 html_encoded 多行字符串并将所有已转换为 html 实体代码的 maruku blockquote 元素替换回 >

出于此实现的目的,maruku 块引用行被定义为以一个或多个 > 序列开头的行,并用可选的空格分隔。

def maruku_escape(text)
  text.gsub(/^([\s]*\&gt;)+/) {|match| match.gsub(/\&gt;/, '>')}
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 =  "&lt;b&gt;A bold tag&lt;/b&gt;
&lt;span&gt;Some text in a span&lt;/span&gt;

Some Markdown
> Blockquote 1
  > > nested blockquote 1
  > > nested blockquote 2
  >> nested blockquote 3 with no spaces


Some plain text with an invalid blockquote &gt; Some blockquote text
&lt;i&gt;The end in italics&lt;i&gt;
"
于 2010-06-06T12:34:39.323 回答