Skip to content

Instantly share code, notes, and snippets.

@jgarber
Created February 21, 2012 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgarber/1879199 to your computer and use it in GitHub Desktop.
Save jgarber/1879199 to your computer and use it in GitHub Desktop.
class HtmlTag < Parslet::Parser
root(:tag)
rule(:tag) do
( open_tag | close_tag | self_closing_tag ).as(:html_tag)
end
rule(:self_closing_tag) { str("<") >> tag_name >> attributes? >> (spaces? >> str("/")) >> str(">") }
rule(:open_tag) { str("<") >> tag_name >> attributes? >> str(">") }
rule(:close_tag) { str("</") >> tag_name >> str(">") }
rule(:tag_name) { match("[A-Za-z_:]") >> name_char.repeat }
rule(:attributes?) { attribute.repeat }
rule(:attribute) do
spaces >> attribute_name >> (str("=") >> attribute_value).maybe
end
rule(:attribute_name) { match("[A-Za-z_:]") >> name_char.repeat(1) }
rule(:attribute_value) { (str('"') >> match('[^"]').repeat >> str('"') | str("'") >> match("[^']").repeat >> str("'") ) }
rule(:name_char) { match("[\-A-Za-z0-9._:?]") }
rule(:space) { match("[ \t]") }
rule(:spaces) { space.repeat(1) }
rule(:spaces?) { space.repeat }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment