Class: Glyph::MacroNode

Inherits:
SyntaxNode show all
Defined in:
lib/glyph/syntax_node.rb

Overview

A Glyph macro in Glyph Abstract Syntax Tree

Since:

  • 0.3.0

Instance Attribute Summary

Attributes inherited from Node

#children, #parent

Instance Method Summary collapse

Methods inherited from SyntaxNode

#inspect, #parent_macro

Methods inherited from Node

#&, #<<, #==, #>>, #ascend, #child, #clear, #descend, #each_child, #find_child, #find_parent, #from, #initialize, #inspect, #root, #to_hash, #to_node

Methods inherited from Hash

#to_node, #to_options

Constructor Details

This class inherits a constructor from Node

Instance Method Details

#attribute(name) ⇒ Glyph::AttributeNode? Also known as: attr

Returns the attribute syntax node with the specified name

Parameters:

  • name (Symbol)

    the name of the attribute

Returns:

Since:

  • 0.3.0



120
121
122
# File 'lib/glyph/syntax_node.rb', line 120

def attribute(name)
	attributes.select{|n| n[:name] == name}[0]
end

#attributesArray<Glyph::AttributeNode> Also known as: attrs

Returns an array of the child attribute nodes.

Returns:

Since:

  • 0.3.0



112
113
114
# File 'lib/glyph/syntax_node.rb', line 112

def attributes
	children.select{|n| n.is_a? AttributeNode }
end

#child_macrosArray<Glyph::MacroNode>

Returns an array of the child macro nodes.

Returns:

Since:

  • 0.4.0



88
89
90
91
92
93
94
# File 'lib/glyph/syntax_node.rb', line 88

def child_macros
	macros = []
	parameters.each do |p|
		macros += p.children.select{|n| n.is_a? MacroNode }
	end
	macros
end

#contentsString

Returns a textual representation of the macro parameters.

Returns:

  • (String)

    a textual representation of the macro parameters.

Since:

  • 0.3.0



69
70
71
# File 'lib/glyph/syntax_node.rb', line 69

def contents
	attributes.join+parameters.join("|")
end

#dispatch(node) ⇒ Object

Calls the ruby block stored in the :dispatch key, if present, in the context of node

Since:

  • 0.5.0



160
161
162
163
# File 'lib/glyph/syntax_node.rb', line 160

def dispatch(node)
	return self[:dispatch].call node if self[:dispatch]
	false
end

#evaluate(context, options = {}) ⇒ String

Expands the macro

Returns:

  • (String)

    the value of the macro

Since:

  • 0.3.0



76
77
78
# File 'lib/glyph/syntax_node.rb', line 76

def evaluate(context, options={})
	self[:value] = expand(context)
end

#expand(context) ⇒ String

Expands the macro corresponding to self

Parameters:

Returns:

  • (String)

    the value of the macro

Since:

  • 0.3.0



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/glyph/syntax_node.rb', line 133

def expand(context)
	self[:source] = context[:source]
	self[:document] = context[:document] 
	self[:info] = context[:info]
	self[:value] = ""
	dispatched = parent_macro.dispatch(self) if parent_macro
	return dispatched if dispatched
	if Glyph['options.macro_set'] == "xml" || Glyph::MACROS[self[:name]].blank? && Glyph['options.xml_fallback'] then
		m = Glyph::MacroNode.new
		m[:name] = :xml
		Glyph::Macro.new(m).expand
		return m[:dispatch].call self
	end
	Glyph::Macro.new(self).expand
end

#parameter(n) ⇒ Glyph::ParameterNode? Also known as: param

Returns the parameter syntax node at the specified index

Parameters:

  • n (Fixnum)

    the index of the parameter

Returns:

Since:

  • 0.3.0



100
101
102
# File 'lib/glyph/syntax_node.rb', line 100

def parameter(n)
	parameters[n]
end

#parametersArray<Glyph::ParameterNode> Also known as: params

Returns an array of the child parameter nodes.

Returns:

Since:

  • 0.3.0



82
83
84
# File 'lib/glyph/syntax_node.rb', line 82

def parameters
	children.select{|n| n.is_a? ParameterNode }
end

#sourceObject

Returns where the macro was used (used in Glyph::Analyzer)

Since:

  • 0.4.0



151
152
153
154
155
# File 'lib/glyph/syntax_node.rb', line 151

def source
	s = self[:source][:file] rescue nil 
	s ||= self[:source][:name] rescue nil
	s
end

#to_sString

Returns a textual representation of the macro.

Returns:

  • (String)

    a textual representation of the macro

Since:

  • 0.3.0



62
63
64
65
# File 'lib/glyph/syntax_node.rb', line 62

def to_s
	e = self[:escape] ? "=" : ""
	"#{self[:name]}["+e+contents+e+"]"
end

#valueObject

Equivalent to Glyph::MacroNode#parameter(0).

Since:

  • 0.3.0



106
107
108
# File 'lib/glyph/syntax_node.rb', line 106

def value
	parameter(0)
end