mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Add rake dump_portable_ast
This commit is contained in:
@@ -100,3 +100,9 @@ end
|
|||||||
task :console do
|
task :console do
|
||||||
exec 'irb -I lib -r liquid'
|
exec 'irb -I lib -r liquid'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
task :dump_portable_ast, [:filename] do |_task, args|
|
||||||
|
require 'liquid/ast_to_portable_json'
|
||||||
|
ast = Liquid::Template.parse(File.read(args[:filename]))
|
||||||
|
puts(Liquid::ASTToPortableJSON.dump(ast))
|
||||||
|
end
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
require 'yaml'
|
||||||
|
require 'json'
|
||||||
|
require 'liquid'
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
class ASTToPortableJSON
|
||||||
|
def self.dump(ast)
|
||||||
|
yaml = YAML.dump(ast)
|
||||||
|
yaml.gsub!(/---.*/, '')
|
||||||
|
yaml.gsub!(/!ruby\/object:(.+)\n(\s+)/, "\n\\2class_name: \\1\n\\2")
|
||||||
|
|
||||||
|
bare_hash_ast = YAML.load(yaml)
|
||||||
|
delete_parse_context(bare_hash_ast)
|
||||||
|
JSON.pretty_generate(bare_hash_ast)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.delete_parse_context(bare_hash_ast)
|
||||||
|
if bare_hash_ast.is_a?(Array)
|
||||||
|
bare_hash_ast.each { |v| delete_parse_context(v) }
|
||||||
|
elsif bare_hash_ast.is_a?(Hash)
|
||||||
|
bare_hash_ast.delete('parse_context')
|
||||||
|
bare_hash_ast.each { |k, v| delete_parse_context(v) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Reference in New Issue
Block a user