Class: Deplate::Output

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/deplate/output.rb

Overview

Description:

Usage:

TODO:

CHANGES:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deplate, inherited_output = nil) ⇒ Output

Returns a new instance of Output.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/deplate/output.rb', line 51

def initialize(deplate, inherited_output=nil)
    @@idx += 1
    @deplate     = deplate
    @formatter   = deplate.formatter
    @options     = deplate.options
    @variables   = deplate.variables
    @templates   = deplate.templates
    @meta        = Deplate::.new(deplate, self)
    @attributes  = {}
    # @metadata    = {}
    @destination = nil
    @index       = nil
    @output      = nil
    if inherited_output
        @pre_matter  = inherited_output.pre_matter.dup
        @body        = @@body_template.dup
        @post_matter = inherited_output.post_matter.dup
    else
        @pre_matter  = @@pre_matter_template.dup
        @body        = @@body_template.dup
        @post_matter = @@post_matter_template.dup
    end
    @top_heading     = nil
    @body_empty_done = false
    reset
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/deplate/output.rb', line 27

def attributes
  @attributes
end

#bodyObject

Returns the value of attribute body.



24
25
26
# File 'lib/deplate/output.rb', line 24

def body
  @body
end

#destinationObject

Returns the value of attribute destination.



26
27
28
# File 'lib/deplate/output.rb', line 26

def destination
  @destination
end

#indexObject

Returns the value of attribute index.



25
26
27
# File 'lib/deplate/output.rb', line 25

def index
  @index
end

#post_matterObject

Returns the value of attribute post_matter.



24
25
26
# File 'lib/deplate/output.rb', line 24

def post_matter
  @post_matter
end

#pre_matterObject

Returns the value of attribute pre_matter.



24
25
26
# File 'lib/deplate/output.rb', line 24

def pre_matter
  @pre_matter
end

#top_headingObject

Returns the value of attribute top_heading.



25
26
27
# File 'lib/deplate/output.rb', line 25

def top_heading
  @top_heading
end

Class Method Details

.resetObject



43
44
45
46
47
48
# File 'lib/deplate/output.rb', line 43

def reset
    @@pre_matter_template  = Array.new
    @@body_template        = Array.new
    @@post_matter_template = Array.new
    @@idx = 0
end

Instance Method Details

#add_at(type, pos, *text) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/deplate/output.rb', line 203

def add_at(type, pos, *text)
    text = text.compact
    unless text.empty?
        negative = false
        pos, arr = pos_and_array(type, pos)
        if pos
            negative = pos ? pos < 0 : false
            if text.first == :prepend
                negative = true
                text.shift
            end
            pos = pos.abs
            if arr[pos].nil?
                arr[pos] = text
            else
                for t in text
                    if block_given?
                        arr[pos] = yield(arr[pos], t)
                    elsif negative
                        arr[pos].unshift(t)
                    else
                        arr[pos] << t
                    end
                end
            end
            @formatter.join_blocks(text)
        else
            log(["No slot", text], :error)
        end
    end
end

#body_empty?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
# File 'lib/deplate/output.rb', line 130

def body_empty?
    @body_empty = @body.compact.flatten.empty?
    log(["Body is empty", caller[0..5]], :debug) if @body_empty
    @body_empty
end

#body_flushObject



85
86
87
88
89
90
91
# File 'lib/deplate/output.rb', line 85

def body_flush
    call_methods_matching(@formatter, /^hook_pre_body_flush_/)
    save!
    log(['Flushing formatted elements', @destination], :debug)
    process
    call_methods_matching(@formatter, /^hook_post_body_flush_/)
end

#do_i_feel_empty?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
# File 'lib/deplate/output.rb', line 136

def do_i_feel_empty?
    if @body_empty_done
        @body_empty
    else
        @body_empty_done = true
        body_empty?
    end
end

#empty_at?(type, pos) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
# File 'lib/deplate/output.rb', line 197

def empty_at?(type, pos)
    pos, arr = pos_and_array(type, pos)
    slot = arr[pos]
    return slot.nil? || slot.empty?
end

#fill_in_template(template_file) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/deplate/output.rb', line 158

def fill_in_template(template_file)
    unless do_i_feel_empty?
        t = Deplate::Template.new(:file => template_file, :master => @deplate)
        @output = t.fill_in(@deplate, 
                            :pre  => @pre_matter,
                            :body => @body,
                            :post => @post_matter)
    end
end

#flatten!Object



152
153
154
155
156
# File 'lib/deplate/output.rb', line 152

def flatten!
    unless do_i_feel_empty?
        @output = [@pre_matter, @body, @post_matter].flatten.compact
    end
end

#join(sep) ⇒ Object



168
169
170
# File 'lib/deplate/output.rb', line 168

def join(sep)
    @output.join(sep) if @output
end

#peel!Object

private :do_i_feel_empty?



146
147
148
149
150
# File 'lib/deplate/output.rb', line 146

def peel!
    unless do_i_feel_empty?
        @output = @body.flatten.compact
    end
end

#pos_and_array(type, pos) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/deplate/output.rb', line 172

def pos_and_array(type, pos)
    if type == :array
        arr = [pos]
        pos = 0
    else
        pos = slot_by_name(pos)
        unless pos or pos == 0
            log(["Undefined slot", pos, type], :error) if pos.nil?
            return
        end
        case type
        when :body, "body"
            arr = @body
        when :pre, "preMatter", "pre"
            arr = @pre_matter
        when :post, "postMatter", "post"
            arr = @post_matter
        when :array
        else
            raise "Unknown type: #{type}"
        end
    end
    return pos, arr
end

#processObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/deplate/output.rb', line 106

def process
    if @options.included
        peel!
    else
        tmpl = @options.template || @variables['template']
        if tmpl
            if File.exist?(tmpl)
                fill_in_template(tmpl)
            else
                template_file = @templates[tmpl]
                if template_file
                    log(['Using template', template_file], :debug)
                    fill_in_template(template_file)
                else
                    log(['Unknown template', tmpl], :error)
                    flatten!
                end
            end
        else
            flatten!
        end
    end
end

#resetObject Also known as: simulate_reset



78
79
80
81
82
# File 'lib/deplate/output.rb', line 78

def reset
    @attributes[:stepwiseIdx] = 0
    @attributes[:consumed_labels] = []
    @attributes[:consumed_ids] = []
end

#save!Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/deplate/output.rb', line 93

def save!
    unless do_i_feel_empty?
        call_methods_matching(@formatter, /^hook_pre_output_save_/)
        @index       ||= @top_heading.top_index
        @destination ||= @top_heading.destination
        @pre_matter    = @pre_matter.dup
        @body          = @body.dup
        @post_matter   = @post_matter.dup
        call_methods_matching(@formatter, /^hook_post_output_save_/)
    end
    log(['Save output', destination], :debug)
end

#set_at(type, pos, *text) ⇒ Object



245
246
247
248
249
# File 'lib/deplate/output.rb', line 245

def set_at(type, pos, *text)
    add_at(type, pos, *text) do |arr, t|
        t
    end
end

#union_at(type, pos, *text) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/deplate/output.rb', line 235

def union_at(type, pos, *text)
    add_at(type, pos, *text) do |arr, t|
        if !arr.include?(t)
            arr << t
        else
            arr
        end
    end
end