Class: Trenni::Builder
- Inherits:
-
Object
- Object
- Trenni::Builder
- Includes:
- Markup
- Defined in:
- lib/trenni/builder.rb,
lib/trenni/template.rb
Overview
Build markup quickly and efficiently.
Constant Summary collapse
- INDENT =
"\t".freeze
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
-
.fragment(builder = nil, &block) ⇒ Object
A helper to generate fragments of markup.
- .tag(name, content, **attributes) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #>>(block) ⇒ Object
-
#append(value) ⇒ Object
Append pre-existing markup:.
- #capture(*args, &block) ⇒ Object
- #doctype(attributes = 'html') ⇒ Object
- #indentation ⇒ Object
-
#initialize(output = nil, indent: true, encoding: Encoding::UTF_8) ⇒ Builder
constructor
A new instance of Builder.
-
#inline(name, attributes = {}, &block) ⇒ Object
Begin an inline tag.
-
#tag(name, attributes = {}, &block) ⇒ Object
Begin a block tag.
- #text(value) ⇒ Object
-
#to_str ⇒ Object
(also: #to_s)
Required for output to buffer.
Methods included from Markup
Constructor Details
#initialize(output = nil, indent: true, encoding: Encoding::UTF_8) ⇒ Builder
Returns a new instance of Builder.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/trenni/builder.rb', line 52 def initialize(output = nil, indent: true, encoding: Encoding::UTF_8) # This field gets togged in #inline so we keep track of it separately from @indentation. @indent = indent # We don't need to use MarkupString here as Builder itself is considered markup and should be inserted directly into the output stream. @output = output || MarkupString.new.force_encoding(encoding) @level = [0] @children = [0] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
63 64 65 |
# File 'lib/trenni/builder.rb', line 63 def output @output end |
Class Method Details
.fragment(builder = nil, &block) ⇒ Object
A helper to generate fragments of markup.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/trenni/builder.rb', line 32 def self.fragment(builder = nil, &block) if builder yield builder else builder = self.new yield builder end return builder end |
.tag(name, content, **attributes) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/trenni/builder.rb', line 44 def self.tag(name, content, **attributes) self.fragment do |builder| builder.inline(name, attributes) do builder.text content end end end |
Instance Method Details
#==(other) ⇒ Object
72 73 74 |
# File 'lib/trenni/builder.rb', line 72 def == other @output == String(other) end |
#>>(block) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/trenni/template.rb', line 30 def >> block if block Template.buffer(block.binding) << self return nil else return self end end |
#append(value) ⇒ Object
Append pre-existing markup:
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/trenni/builder.rb', line 120 def append(value) return unless value # The parent has one more child: @level[-1] += 1 if @indent value.each_line.with_index do |line, i| @output << indentation << line end else @output << value end end |
#capture(*args, &block) ⇒ Object
39 40 41 |
# File 'lib/trenni/template.rb', line 39 def capture(*args, &block) self.append Template.capture(*args, &block) end |
#doctype(attributes = 'html') ⇒ Object
84 85 86 |
# File 'lib/trenni/builder.rb', line 84 def doctype(attributes = 'html') @output << "<!DOCTYPE #{attributes}>\n" end |
#indentation ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/trenni/builder.rb', line 76 def indentation if @indent INDENT * (@level.size - 1) else '' end end |
#inline(name, attributes = {}, &block) ⇒ Object
Begin an inline tag.
94 95 96 97 98 99 100 101 102 |
# File 'lib/trenni/builder.rb', line 94 def inline(name, attributes = {}, &block) indent = @indent full_tag(name, attributes, @indent, false) do @indent = false yield if block_given? @indent = indent end end |
#tag(name, attributes = {}, &block) ⇒ Object
Begin a block tag.
89 90 91 |
# File 'lib/trenni/builder.rb', line 89 def tag(name, attributes = {}, &block) full_tag(name, attributes, @indent, @indent, &block) end |
#text(value) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/trenni/builder.rb', line 104 def text(value) return unless value if @indent @output << "\n" if @level.last > 0 @output << indentation end Markup.append(@output, value) if @indent @output << "\n" end end |
#to_str ⇒ Object Also known as: to_s
Required for output to buffer.
66 67 68 |
# File 'lib/trenni/builder.rb', line 66 def to_str @output end |