Class: Cutaneous::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/cutaneous/syntax.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_definitions) ⇒ Syntax

Returns a new instance of Syntax.



7
8
9
# File 'lib/cutaneous/syntax.rb', line 7

def initialize(tag_definitions)
  @tags = tag_definitions
end

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/cutaneous/syntax.rb', line 5

def tags
  @tags
end

Instance Method Details

#compile_start_patternObject



23
24
25
26
# File 'lib/cutaneous/syntax.rb', line 23

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

#compile_start_pattern_with_prefix(prefix) ⇒ Object



28
29
30
31
# File 'lib/cutaneous/syntax.rb', line 28

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

#escaped_tag_patternObject



19
20
21
# File 'lib/cutaneous/syntax.rb', line 19

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

#is_dynamic?(text) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cutaneous/syntax.rb', line 11

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

#tag_start_patternObject



15
16
17
# File 'lib/cutaneous/syntax.rb', line 15

def tag_start_pattern
  @tag_start_pattern ||= compile_start_pattern
end

#token_mapObject

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



35
36
37
# File 'lib/cutaneous/syntax.rb', line 35

def token_map
  @token_map ||= Hash[tags.map { |type, tags| [tags[0], [type, tags[0].count(?{), tags[1].length]] }]
end