Class: Trenni::Builder

Inherits:
Object
  • Object
show all
Includes:
Markup
Defined in:
lib/trenni/builder.rb

Overview

Build markup quickly and efficiently.

Constant Summary collapse

INDENT =
"\t".freeze

Constants included from Markup

Markup::EMPTY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Markup

escape, #escape

Constructor Details

#initialize(output = nil, indent: true, encoding: Encoding::UTF_8) ⇒ Builder

Returns a new instance of Builder.



56
57
58
59
60
61
62
63
64
65
# File 'lib/trenni/builder.rb', line 56

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

#outputObject (readonly)

Returns the value of attribute output.



67
68
69
# File 'lib/trenni/builder.rb', line 67

def output
  @output
end

Class Method Details

.fragment(builder = nil, &block) ⇒ Object

A helper to generate fragments of markup.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/trenni/builder.rb', line 36

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



48
49
50
51
52
53
54
# File 'lib/trenni/builder.rb', line 48

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



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

def == other
  @output == String(other)
end

#append(data) ⇒ Object

Append pre-existing markup:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/trenni/builder.rb', line 113

def append(data)
  return unless data
  
  # The parent has one more child:
  @level[-1] += 1
  
  if @indent
    data.each_line.with_index do |line, i|
      @output << indentation << line
    end
  else
    @output << data
  end
end

#doctype(attributes = 'html') ⇒ Object



88
89
90
# File 'lib/trenni/builder.rb', line 88

def doctype(attributes = 'html')
  @output << "<!DOCTYPE #{attributes}>\n"
end

#indentationObject



80
81
82
83
84
85
86
# File 'lib/trenni/builder.rb', line 80

def indentation
  if @indent
    INDENT * (@level.size - 1)
  else
    ''
  end
end

#inline(name, attributes = {}, &block) ⇒ Object

Begin an inline tag.



98
99
100
101
102
103
104
105
106
# File 'lib/trenni/builder.rb', line 98

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.



93
94
95
# File 'lib/trenni/builder.rb', line 93

def tag(name, attributes = {}, &block)
  full_tag(name, attributes, @indent, @indent, &block)
end

#text(data) ⇒ Object



108
109
110
# File 'lib/trenni/builder.rb', line 108

def text(data)
  append escape(data)
end

#to_strObject Also known as: to_s

Required for output to buffer.



70
71
72
# File 'lib/trenni/builder.rb', line 70

def to_str
  @output
end