Update: for the record, here's the implementation I ended up using.
Here's a trimmed down version of a parser I'm working on. There's still some code, but it should be quite easy to grasp the basic concepts of this parser.
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>"
So, as you can see, I'm breaking the text into paragraphs, and each paragraph is either a header, a list or a regular paragraph.
Is it feasible to add support for nowiki tags (where everything between <nowiki></nowiki> is not being parsed) for a parser like this? Feel free to answer "no", and suggest alternative methods of creating a parser :)
As a sidenote, you can see the actual parser code on Github. markup.rb and paragraph.rb