mirror of
https://github.com/kemko/liquid.git
synced 2026-01-03 16:55:40 +03:00
Add rake dump_portable_ast
This commit is contained in:
6
Rakefile
6
Rakefile
@@ -100,3 +100,9 @@ end
|
||||
task :console do
|
||||
exec 'irb -I lib -r liquid'
|
||||
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
|
||||
|
||||
27
lib/liquid/ast_to_portable_json.rb
Normal file
27
lib/liquid/ast_to_portable_json.rb
Normal file
@@ -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