Class: Decode::Comment::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/comment/tags.rb

Constant Summary collapse

PATTERN =
/\A\s*@(?<directive>.*?)(\s+(?<remainder>.*?))?\Z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directives) ⇒ Tags

Returns a new instance of Tags.



19
20
21
# File 'lib/decode/comment/tags.rb', line 19

def initialize(directives)
	@directives = directives
end

Class Method Details

.build {|directives| ... } ⇒ Object

Yields:

  • (directives)


11
12
13
14
15
16
17
# File 'lib/decode/comment/tags.rb', line 11

def self.build
	directives = Hash.new
	
	yield directives
	
	return self.new(directives)
end

Instance Method Details

#ignore(lines, level = 0) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/decode/comment/tags.rb', line 55

def ignore(lines, level = 0)
	if line = lines.first
		# Is it at the right indentation level:
		return unless valid_indentation?(line, level)
		
		lines.shift
	end
end

#parse(lines, level = 0, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/decode/comment/tags.rb', line 29

def parse(lines, level = 0, &block)
	while line = lines.first
		# Is it at the right indentation level:
		return unless valid_indentation?(line, level)
		
		# We are going to consume the line:
		lines.shift
		
		# Match it against a tag:
		if match = PATTERN.match(line)
			if klass = @directives[match[:directive]]
				yield klass.parse(
					match[:directive], match[:remainder],
					lines, self, level
				)
			else
				# Ignore unknown directive.
			end
		
		# Or it's just text:
		else
			yield Text.new(line)
		end
	end
end

#valid_indentation?(line, level) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/decode/comment/tags.rb', line 23

def valid_indentation?(line, level)
	line.start_with?('  ' * level) || line.start_with?("\t" * level)
end