Class: Trenni::Markdown::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/trenni/markdown/generator.rb

Overview

This parser delegate generates nested code output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment_prefix = '# ') ⇒ Generator

Returns a new instance of Generator.



25
26
27
28
29
30
31
# File 'lib/trenni/markdown/generator.rb', line 25

def initialize(comment_prefix = '# ')
	@comment_prefix = comment_prefix
	@stack = []
	
	@whitespace = String.new
	@output = String.new
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



33
34
35
# File 'lib/trenni/markdown/generator.rb', line 33

def output
  @output
end

Instance Method Details

#append(lines, prefix = nil, ignore_whitespace: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trenni/markdown/generator.rb', line 46

def append(lines, prefix = nil, ignore_whitespace: false)
	append_whitespace unless ignore_whitespace
	
	if prefix
		prefix = indentation + prefix
	else
		prefix = indentation
	end
	
	Array(lines).each do |line|
		@output << prefix << line
	end
end

#append_whitespaceObject



39
40
41
42
43
44
# File 'lib/trenni/markdown/generator.rb', line 39

def append_whitespace
	unless @whitespace.empty?
		@output << @whitespace
		@whitespace = String.new
	end
end

#begin_parse(parser) ⇒ Object



69
70
# File 'lib/trenni/markdown/generator.rb', line 69

def begin_parse(parser)
end

#close(text) ⇒ Object



80
81
82
# File 'lib/trenni/markdown/generator.rb', line 80

def close(text)
	@stack.push(text)
end

#code(text) ⇒ Object



104
105
106
# File 'lib/trenni/markdown/generator.rb', line 104

def code(text)
	append(text)
end

#end_parse(parser) ⇒ Object



72
73
74
# File 'lib/trenni/markdown/generator.rb', line 72

def end_parse(parser)
	outdent(1)
end

#heading(level, text) ⇒ Object



96
97
98
# File 'lib/trenni/markdown/generator.rb', line 96

def heading(level, text)
	nest(level, "module #{text}\n", "end\n")
end

#indentationObject



35
36
37
# File 'lib/trenni/markdown/generator.rb', line 35

def indentation
	"\t" * @stack.size
end

#nest(level, first_line, last_line) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/trenni/markdown/generator.rb', line 84

def nest(level, first_line, last_line)
	outdent(level)
	
	# We should now be at the point where we can indent:
	append(first_line)
	@stack.push(last_line)
end

#open(text) ⇒ Object



76
77
78
# File 'lib/trenni/markdown/generator.rb', line 76

def open(text)
	@output << text
end

#outdent(level) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/trenni/markdown/generator.rb', line 60

def outdent(level)
	# Outdent until we can indent one level:
	while level <= @stack.size
		append(@stack.pop, ignore_whitespace: true)
	end
	
	append_whitespace
end

#paragraph(text) ⇒ Object



100
101
102
# File 'lib/trenni/markdown/generator.rb', line 100

def paragraph(text)
	append(text, @comment_prefix)
end

#whitespace(text) ⇒ Object



92
93
94
# File 'lib/trenni/markdown/generator.rb', line 92

def whitespace(text)
	@whitespace << text
end