lib/glyph/macro.rb in glyph-0.3.0 vs lib/glyph/macro.rb in glyph-0.4.0
- old
+ new
@@ -6,27 +6,35 @@
# The Macro class contains shortcut methods to access the current node and document, as well as other
# useful methods to be used in macro definitions.
class Macro
include Validators
+ include Helpers
+ include Utils
- attr_reader :node, :source
+ attr_reader :node, :source_name, :source_file, :source_topic
# Creates a new macro instance from a Node
# @param [Node] node a node populated with macro data
def initialize(node)
@node = node
@name = @node[:name]
@updated_source = nil
- @source = @node[:source][:name] rescue "--"
+ @source_name = @node[:source][:name] || nil rescue "--"
+ @source_topic = @node[:source][:topic] || nil rescue "--"
+ @source_file = @node[:source][:file] rescue nil
end
# Resets the name of the updated source (call before calling
# Macro#interpret)
# @param [String] name the source name
- def update_source(name)
- @updated_source = {:node => @node, :name => name}
+ # @param [String] file the source file
+ # @param [String] topic the topic file
+ # @since 0.3.0
+ def update_source(name, file=nil, topic=nil)
+ file ||= @node[:source][:file] rescue nil
+ @updated_source = {:name => name, :file => file, :topic => topic}
end
# Returns a Glyph code representation of the specified parameter
# @param [Fixnum] n the index of the parameter
# @return [String, nil] the string representation of the parameter
@@ -178,11 +186,11 @@
# @since 0.2.0
def macro_warning(msg, e=nil)
if e.is_a?(Glyph::MacroError) then
e.display
else
- message = "#{msg}\n source: #{@source}\n path: #{path}"
+ message = "#{msg}\n source: #{@source_name}\n path: #{path}"
if Glyph.debug? then
message << %{\n#{"-"*54}\n#{@node.to_s.gsub(/\t/, ' ')}\n#{"-"*54}}
if e then
message << "\n"+"-"*20+"[ Backtrace: ]"+"-"*20
message << "\n"+e.backtrace.join("\n")
@@ -204,9 +212,10 @@
context[:source] = @updated_source || @node[:source]
context[:embedded] = true
context[:document] = @node[:document]
interpreter = Glyph::Interpreter.new string, context
subtree = interpreter.parse
+ subtree[:source] = context[:source]
@node << subtree
result = interpreter.document.output
end
result.gsub(/\\*([\[\]])/){"\\#$1"}
result