Sha256: 7edfb0921dc3f551a6ada1c6ee592fff8bc6cec67319780fa41a2832a8cf894c

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

class Shortcode::Parser < Parslet::Parser

  rule(:block_tag)        { match_any_of Shortcode.block_tags }
  rule(:self_closing_tag) { match_any_of Shortcode.self_closing_tags }

  rule(:quotes) { str('"') }

  rule(:space)        { str(' ').repeat(1) }
  rule(:space?)       { space.maybe }
  rule(:newline)      { (str("\r\n") | str("\n")) >> space? }
  rule(:whitespace)   { (space | newline).repeat(1) }
  rule(:whitespace?)  { whitespace.maybe }

  rule(:key)    { match('[a-zA-Z0-9\-_]').repeat(1) }
  rule(:value)  { quotes >> (quotes.absent? >> any).repeat.as(:value) >> quotes }

  rule(:option)   { key.as(:key) >> str('=') >> value }
  rule(:options)  { (str(' ') >> option).repeat(1) }
  rule(:options?) { options.repeat(0, 1) }

  rule(:open)       { str('[') >> block_tag.as(:open) >> options?.as(:options) >> str(']') >> whitespace? }
  rule(:close)      { str('[/') >> block_tag.as(:close) >> str(']') >> whitespace?  }
  rule(:open_close) { str('[') >> self_closing_tag.as(:open_close) >> options?.as(:options) >> str(']') >> whitespace? }

  rule(:text)   { ((close | block | open_close).absent? >> any).repeat(1).as(:text) }
  rule(:block)  { (open >> (block | text | open_close).repeat.as(:inner) >> close) }

  rule(:body) { (block | text | open_close).repeat.as(:body) }
  root(:body)

  private

    def match_any_of(tags)
      tags.map{ |tag| str(tag) }.inject do |tag_chain, tag|
        tag_chain.send :|, tag
      end
    end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shortcode-0.0.2 lib/shortcode/parser.rb
shortcode-0.0.1 lib/shortcode/parser.rb