lib/opulent/compiler.rb in opulent-1.5.5 vs lib/opulent/compiler.rb in opulent-1.6.0
- old
+ new
@@ -24,21 +24,24 @@
#
# [:node_type, :value, :attributes, :children, :indent]
#
# @param path [String] Current file path needed for include nodes
#
- def initialize
+ def initialize(settings = {})
# Setup convention accessors
@type = 0
@value = 1
@options = 2
@children = 3
@indent = 4
+ # Inherit settings from Engine
+ @settings = settings
+
# Get special node types from the settings
- @multi_node = Settings::MultiNode
- @inline_node = Settings::InlineNode
+ @multi_node = Settings::MULTI_NODE
+ @inline_node = Settings::INLINE_NODE
# Initialize amble object
@template = [[:preamble]]
# Incrmental counters
@@ -61,23 +64,28 @@
end
# Compile input nodes, replace them with their definitions and
#
# @param root_node [Array] Root node containing all document nodes
- # @param context [Context] Context holding environment variables
#
- def compile(root_node, context = nil)
+ def compile(root_node, definitions = {})
# Compiler generated code
@code = ''
@generator = ''
+ @definitions = definitions
# Set initial parent, from which we start generating code
@sibling_stack << root_node[@children].size
+ # Write all node definitions as method defs
+ @definitions.each do |_, node|
+ define node
+ end
+
# Start building up the code from the root node
root_node[@children].each do |child|
- root child, 0, context
+ root child, 0
end
@template << [:postamble]
templatize
@@ -94,12 +102,10 @@
# @param text [String] Input text to be indented
# @param indent [String] Indentation string to be appended
#
def indent_lines(text, indent)
text ||= ''
- text.lines.inject('') do |result, line|
- result += indent + line
- end
+ text.lines.map { |line| indent + line }.join
end
# Give an explicit error report where an unexpected sequence of tokens
# appears and give indications on how to solve it
#