volt-slim 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf88cbd7970c3732775c195dd9e15aeb5022af63
4
+ data.tar.gz: ce61a39add0d98c29afa1131bd63f8297345fb99
5
+ SHA512:
6
+ metadata.gz: 82116cf80be147080680f3215100a638530c142e771f7167e2c8bfe5ee94115b1e7401963cb4d36906bd5d1416138458454d62f583afeec6d624bf3c09d9e469
7
+ data.tar.gz: 7a17e90394e49738fb9b0367f4ef1d7830cc288199d00d9c2c650d8a4a32c405832c16773b9e8d269ab84077179ea4bda4526f584113e21a889af36e2827887b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in volt-slim.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Большов Андрей
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # volt-slim
2
+ Slim for Volt framework
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "volt/slim"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ module Volt
2
+ module Slim
3
+ class Compiler
4
+ def self.build template
5
+ SandlebarsConverter.new({pretty: true, use_html_safe: false, disable_escape: true}).call(template)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,82 @@
1
+ require 'ripper'
2
+ require 'sorcerer'
3
+
4
+ module Volt
5
+ module Slim
6
+ module Filters
7
+ # @api private
8
+ class AttrValueConverter < ::Slim::Filter
9
+ ANY_RE = /\b(if|unless|else|elsif|when|rescue|ensure)\b|\b?\b|\bdo\s*(\|[^\|]*\|)?\s*|$\b(else|elsif|when|rescue|ensure)\b|\bend\b/
10
+
11
+ def on_slim_attrvalue(escape, code)
12
+ if code =~ ANY_RE
13
+ a = convert code
14
+ a
15
+ else
16
+ [:dynamic, code]
17
+ end
18
+ end
19
+
20
+ TMP_SYMBOL_RE = /(?=:__volt_slim\d+)/
21
+ TMP2_SYMBOL_RE = /(:__volt_slim(\d+))?(.+)/
22
+ def convert code
23
+ builder = CustomSexp.new(code)
24
+ string = Sorcerer.source builder.parse
25
+ code = string.split(TMP_SYMBOL_RE).each_with_object([]) do |chunk, obj|
26
+ chunk =~ TMP2_SYMBOL_RE
27
+ obj << [:dynamic, Sorcerer.source(builder.storage[$2.to_i])]if $2
28
+ obj << [:code, $3]
29
+ end
30
+ code.unshift :multi
31
+ code
32
+ end
33
+ class CustomSexp < ::Ripper::SexpBuilder
34
+ attr_reader :storage
35
+ def on_if(*args)
36
+ args[1] = new_symbol args[1]
37
+ super
38
+ end
39
+
40
+ def on_ifop(*args)
41
+ args[1] = [:stmts_add, [:stmts_new], new_symbol(args[1])]
42
+ args[2] = [:else, [:stmts_add, [:stmts_new], new_symbol(args[2])]]
43
+ args.unshift :if
44
+ args
45
+ end
46
+
47
+ def on_else(*args)
48
+ args[0] = new_symbol args[0]
49
+ super
50
+ end
51
+ def on_elsif(*args)
52
+ args[1] = new_symbol args[1]
53
+ super
54
+ end
55
+
56
+ def initialize(*args)
57
+ @storage = []
58
+ super
59
+ end
60
+
61
+ def new_symbol store
62
+ index = @storage.size
63
+ position = find_position store
64
+ @storage.push store
65
+
66
+ [:symbol_literal, [:symbol, [:@ident, "__volt_slim#{index}", position]]]
67
+ end
68
+
69
+ def find_position arr
70
+ return nil unless arr.is_a? Array
71
+ return arr.last if SCANNER_EVENTS.include?(arr.first)
72
+ arr[1..-1].each do |item|
73
+ result = find_position item
74
+ return result if result
75
+ end
76
+ nil
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,48 @@
1
+ module Volt
2
+ module Slim
3
+ module Filters
4
+ # @api private
5
+ class AttributeMerger < ::Temple::HTML::AttributeMerger
6
+ def on_html_attrs(*attrs)
7
+ names = []
8
+ values = {}
9
+
10
+ attrs.each do |attr|
11
+ name, value = attr[2].to_s, attr[3]
12
+ if values[name]
13
+ raise(FilterError, "Multiple #{name} attributes specified") unless options[:merge_attrs][name]
14
+ values[name] << value
15
+ else
16
+ values[name] = [value]
17
+ names << name
18
+ end
19
+ end
20
+
21
+ attrs = names.map do |name|
22
+ value = values[name]
23
+ if (delimiter = options[:merge_attrs][name]) && value.size > 1
24
+ exp = [:multi]
25
+ if value.all? {|v| contains_nonempty_static?(v) }
26
+ exp << value.first
27
+ value[1..-1].each {|v| exp << [:static, delimiter] << v }
28
+ [:html, :attr, name, exp]
29
+ else
30
+ captures = unique_name
31
+ statica = value.select {|v| contains_nonempty_static?(v) }
32
+ exp << statica.first
33
+ statica[1..-1].each {|v| exp << [:static, delimiter] << v }
34
+ dynamics = value.reject {|v| contains_nonempty_static?(v) }
35
+ dynamics.each {|v| exp << [:static, delimiter] << v }
36
+ end
37
+ [:html, :attr, name, exp]
38
+ else
39
+ [:html, :attr, name, value.first]
40
+ end
41
+ end
42
+
43
+ [:html, :attrs, *attrs]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,66 @@
1
+ module Volt
2
+ module Slim
3
+
4
+ module Filters
5
+ # @api private
6
+ class CodeAttributes < ::Slim::Filter
7
+ define_options :merge_attrs
8
+
9
+ # Handle attributes expression `[:html, :attrs, *attrs]`
10
+ #
11
+ # @param [Array] attrs Array of temple expressions
12
+ # @return [Array] Compiled temple expression
13
+ def on_html_attrs(*attrs)
14
+ [:multi, *attrs.map {|a| compile(a) }]
15
+ end
16
+
17
+ # Handle attribute expression `[:html, :attr, name, value]`
18
+ #
19
+ # @param [String] name Attribute name
20
+ # @param [Array] value Value expression
21
+ # @return [Array] Compiled temple expression
22
+ def on_html_attr(name, value)
23
+ if value[0] == :slim && value[1] == :attrvalue # && !options[:merge_attrs][name]
24
+ # We handle the attribute as a boolean attribute
25
+ escape, code = value[2], value[3]
26
+ case code
27
+ when 'true'
28
+ [:html, :attr, name, [:multi]]
29
+ when 'false', 'nil'
30
+ [:multi]
31
+ else
32
+ [:html, :attr, name, [:dynamic, code]]
33
+ end
34
+ else
35
+ # Attribute with merging
36
+ @attr = name
37
+ super
38
+ end
39
+ end
40
+
41
+ # Handle attribute expression `[:slim, :attrvalue, escape, code]`
42
+ #
43
+ # @param [Boolean] escape Escape html
44
+ # @param [String] code Ruby code
45
+ # @return [Array] Compiled temple expression
46
+ def on_slim_attrvalue(escape, code)
47
+ # We perform attribute merging on Array values
48
+ # if delimiter = options[:merge_attrs][@attr]
49
+ # tmp = unique_name
50
+ # [:multi,
51
+ # [:code, "#{tmp} = #{code}"],
52
+ # [:if, "Array === #{tmp}",
53
+ # [:multi,
54
+ # [:code, "#{tmp} = #{tmp}.flatten"],
55
+ # [:code, "#{tmp}.map!(&:to_s)"],
56
+ # [:code, "#{tmp}.reject!(&:empty?)"],
57
+ # [:escape, escape, [:dynamic, "#{tmp}.join(#{delimiter.inspect})"]]],
58
+ # [:escape, escape, [:dynamic, tmp]]]]
59
+ # else
60
+ [:dynamic, code]
61
+ # end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,15 @@
1
+ module Volt
2
+ module Slim
3
+ module Filters
4
+ # @api private
5
+ class Controls < ::Slim::Controls
6
+ def on_slim_control(code, content)
7
+ [:multi,
8
+ [:code, :indent],
9
+ [:code, code],
10
+ compile(content)]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ module Volt
2
+ module Slim
3
+ module Filters
4
+ # @api private
5
+ class Pretty < ::Temple::HTML::Pretty
6
+
7
+ def on_dynamic(code)
8
+ return [:dynamic, code] unless @pretty
9
+ indent_next, @indent_next = @indent_next, false
10
+ [:multi, [:static, ((options[:indent] || '') * @indent)], [:dynamic, code]]
11
+ end
12
+
13
+ def on_code(code)
14
+ if code =~ ::Slim::EndInserter::END_RE
15
+ [:multi, [:static, ("\n"+(options[:indent] || '') * @indent)], [:code, code]]
16
+ elsif code == :indent
17
+ [:static, ("\n"+(options[:indent] || '') * @indent)]
18
+ else
19
+ [:code, code]
20
+ end
21
+ end
22
+
23
+ def preamble
24
+ [:multi]
25
+ end
26
+
27
+ SANDLEBARS_PREFIXES_RE = /\A(tpl|use)-/
28
+ def on_html_tag(name, attrs, content = nil)
29
+ if name =~ SANDLEBARS_PREFIXES_RE
30
+ closed = empty_exp?(content)
31
+ is_template, sb_name = sendlebars name
32
+ indent = tag_indent(name)
33
+ result = [:multi, [:static, "#{indent}<:#{sb_name}"], compile(attrs)]
34
+ result << [:static, (closed ? ' /' : '') + '>']
35
+ unless closed
36
+ @indent += 1
37
+ result << compile(content)
38
+ @indent -= 1
39
+ result << [:static, "#{indent}</:#{sb_name}>"] unless is_template
40
+ end
41
+ result
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ TEMPLATE_TAG = %r{tpl-(.+)}i
48
+ PARTIAL_TAG = %r{use-(.+)}i
49
+ PARTIAL_TAG_REPLACE = '<:\1\2>'
50
+ def sendlebars name
51
+ case name
52
+ when TEMPLATE_TAG then [true, $1.capitalize]
53
+ when PARTIAL_TAG then [false, $1]
54
+ end
55
+ end
56
+ # def tag_indent(name)
57
+ # if name =~ /\A(tpl|use)-/
58
+ # indent
59
+ # else
60
+ # super
61
+ # end
62
+ # end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,16 @@
1
+ module Volt
2
+ module Slim
3
+ class SandlebarsConverter < ::Slim::ERBConverter
4
+ replace :'Slim::CodeAttributes', Volt::Slim::Filters::CodeAttributes
5
+ # before :'Volt::Slim::Filters::CodeAttributes', Volt::Slim::Filters::AttrValueConverter
6
+ replace :AttributeMerger, Volt::Slim::Filters::AttributeMerger
7
+ replace :Pretty, Volt::Slim::Filters::Pretty
8
+ replace :'Slim::Controls', Volt::Slim::Filters::Controls
9
+ replace :'Temple::Generators::ERB', Volt::Slim::SendlebarsGenerator
10
+
11
+ # remove :AttributeMerger
12
+ remove :AttributeRemover
13
+ remove :'Temple::Filters::CodeMerger'
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module Volt
2
+ module Slim
3
+ class SendlebarsGenerator < ::Temple::Generators::ERB
4
+ def on_dynamic(code)
5
+ "{{ #{code} }}"
6
+ end
7
+
8
+ def on_code(code)
9
+ "{{ #{code} }}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Volt
2
+ module Slim
3
+ module Sprockets
4
+ class SlimBuilder
5
+ def call(html)
6
+ result = Volt::Slim::Compiler.new(template: data).build
7
+ result
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ if defined?(Volt::ComponentTemplates)
15
+ Volt::ComponentTemplates.register_template_handler(:slim, Volt::Slim::Sprockets::SlimBuilder.new)
16
+ end
@@ -0,0 +1,5 @@
1
+ module Volt
2
+ module Slim
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/volt/slim.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "volt/slim/version"
2
+ require 'slim/erb_converter'
3
+
4
+ module Volt
5
+ module Slim
6
+ require "volt/slim/filters/attr_value_converter"
7
+ require "volt/slim/filters/attribute_merger"
8
+ require "volt/slim/filters/code_attributes"
9
+ require "volt/slim/filters/pretty"
10
+ require "volt/slim/filters/controls"
11
+ require "volt/slim/sandlebars_generator"
12
+ require "volt/slim/sandlebars_converter"
13
+ require "volt/slim/compiler"
14
+ require "volt/slim/sprockets/slim_builder"
15
+ end
16
+ end
data/volt-slim.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'volt/slim/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "volt-slim"
8
+ spec.version = Volt::Slim::VERSION
9
+ spec.authors = ["Андрей Большов"]
10
+ spec.email = ["asnow.dev@gmail.ru"]
11
+
12
+ spec.summary = %q{Allow you use slim templates in volt framework.}
13
+ spec.description = %q{This gem translate slim to volt template.}
14
+ spec.homepage = "http://github.com/asnow/volt-slim"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "slim"
25
+ spec.add_dependency 'sorcerer'
26
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: volt-slim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Андрей Большов"
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: slim
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sorcerer
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This gem translate slim to volt template.
84
+ email:
85
+ - asnow.dev@gmail.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/volt/slim.rb
100
+ - lib/volt/slim/compiler.rb
101
+ - lib/volt/slim/filters/attr_value_converter.rb
102
+ - lib/volt/slim/filters/attribute_merger.rb
103
+ - lib/volt/slim/filters/code_attributes.rb
104
+ - lib/volt/slim/filters/controls.rb
105
+ - lib/volt/slim/filters/pretty.rb
106
+ - lib/volt/slim/sandlebars_converter.rb
107
+ - lib/volt/slim/sandlebars_generator.rb
108
+ - lib/volt/slim/sprockets/slim_builder.rb
109
+ - lib/volt/slim/version.rb
110
+ - volt-slim.gemspec
111
+ homepage: http://github.com/asnow/volt-slim
112
+ licenses: []
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.4.5
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Allow you use slim templates in volt framework.
134
+ test_files: []