Created
February 21, 2012 21:47
-
-
Save jgarber/1879199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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