mirror of
https://github.com/kemko/liquid.git
synced 2026-01-03 00:35:40 +03:00
Compare commits
2 Commits
render-for
...
truffle-na
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
350addf364 | ||
|
|
49488e3757 |
10
Gemfile
10
Gemfile
@@ -9,9 +9,9 @@ group :benchmark, :test do
|
|||||||
gem 'benchmark-ips'
|
gem 'benchmark-ips'
|
||||||
gem 'memory_profiler'
|
gem 'memory_profiler'
|
||||||
|
|
||||||
install_if -> { RUBY_PLATFORM !~ /mingw|mswin|java/ } do
|
# install_if -> { RUBY_PLATFORM !~ /mingw|mswin|java/ } do
|
||||||
gem 'stackprof'
|
# gem 'stackprof'
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
group :test do
|
group :test do
|
||||||
@@ -21,3 +21,7 @@ group :test do
|
|||||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '9168659de45d6d576fce30c735f857e597fa26f6'
|
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '9168659de45d6d576fce30c735f857e597fa26f6'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# gem "byebug", "~> 11.0"
|
||||||
|
|
||||||
|
gem "pry", "~> 0.12.2"
|
||||||
|
|||||||
6
Rakefile
6
Rakefile
@@ -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
|
||||||
|
|||||||
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
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
require 'liquid/ast_to_portable_json'
|
||||||
module Liquid
|
module Liquid
|
||||||
# Templates are central to liquid.
|
# Templates are central to liquid.
|
||||||
# Interpretating templates is a two step process. First you compile the
|
# Interpretating templates is a two step process. First you compile the
|
||||||
@@ -130,6 +131,8 @@ module Liquid
|
|||||||
@line_numbers = options[:line_numbers] || @profiling
|
@line_numbers = options[:line_numbers] || @profiling
|
||||||
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
|
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
|
||||||
@root = Document.parse(tokenize(source), parse_context)
|
@root = Document.parse(tokenize(source), parse_context)
|
||||||
|
@json = ASTToPortableJSON.dump(self)
|
||||||
|
|
||||||
@warnings = parse_context.warnings
|
@warnings = parse_context.warnings
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
@@ -198,6 +201,15 @@ module Liquid
|
|||||||
context.add_filters(args.pop)
|
context.add_filters(args.pop)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
unless @truffle_context
|
||||||
|
environment = context.environments.first
|
||||||
|
@truffle_context = Polyglot.eval('liquid', @json)
|
||||||
|
environment.each do |key, value|
|
||||||
|
@truffle_context[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return @truffle_context[:render].call
|
||||||
|
|
||||||
# Retrying a render resets resource usage
|
# Retrying a render resets resource usage
|
||||||
context.resource_limits.reset
|
context.resource_limits.reset
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
require 'bundler/setup'
|
||||||
require 'benchmark/ips'
|
require 'benchmark/ips'
|
||||||
require_relative 'theme_runner'
|
require_relative 'theme_runner'
|
||||||
|
|
||||||
@@ -5,14 +6,15 @@ Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
|
|||||||
profiler = ThemeRunner.new
|
profiler = ThemeRunner.new
|
||||||
|
|
||||||
Benchmark.ips do |x|
|
Benchmark.ips do |x|
|
||||||
x.time = 10
|
x.time = 60
|
||||||
x.warmup = 5
|
x.warmup = 1
|
||||||
|
|
||||||
puts
|
puts
|
||||||
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
|
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
|
||||||
puts
|
puts
|
||||||
|
|
||||||
x.report("parse:") { profiler.compile }
|
profiler.compile
|
||||||
|
# x.report("parse:") { profiler.compile }
|
||||||
x.report("render:") { profiler.render }
|
x.report("render:") { profiler.render }
|
||||||
x.report("parse & render:") { profiler.run }
|
x.report("parse & render:") { profiler.run }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class ThemeRunner
|
|||||||
# Initialize a new liquid ThemeRunner instance
|
# Initialize a new liquid ThemeRunner instance
|
||||||
# Will load all templates into memory, do this now so that we don't profile IO.
|
# Will load all templates into memory, do this now so that we don't profile IO.
|
||||||
def initialize
|
def initialize
|
||||||
@tests = Dir[__dir__ + '/tests/**/*.liquid'].collect do |test|
|
@tests = Dir[__dir__ + '/tests/ripen/product.liquid'].collect do |test|
|
||||||
next if File.basename(test) == 'theme.liquid'
|
next if File.basename(test) == 'theme.liquid'
|
||||||
|
|
||||||
theme_path = File.dirname(test) + '/theme.liquid'
|
theme_path = File.dirname(test) + '/theme.liquid'
|
||||||
|
|||||||
Reference in New Issue
Block a user