Sha256: 886251a91fe5d4de71bdf5c2d39714149c752f83b30d2261a68f0c18e53b4980

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

# encoding: UTF-8

module Cutaneous
  class Syntax
    attr_reader :tags

    def initialize(tag_definitions)
      @tags = tag_definitions
    end

    def is_dynamic?(text)
      !text.index(tag_start_pattern).nil?
    end

    def tag_start_pattern
      @tag_start_pattern ||= compile_start_pattern
    end

    def escaped_tag_pattern
      @escaped_tag_pattern ||= compile_start_pattern_with_prefix("\\\\")
    end

    def compile_start_pattern
      not_escaped = "(?<!\\\\)"
      compile_start_pattern_with_prefix(not_escaped)
    end

    def compile_start_pattern_with_prefix(prefix)
      openings = self.tags.map { |type, tags| Regexp.escape(tags[0]) }
      Regexp.new("#{prefix}(#{ openings.join("|") })")
    end

    # map the set of tags into a hash used by the parse routine that converts an opening tag into a
    # list of: tag type, the number of opening braces in the tag and the length of the closing tag
    def token_map
      @token_map ||= Hash[tags.map { |type, tags| [tags[0], [type, tags[0].count(?{), tags[1].length]] }]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cutaneous-0.3.0 lib/cutaneous/syntax.rb
cutaneous-0.2.0 lib/cutaneous/syntax.rb
cutaneous-0.1.7 lib/cutaneous/syntax.rb
cutaneous-0.1.6 lib/cutaneous/syntax.rb
cutaneous-0.1.5 lib/cutaneous/syntax.rb
cutaneous-0.1.4 lib/cutaneous/syntax.rb
cutaneous-0.1.3 lib/cutaneous/syntax.rb
cutaneous-0.1.2 lib/cutaneous/syntax.rb
cutaneous-0.1.1 lib/cutaneous/syntax.rb
cutaneous-0.1.0 lib/cutaneous/syntax.rb