lib/erbook/document.rb in erbook-7.3.0 vs lib/erbook/document.rb in erbook-8.0.0
- old
+ new
@@ -81,48 +81,58 @@
# Handles the method call from a node
# placeholder in the input document.
#
def sandbox.__node_impl__ node_type, *node_args, &node_content
node = Node.new(
- :type => node_type,
- :defn => @format['nodes'][node_type],
- :args => node_args,
- :trace => caller,
- :children => []
+ :type => node_type,
+ :definition => @format['nodes'][node_type],
+ :arguments => node_args,
+ :backtrace => caller,
+ :parent => @stack.last,
+ :children => []
)
+
+ Array(node.definition['params']).each do |param|
+ break if node_args.empty?
+ node.__send__ "#{param}=", node_args.shift
+ end
+
@nodes << node
@nodes_by_type[node.type] << node
- # calculate occurrence number for this node
- if node.defn['number']
+ # calculate ordinal number for this node
+ if node.ordinal_number?
@count_by_type ||= Hash.new {|h,k| h[k] = 0 }
- node.number = (@count_by_type[node.type] += 1)
+ node.ordinal_number = (@count_by_type[node.type] += 1)
end
# assign node family
- if parent = @stack.last
+ if parent = node.parent
parent.children << node
node.parent = parent
node.depth = parent.depth
- node.depth += 1 if node.defn['depth']
+ node.depth += 1 if node.anchor?
- # calculate latex-style index number for this node
- if node.defn['index']
- ancestry = @stack.reverse.find {|n| n.defn['index'] }.index
- branches = node.parent.children.select {|n| n.index }
+ # calculate section number for this node
+ if node.section_number?
+ ancestor = @stack.reverse.find {|n| n.section_number }
+ branches = parent.children.select {|n| n.section_number }
- node.index = [ancestry, branches.length + 1].join('.')
+ node.section_number = [
+ ancestor.section_number,
+ branches.length + 1
+ ].join('.')
end
else
@roots << node
node.parent = nil
node.depth = 0
- # calculate latex-style index number for this node
- if node.defn['index']
- branches = @roots.select {|n| n.index }
- node.index = (branches.length + 1).to_s
+ # calculate section number for this node
+ if node.section_number?
+ branches = @roots.select {|n| n.section_number }
+ node.section_number = (branches.length + 1).to_s
end
end
# assign node content
if block_given?
@@ -151,13 +161,11 @@
# evaluate the input & build the document tree
template.render
@processed_document = template.buffer
# chain block-level nodes together for local navigation
- block_nodes = @nodes.reject do |n|
- n.defn['bypass'] || n.defn['inline']
- end
+ block_nodes = @nodes.select {|n| n.anchor? }
require 'enumerator'
block_nodes.each_cons(2) do |a, b|
a.next_node = b
b.prev_node = a
@@ -177,20 +185,24 @@
n.children.each {|c| visitor.call c }
# calculate the output for this node
actual_output = Template.new(
"#{@format_file}:nodes:#{n.type}:output",
- n.defn['output'].to_s.chomp
+ n.definition['output'].to_s.chomp
).render_with(@template_vars.merge(:@node => n))
# reveal child nodes' actual output in this node's actual output
n.children.each do |c|
- if c.defn['inline'] && !c.defn['bypass']
+ if c.silent?
+ # this child's output is not meant to be revealed at this time
+ next
+
+ elsif c.inline?
actual_output[c.output] = actual_output_by_node[c]
else
- # pull block-level node out of paragraph tag added by Maruku
+ # pull block-level child out of paragraph tag added by Maruku
actual_output.sub! %r/(<p>\s*)?#{Regexp.quote c.output}/ do
actual_output_by_node[c] + $1.to_s
end
end
end
@@ -237,10 +249,34 @@
##
# Returns the output of this node.
#
def to_s
- defn['silent'] ? '' : output
+ if silent?
+ ''
+ else
+ output
+ end
+ end
+
+ def section_number?
+ Array(definition['number']).include? 'section'
+ end
+
+ def ordinal_number?
+ Array(definition['number']).include? 'ordinal'
+ end
+
+ def anchor?
+ not inline? and not silent?
+ end
+
+ def inline?
+ definition['inline']
+ end
+
+ def silent?
+ definition['silent']
end
end
private