0
更新:備案,here's the implementation I ended up using。將nowiki-tags添加到此解析器是否可行?
下面是我正在處理的解析器的修剪版本。還有一些代碼,但應該很容易掌握這個解析器的基本概念。
class Markup
def initialize(markup)
@markup = markup
end
def to_html
@html ||= @markup.split(/(\r\n){2,}|\n{2,}/).map {|p| Paragraph.new(p).to_html }.join("\n")
end
class Paragraph
def initialize(paragraph)
@p = paragraph
end
def to_html
@p.gsub!(/'{3}([^']+)'{3}/, "<strong>\\1</strong>")
@p.gsub!(/'{2}([^']+)'{2}/, "<em>\\1</em>")
@p.gsub!(/`([^`]+)`/, "<code>\\1</code>")
case @p
when /^=/
level = (@p.count("=")/2) + 1 # Starting on h2
@p.gsub!(/^[= ]+|[= ]+$/, "")
"<h#{level}>" + @p + "</h#{level}>"
when /^(\*|\#)/
# I'm parsing lists here. Quite a lot of code, and not relevant, so
# I'm leaving it out.
else
@p.gsub!("\n", "\n<br/>")
"<p>" + @p + "</p>"
end
end
end
end
p Markup.new("Here is `code` and ''emphasis'' and '''bold'''!
Baz").to_html
# => "<p>Here is <code>code</code> and <em>emphasis</em> and <strong>bold</strong>!</p>\n<p>Baz</p>"
所以,你可以看到,我打破了文成段,每個段是一個頁眉,列表或常規段落。
對於像這樣的解析器添加對nowiki標記的支持(< nowiki> </nowiki>沒有被解析)是否可行?隨意回答「否」,並建議創建解析器的其他方法:)
作爲旁註,您可以在Github上看到實際的解析器代碼。 markup.rb和paragraph.rb
是文本的分割成段,像我的解析器確實,分詞的一種形式? – 2009-09-17 05:33:26
也許使用一個非常鬆散的定義。一般來說,一個標記器將輸入流分割成不同的組件,可以使用最好的粒度級別單獨運行。分割成段落,然後分裂成其他部分是一種雙通道標記器。通常,在編寫這種類型的東西時,您只能通過自己的方法來解析。在某個時候,使用適當的解析器框架更有效,但這是另一個主題。 – tadman 2009-09-17 15:12:44
標記爲答案。謝謝! – 2009-09-21 08:23:53