2017-10-13 53 views

回答

1

有该媒体链接的宝石,如redcarpet

这里是一个使用默认降价渲染器的示例,但如果您需要不同的语法,则可以使用自己的渲染器。

require 'redcarpet' 
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) 
markdown.render("**This** _is_ an [example](http://example.org/).") 
# => "<p><strong>This</strong> <em>is</em> an <a href="http://example.org/">example</a>.</p>" 

总的来说这是一个坏主意,使用正则表达式与HTML或推倒重来,但如果你坚持,在这里一个简单的例子,你怎么能做到这一点。

# create a hash with our regular expressions and replacements 
map = { /_(.+?)_/ => '<em>\1</em>', /\*(.+?)\*/ => '<strong>\1</strong>'} 
markdown = "hallo _html_ dan *gaga* das *test*" 
# enumerate the hash and replace the expressions with their replacement 
# tap returns the result to itself 
markdown.tap{|md| map.each { |re, v| md.gsub!(re, v)}} 
# => hallo <em>html</em> dan <strong>gaga</strong> das <strong>test</strong> 
+0

@Cary Swoveland嗨卡里,我是那种你,你一个风扇知道。谢谢你的建议,但你为什么不回答这个问题?我感觉就像瞄准一个移动的目标,所以我删除了它。在临睡前看到这一点,我会打电话给休息日。 – peter