Class: TraceryNode

Inherits:
Object
  • Object
show all
Includes:
Tracery
Defined in:
lib/tracery.rb

Constant Summary

Constants included from Tracery

Tracery::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tracery

#createGrammar, #createSection, #parse, #parseTag, random, resetRnd, setRnd

Constructor Details

#initialize(parent, childIndex, settings) ⇒ TraceryNode

Returns a new instance of TraceryNode.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tracery.rb', line 181

def initialize(parent, childIndex, settings)
    @errors = []
    @children = []

    if(settings[:raw].nil?) then
        @errors << "Empty input for node"
        settings[:raw] = ""
    end
    
    # If the root node of an expansion, it will have the grammar passed as the 'parent'
    # set the grammar from the 'parent', and set all other values for a root node
    if(parent.is_a? Grammar)
        @grammar = parent
        @parent = nil
        @depth = 0
        @childIndex = 0
    else
        @grammar = parent.grammar
        @parent = parent
        @depth = parent.depth + 1
        @childIndex = childIndex
    end

    @raw = settings[:raw]
    @type = settings[:type]
    @isExpanded = false
    
    @errors << "No grammar specified for this node #{self}" if (@grammar.nil?)
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



177
178
179
# File 'lib/tracery.rb', line 177

def children
  @children
end

#depthObject

Returns the value of attribute depth.



177
178
179
# File 'lib/tracery.rb', line 177

def depth
  @depth
end

#errorsObject

Returns the value of attribute errors.



177
178
179
# File 'lib/tracery.rb', line 177

def errors
  @errors
end

#finishedTextObject

Returns the value of attribute finishedText.



177
178
179
# File 'lib/tracery.rb', line 177

def finishedText
  @finishedText
end

#grammarObject

Returns the value of attribute grammar.



177
178
179
# File 'lib/tracery.rb', line 177

def grammar
  @grammar
end

Instance Method Details

#allErrorsObject



331
332
333
334
# File 'lib/tracery.rb', line 331

def allErrors
    child_errors = @children.inject([]){|all, child| all.concat(child.allErrors)}
    return child_errors.concat(@errors) 
end

#clearEscapeCharactersObject



336
337
338
# File 'lib/tracery.rb', line 336

def clearEscapeCharacters
    @finishedText = @finishedText.gsub(/\\\\/, "DOUBLEBACKSLASH").gsub(/\\/, "").gsub(/DOUBLEBACKSLASH/, "\\")
end

#expand(preventRecursion = false) ⇒ Object

Expand this rule (possibly creating children)



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/tracery.rb', line 240

def expand(preventRecursion = false)
    if(@isExpanded) then
        @errors << "Already expanded #{self}"
        return
    end

    @isExpanded = true
    #this is no longer used
    @expansionErrors = []
            
    # Types of nodes
    # -1: raw, needs parsing
    #  0: Plaintext
    #  1: Tag ("#symbol.mod.mod2.mod3#" or "#[pushTarget:pushRule]symbol.mod#")
    #  2: Action ("[pushTarget:pushRule], [pushTarget:POP]", more in the future)
    
    case(@type)
        when -1 then
            #raw rule
            expandChildren(@raw, preventRecursion)
        when 0 then
            #plaintext, do nothing but copy text into finished text
            @finishedText = @raw
        when 1 then
            #tag - Parse to find any actions, and figure out what the symbol is
            @preactions = []
            @postactions = []
            parsed = parseTag(@raw)
            @symbol = parsed[:symbol]
            @modifiers = parsed[:modifiers]

            # Create all the preactions from the raw syntax
            @preactions = parsed[:preactions].map{|preaction|
                NodeAction.new(self, preaction[:raw])
            }

            # @postactions = parsed[:preactions].map{|postaction|
            #     NodeAction.new(self, postaction.raw)
            # }
            
            # Make undo actions for all preactions (pops for each push)
            @postactions = @preactions.
                            select{|preaction| preaction.type == 0 }.
                            map{|preaction| preaction.createUndo() }
            
            @preactions.each { |preaction| preaction.activate }
            
            @finishedText = @raw

            # Expand (passing the node, this allows tracking of recursion depth)
            selectedRule = @grammar.selectRule(@symbol, self, @errors)

            expandChildren(selectedRule, preventRecursion)
            
            # Apply modifiers
            # TODO: Update parse function to not trigger on hashtags within parenthesis within tags,
            # so that modifier parameters can contain tags "#story.replace(#protagonist#, #newCharacter#)#"
            @modifiers.each{|modName|
                modParams = [];
                if (modName.include?("(")) then
                    #match something like `modifier(param, param)`, capture name and params separately
                    match = /([^\(]+)\(([^)]+)\)/.match(modName)
                    if(!match.nil?) then
                        modParams = match.captures[1].split(",")
                        modName = match.captures[0]
                    end
                end

                mod = @grammar.modifiers[modName]

                # Missing modifier?
                if(mod.nil?)
                    @errors << "Missing modifier #{modName}"
                    @finishedText += "((.#{modName}))"
                else
                    @finishedText = mod.call(@finishedText, modParams)
                end
            }
            # perform post-actions
            @postactions.each{|postaction| postaction.activate()}
        when 2 then
            # Just a bare action? Expand it!
            @action = NodeAction.new(self, @raw)
            @action.activate()
            
            # No visible text for an action
            # TODO: some visible text for if there is a failure to perform the action?
            @finishedText = ""
    end
end

#expandChildren(childRule, preventRecursion) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/tracery.rb', line 215

def expandChildren(childRule, preventRecursion)
    @finishedText = ""
    @childRule = childRule
    
    if(!@childRule.nil?)
        parsed = parse(childRule)
        sections = parsed[:sections]

        @errors.concat(parsed[:errors])

        sections.each_with_index do |section, i|
            child = TraceryNode.new(self, i, section)
            if(!preventRecursion)
                child.expand(preventRecursion)
            end
            @finishedText += child.finishedText
            @children << child
        end
    else
        # In normal operation, this shouldn't ever happen
        @errors << "No child rule provided, can't expand children"
    end
end

#to_sObject



211
212
213
# File 'lib/tracery.rb', line 211

def to_s
    "Node('#{@raw}' #{@type} d:#{@depth})"
end