-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This enables traversal over whole document tree.
- Loading branch information
1 parent
53b8bab
commit b325070
Showing
10 changed files
with
372 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
module Liquid | ||
class Traversal | ||
def self.for(node, callbacks = Hash.new(proc {})) | ||
kase = CASES.find { |(klass, _)| node.is_a?(klass) }&.last | ||
(kase || self).new(node, callbacks) | ||
end | ||
|
||
def initialize(node, callbacks) | ||
@node = node | ||
@callbacks = callbacks | ||
end | ||
|
||
def callback_for(*classes, &block) | ||
cb = block | ||
cb = ->(node, _) { block[node] } if block.arity.abs == 1 | ||
cb = ->(_, _) { block[] } if block.arity.zero? | ||
classes.each { |klass| @callbacks[klass] = cb } | ||
self | ||
end | ||
|
||
def traverse(context = nil) | ||
children.map do |node| | ||
item, new_context = @callbacks[node.class][node, context] | ||
[ | ||
item, | ||
Traversal.for(node, @callbacks).traverse( | ||
new_context.nil? ? context : new_context | ||
) | ||
] | ||
end | ||
end | ||
|
||
protected | ||
|
||
def children | ||
@node.respond_to?(:nodelist) ? Array(@node.nodelist) : [] | ||
end | ||
|
||
class Assign < Traversal | ||
def children | ||
[@node.from] | ||
end | ||
end | ||
|
||
class Case < Traversal | ||
def children | ||
[@node.left] + @node.blocks | ||
end | ||
end | ||
|
||
class Condition < Traversal | ||
def children | ||
[ | ||
@node.left, @node.right, | ||
@node.child_condition, @node.attachment | ||
].compact | ||
end | ||
end | ||
|
||
class Cycle < Traversal | ||
def children | ||
Array(@node.variables) | ||
end | ||
end | ||
|
||
class For < Traversal | ||
def children | ||
(super + [@node.limit, @node.from, @node.collection_name]).compact | ||
end | ||
end | ||
|
||
class If < Traversal | ||
def children | ||
@node.blocks | ||
end | ||
end | ||
|
||
class Include < Traversal | ||
def children | ||
[ | ||
@node.template_name_expr, | ||
@node.variable_name_expr | ||
] + @node.attributes.values | ||
end | ||
end | ||
|
||
class TableRow < Traversal | ||
def children | ||
super + @node.attributes.values + [@node.collection_name] | ||
end | ||
end | ||
|
||
class Variable < Traversal | ||
def children | ||
[@node.name] + @node.filters.flatten | ||
end | ||
end | ||
|
||
class VariableLookup < Traversal | ||
def children | ||
@node.lookups | ||
end | ||
end | ||
|
||
CASES = { | ||
Liquid::Assign => Assign, | ||
Liquid::Case => Case, | ||
Liquid::Condition => Condition, | ||
Liquid::Cycle => Cycle, | ||
Liquid::For => For, | ||
Liquid::If => If, | ||
Liquid::Include => Include, | ||
Liquid::TableRow => TableRow, | ||
Liquid::Variable => Variable, | ||
Liquid::VariableLookup => VariableLookup | ||
}.freeze | ||
end | ||
end |
Oops, something went wrong.