8
我希望在使用Ruby库的Rails CMS评论系统中实现Markdown,例如Maraku或Kramdown。我需要限制用户可以提交哪些Markdown功能。在这个系统中,用户不允许插入图像,html或执行任何繁重的编辑,但重点和超链接都可以。如何限制Ruby中的Markdown语法?
本质上,我希望创建类似于this Textile filter的东西,但对于Markdown语法。
我希望在使用Ruby库的Rails CMS评论系统中实现Markdown,例如Maraku或Kramdown。我需要限制用户可以提交哪些Markdown功能。在这个系统中,用户不允许插入图像,html或执行任何繁重的编辑,但重点和超链接都可以。如何限制Ruby中的Markdown语法?
本质上,我希望创建类似于this Textile filter的东西,但对于Markdown语法。
我在使用sanitize gem进行降价转换以消毒数据之后使用了第二步。它的白名单和非常可配置的,你可以很容易地实现你所追求的。
为了节省你一些时间,这里是我的文本格式化模块,希望它可以帮助你。内置的宽松规则对我来说有点太严格了。
module TextFormatter
require 'sanitize'
module Formatters
MARKDOWN = 1
TEXTILE = 2
end
RELAXED = {
:elements => [
'a', 'b', 'blockquote', 'br', 'caption', 'cite', 'code', 'col',
'colgroup', 'dd', 'dl', 'dt', 'em', 'i', 'img', 'li', 'ol', 'p', 'pre',
'q', 'small', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody', 'td',
'tfoot', 'th', 'thead', 'tr', 'u', 'ul', 'del', 'ins', 'h1', 'h2', 'h3', 'h4', 'h5', 'h5', 'hr', 'kbd'],
:attributes => {
'a' => ['href', 'title'],
'blockquote' => ['cite'],
'col' => ['span', 'width'],
'colgroup' => ['span', 'width'],
'img' => ['align', 'alt', 'height', 'src', 'title', 'width'],
'ol' => ['start', 'type'],
'q' => ['cite'],
'table' => ['summary', 'width'],
'td' => ['abbr', 'axis', 'colspan', 'rowspan', 'width'],
'th' => ['abbr', 'axis', 'colspan', 'rowspan', 'scope',
'width'],
'ul' => ['type']
},
:protocols => {
'a' => {'href' => ['ftp', 'http', 'https', 'mailto',
:relative]},
'blockquote' => {'cite' => ['http', 'https', :relative]},
'img' => {'src' => ['http', 'https', :relative]},
'q' => {'cite' => ['http', 'https', :relative]}
}
}
def self.to_html(text, formatter = Formatters::MARKDOWN)
return "" unless text
html = case formatter
when Formatters::MARKDOWN then
RDiscount.new(text, :smart).to_html
when Formatters::TEXTILE then
RedCloth.new(text).to_html
end
Sanitize.clean(html, RELAXED)
end
end
谢谢,这看起来像一个很好的方法来解决这个问题。我会试一试。 – 2010-01-12 04:03:20