我正在使用Maruku和我的RoR3应用程序。但问题是,当我在使用Maruku之前使用h(text)方法转义数据库中的文本时,它会将>转义为>,因此Maruku不会将其视为块引用。
但我仍然想避开文本的其余部分,所以我的问题是,我如何才能使其工作?
我不想禁用转义,但也不想让它转义>
发布于 2010-06-06 20:34:39
下面的方法接受html_encoded多行字符串,并将所有已转换为html实体代码的maruku块引用元素替换回>
出于这个实现的目的,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>
"发布于 2010-06-07 07:13:33
Rails 3转义所有字符串by default。您需要使用"some_string.html_safe“将它们标记为安全,或者在模板中使用<%= raw some_string %>,如果您想避免这种情况。
如果您将sanitize helper设置为允许您想要传递的HTML标记,则可以执行以下操作:
<%= sanitize(@maruku_content.to_html) %>Sanitize将擦除您的内容并将输出标记为html_safe,同时保持所需的标记不变。此选项在rails_xss插件文档here中进行了讨论。他们使用的例子是针对纺织品的。
https://stackoverflow.com/questions/2920850
复制相似问题