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.



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
77
# File 'lib/deplate/output.rb', line 52

def initialize(deplate, inherited_output=nil)
    @@idx += 1
    @deplate     = deplate
    @formatter   = deplate.formatter
    @options     = deplate.options
    @variables   = deplate.variables
    @templates   = deplate.templates
    @meta        = Deplate::Metadata.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.



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

def attributes
  @attributes
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#destinationObject

Returns the value of attribute destination.



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

def destination
  @destination
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#post_matterObject

Returns the value of attribute post_matter.



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

def post_matter
  @post_matter
end

#pre_matterObject

Returns the value of attribute pre_matter.



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

def pre_matter
  @pre_matter
end

#top_headingObject

Returns the value of attribute top_heading.



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

def top_heading
  @top_heading
end

Class Method Details

.resetObject



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

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



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
234
# File 'lib/deplate/output.rb', line 204

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)


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

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



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

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)


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

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)


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

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



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

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



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

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

#join(sep) ⇒ Object



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

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

#peel!Object

private :do_i_feel_empty?



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

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

#pos_and_array(type, pos) ⇒ Object



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

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



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

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



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

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

#save!Object



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

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



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

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

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



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

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