quintype-liquid 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/assets/javascripts/quintype-liquid.js.erb +34 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/quintype/liquid/file_system.rb +53 -0
- data/lib/quintype/liquid/liquid_template_caching_module.rb +27 -0
- data/lib/quintype/liquid/railtie.rb +19 -0
- data/lib/quintype/liquid/templates.rb +28 -0
- data/lib/quintype/liquid/version.rb +5 -0
- data/lib/quintype/liquid.rb +10 -0
- data/quintype-liquid.gemspec +24 -0
- data/vendor/assets/javascripts/liquid.js +1532 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec1618eae00d20307f8212f342662f4e968f0db5
|
4
|
+
data.tar.gz: d329247a957484ff9d44995b4bc11ab6eae62b43
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 951676d58b5e83dd10b9ecada3c65b92984626c2295238b7fe30a290e39aaac3ebf71b4a7ecd84e47a0fa42d9a2553b8e5ef5b11285f696da832298d0f294761
|
7
|
+
data.tar.gz: fe61a6a72b26ed2fdafa9aea04269aece3c4fd9984a8110336857fc587fbdfbb94585d5bbf867e2aaadb74d2c61d3d4a91ed94d721bf59ad6d47ea67aae9cc96
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Quintype::Liquid
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/quintype/liquid`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'quintype-liquid'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install quintype-liquid
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quintype-liquid.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
//= require liquid
|
2
|
+
|
3
|
+
(function() {
|
4
|
+
|
5
|
+
<% loader = Quintype::Liquid::Templates.new %>
|
6
|
+
<% loader.paths.each do |path| %>
|
7
|
+
<% depend_on path %>
|
8
|
+
<% end %>
|
9
|
+
var Templates = <%= loader.templates.to_json %>;
|
10
|
+
|
11
|
+
Liquid.readTemplateFile = function(path) {
|
12
|
+
var partialPath, template;
|
13
|
+
partialPath = replaceLast(path, '/', '/_');
|
14
|
+
template = Templates[path] || Templates[partialPath];
|
15
|
+
if (template) {
|
16
|
+
return template;
|
17
|
+
} else {
|
18
|
+
throw "Cannot find template! " + path;
|
19
|
+
}
|
20
|
+
|
21
|
+
function replaceLast(string, from, to) {
|
22
|
+
var last, pieces;
|
23
|
+
pieces = string.split(from);
|
24
|
+
last = pieces.pop();
|
25
|
+
return pieces.join(from) + to + last;
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
window.quintypeLiquid = {
|
30
|
+
templates: Templates,
|
31
|
+
parse: function(s) { return Liquid.parse(s); }
|
32
|
+
};
|
33
|
+
|
34
|
+
})();
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "quintype/liquid"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Quintype
|
2
|
+
module Liquid
|
3
|
+
class FileSystem < ::Liquid::LocalFileSystem
|
4
|
+
def caching_enabled?
|
5
|
+
Rails.env.production?
|
6
|
+
end
|
7
|
+
|
8
|
+
def read_file(path)
|
9
|
+
full_path = full_path(path)
|
10
|
+
File.read(full_path) if File.exists?(full_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
def read_template_file(template_path, context)
|
14
|
+
controller = context.registers[:controller]
|
15
|
+
controller_path = controller.controller_path
|
16
|
+
formats = controller.formats.map { |format| '.' + format.to_s } + ['']
|
17
|
+
|
18
|
+
file = nil
|
19
|
+
|
20
|
+
formats.each do |format|
|
21
|
+
path = template_path.include?('/') ? template_path : "#{controller_path}/#{template_path}"
|
22
|
+
path = path + format
|
23
|
+
|
24
|
+
file = caching_enabled? ? Rails.cache.fetch(cache_path(path)) { read_file(path) } : read_file(path)
|
25
|
+
|
26
|
+
break if file
|
27
|
+
end
|
28
|
+
|
29
|
+
raise Liquid::FileSystemError, "No such template '#{template_path}'" unless file
|
30
|
+
|
31
|
+
file
|
32
|
+
end
|
33
|
+
|
34
|
+
def full_path(template_path)
|
35
|
+
raise Liquid::FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /\A[^.\/][a-zA-Z0-9_\.\/]+\z/
|
36
|
+
|
37
|
+
full_path = if template_path.include?('/'.freeze)
|
38
|
+
File.join(root, File.dirname(template_path), @pattern % File.basename(template_path))
|
39
|
+
else
|
40
|
+
File.join(root, @pattern % template_path)
|
41
|
+
end
|
42
|
+
|
43
|
+
raise Liquid::FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /\A#{File.expand_path(root)}/
|
44
|
+
|
45
|
+
full_path
|
46
|
+
end
|
47
|
+
|
48
|
+
def cache_path(path)
|
49
|
+
"liquid:#{path}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Quintype
|
2
|
+
module Liquid
|
3
|
+
module LiquidTemplateCachingModule
|
4
|
+
CACHED_TEMPLATES = Concurrent::Map.new do |m, template|
|
5
|
+
Rails.logger.info "Compiling Template"
|
6
|
+
m[template] = ::Liquid::Template.parse(template)
|
7
|
+
end
|
8
|
+
|
9
|
+
def render(template, local_assigns = {})
|
10
|
+
@view.controller.headers['Content-Type'] ||= 'text/html; charset=utf-8'
|
11
|
+
|
12
|
+
assigns = if @controller.respond_to?(:liquid_assigns, true)
|
13
|
+
@controller.send(:liquid_assigns)
|
14
|
+
else
|
15
|
+
@view.assigns
|
16
|
+
end
|
17
|
+
assigns['content_for_layout'] = @view.content_for(:layout) if @view.content_for?(:layout)
|
18
|
+
assigns.merge!(local_assigns.stringify_keys)
|
19
|
+
|
20
|
+
|
21
|
+
liquid = LiquidTemplateCachingModule::CACHED_TEMPLATES[template]
|
22
|
+
render_method = (::Rails.env.development? || ::Rails.env.test?) ? :render! : :render
|
23
|
+
liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper }).html_safe
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Quintype
|
2
|
+
module Liquid
|
3
|
+
class Railtie < Rails::Engine
|
4
|
+
initializer "quintype-liquid.assets.precompile" do |app|
|
5
|
+
app.config.assets.paths << root.join("assets", "javascripts").to_s
|
6
|
+
app.config.assets.paths << root.join("vendor", "assets", "javascripts").to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
initializer "quintype-liquid.liquid-file-system" do |app|
|
10
|
+
template_path = ::Rails.root.join('app/views')
|
11
|
+
::Liquid::Template.file_system = Quintype::Liquid::FileSystem.new(template_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
initializer "quintype-liquid.fix-caching" do |app|
|
15
|
+
::Liquid::Rails::TemplateHandler.send(:prepend, Quintype::Liquid::LiquidTemplateCachingModule) if (::Rails.env.development? || ::Rails.env.test?)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Quintype
|
2
|
+
module Liquid
|
3
|
+
class Templates
|
4
|
+
attr_reader :paths
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@root = Rails.root.join("app", "views").to_s
|
8
|
+
@paths = Dir.glob("#{@root}/**/*.liquid").sort
|
9
|
+
end
|
10
|
+
|
11
|
+
def templates
|
12
|
+
paths.reduce({}) do |acc, path|
|
13
|
+
acc[clean_path(path)] = IO.read path
|
14
|
+
acc
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def clean_path(path)
|
21
|
+
path
|
22
|
+
.sub(@root, '')
|
23
|
+
.sub('.liquid', '')
|
24
|
+
.sub(/^\//, '')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'quintype/liquid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "quintype-liquid"
|
8
|
+
spec.version = Quintype::Liquid::VERSION
|
9
|
+
spec.authors = ["Tejas Dinkar"]
|
10
|
+
spec.email = ["tejas@gja.in"]
|
11
|
+
|
12
|
+
spec.summary = %q{Isomorphically Render Liquid in Rails and JS}
|
13
|
+
spec.homepage = "https://github.com/quintype/quintype-liquid"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "liquid-rails", "~> 0.1.3"
|
21
|
+
spec.add_dependency "concurrent-ruby", "~> 1.0.2"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|