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.



34
35
36
# File 'lib/decode/comment/tags.rb', line 34

def initialize(directives)
	@directives = directives
end

Class Method Details

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

Yields:

  • (directives)


26
27
28
29
30
31
32
# File 'lib/decode/comment/tags.rb', line 26

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

Instance Method Details

#ignore(lines, level = 0) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/decode/comment/tags.rb', line 70

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/decode/comment/tags.rb', line 44

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.strip)
		end
	end
end

#valid_indentation?(line, level) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/decode/comment/tags.rb', line 38

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