2010-01-11 4 views
8

Maraku 또는 Kramdown과 같은 Ruby 라이브러리를 사용하여 Rails CMS 주석 시스템에 Markdown을 구현하고 싶습니다. 사용자가 제출할 수있는 Markdown 기능을 제한해야합니다. 이 시스템에서 사용자는 이미지, HTML에 삽입하거나 무거운 편집을 수행 할 수 없지만 강조 및 하이퍼 링크는 괜찮습니다.Ruby에서 Markdown 구문을 어떻게 제한 할 수 있습니까?

기본적으로 this Textile filter과 비슷한 것을 만들고 싶지만 Markdown 구문을 사용하고 싶습니다.

답변

8

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 
+0

감사합니다.이 방법은 문제를 해결하는 좋은 방법입니다. 나는 그것을 시도 할 것이다. –

관련 문제