js-text-rails 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b11dad2c06afa837ca4d482b5d77e3c6984785a8
4
+ data.tar.gz: ba00a457af07ca514ae2655d8c6bdb72d1bbeb7b
5
+ SHA512:
6
+ metadata.gz: 91265429b56ebd440e9ad1d4555d8678b4eb37a6e4a220d1cc0bc1a13011328ab7e4faf72be3dbc46639c88f21487b1a105ad4c793fcadb9387c0b9c579fb11f
7
+ data.tar.gz: 7c60e731f68d7b52c7c55f72ba0a26dbb1e413a4bfa6a7a0d95d35a9ce4884f1b661a37449ea68e387f1745334cb9dd12f459016b40546a95c7910c693eeeed8
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.1
5
+ - 2.3.3
6
+ - 2.4.1
7
+ cache: bundler
8
+ gemfile:
9
+ - sprockets3.gemfile
10
+ - sprockets4.gemfile
11
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ eval_gemfile "#{ File.dirname(__FILE__) }/sprockets4.gemfile"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Clover Sites
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # JsTextRails
2
+
3
+ Transform text files into strings on a global JavaScript object.
4
+
5
+ Why do this? Your Rails application might serve up a single-page web
6
+ application, which requires text assets (like SVGs). You need these assets to
7
+ render your application, and you don't want to fetch them via AJAX (to avoid
8
+ lag), nor embed them in the returned HTML (to keep the HTML response small).
9
+
10
+ Especially in the case of SVGs, their contents are needed so they can be
11
+ inserted into the document inline, so they can be styled with CSS.
12
+
13
+ Support is currently available for the following text file types:
14
+
15
+ - ".svg" files
16
+
17
+ If you'd like more, please submit a PR.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'js-text-rails'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install js-text-rails
34
+
35
+ ## Usage
36
+
37
+ ### Ruby on Rails
38
+
39
+ After adding the gem to your Gemfile, clear your cache:
40
+
41
+ ```sh
42
+ rake tmp:clear
43
+ ```
44
+
45
+ Normally, text file contents are stored in a global object named `TEXT` or `SVG`
46
+ (for ".svg" files). You may customize the namespace used by all text files by
47
+ setting `config.js_text_rails.namespace`. e.g., in `app/config/application.rb`:
48
+
49
+ ```ruby
50
+ module App
51
+ class Application < Rails::Application
52
+ config.js_text_rails.namespace = 'App.Text'
53
+ end
54
+ end
55
+ ```
56
+
57
+ Then all text file contents would instead be stored in the global object `App`
58
+ on a property called `Text`.
59
+
60
+ To disable the gem, just remove transformer:
61
+
62
+ ```ruby
63
+ JsTextRails.uninstall(Rails.application.assets)
64
+ ```
65
+
66
+ ### Sprockets
67
+
68
+ If you use Sinatra or other non-Rails frameworks with Sprockets,
69
+ just connect your Sprockets environment with it:
70
+
71
+ ```ruby
72
+ assets = Sprockets::Environment.new do |env|
73
+ # Your assets settings
74
+ end
75
+
76
+ require 'js-text-rails'
77
+ JsTextRails.install(assets, namespace: 'App.Text')
78
+ ```
79
+
80
+ ### In your application
81
+
82
+ `require` your files, e.g. in `app/assets/javascripts/application.js`:
83
+
84
+ ```js
85
+ //= require_tree ../images/icons
86
+ ```
87
+
88
+ Where `app/assets/images/icons` contains the file `envelope.svg`.
89
+
90
+ Anywhere in your application's JavaScript code, access the text file contents
91
+ via the global `SVG` variable:
92
+
93
+ ```js
94
+ var envelopeIcon = SVG['icons/envelope'];
95
+ // Do something with `envelopeIcon`...
96
+ ```
97
+
98
+ Here is a practical example of inserting an SVG inline:
99
+
100
+ ```js
101
+ document.querySelector('#email-icon').innerHTML = SVG['icons/envelope'];
102
+ ```
103
+
104
+ Or, in a JST template:
105
+
106
+ ```html
107
+ <span id="email-icon"><%- SVG['icons/envelope'] %></span> Email me at <%= @email %>!
108
+ ```
109
+
110
+ ## Development
111
+
112
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
113
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
114
+ prompt that will allow you to experiment.
115
+
116
+ To install this gem onto your local machine, run `bundle exec rake install`. To
117
+ release a new version, update the version number in `version.rb`, and then run
118
+ `bundle exec rake release`, which will create a git tag for the version, push
119
+ git commits and tags, and push the `.gem` file
120
+ to [rubygems.org](https://rubygems.org).
121
+
122
+ ## Contributing
123
+
124
+ Bug reports and pull requests are welcome on GitHub at
125
+ https://github.com/cloversites/js-text-rails.
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of
130
+ the [MIT License](http://opensource.org/licenses/MIT).
131
+
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 "js-text-rails"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js-text-rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "js-text-rails"
8
+ spec.version = JsTextRails::VERSION
9
+ spec.authors = ["Jackson Ray Hamilton"]
10
+ spec.email = ["jackson@jacksonrayhamilton.com"]
11
+
12
+ spec.summary = %q{Transform text files into strings on a global JavaScript object.}
13
+ spec.homepage = "https://github.com/cloversites/js-text-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "rspec-rails", "~> 3.5"
27
+ end
@@ -0,0 +1,24 @@
1
+ # Transform text files into strings on a global JavaScript object.
2
+ module JsTextRails
3
+ autoload :SvgSprockets, 'js-text-rails/svg_sprockets'
4
+
5
+ # Add JsText to Sprockets environment in `assets`.
6
+ def self.install(assets, params = {})
7
+ SvgSprockets.register_transformer(svg_transformer(params))
8
+ SvgSprockets.install(assets)
9
+ end
10
+
11
+ # Disable installed JsText.
12
+ def self.uninstall(assets)
13
+ SvgSprockets.uninstall(assets)
14
+ end
15
+
16
+ # Cache transformer instances.
17
+ def self.svg_transformer(params = {})
18
+ SvgTransformer.new(params)
19
+ end
20
+ end
21
+
22
+ require_relative 'js-text-rails/svg_transformer'
23
+
24
+ require_relative 'js-text-rails/railtie' if defined?(Rails)
@@ -0,0 +1,23 @@
1
+ module JsTextRails
2
+ class Railtie < ::Rails::Railtie
3
+ config.js_text_rails = ActiveSupport::OrderedOptions.new
4
+
5
+ if config.respond_to?(:assets) and not config.assets.nil?
6
+ config.assets.configure do |env|
7
+ JsTextRails.install(env, params)
8
+ end
9
+ else
10
+ initializer :setup_js_text, group: :all do |app|
11
+ if defined? app.assets and not app.assets.nil?
12
+ JsTextRails.install(app.assets, params)
13
+ end
14
+ end
15
+ end
16
+
17
+ def params
18
+ hash = {}
19
+ hash[:namespace] = config.js_text_rails.namespace if not config.js_text_rails.namespace.nil?
20
+ hash
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'text_sprockets'
2
+
3
+ module JsTextRails
4
+ class SvgSprockets < TextSprockets
5
+ @extension = 'svg'
6
+ @mime_type = 'image/svg+xml'
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'text_transformer'
2
+
3
+ module JsTextRails
4
+ class SvgTransformer < TextTransformer
5
+ def self.default_namespace
6
+ 'this.SVG'
7
+ end
8
+
9
+ def sanitize_js_string(text)
10
+ text = strip_xml_declaration(text)
11
+ text = escape_js_string(text)
12
+ text
13
+ end
14
+
15
+ # Strips <?xml ...?> declarations so that external SVG and XML documents can
16
+ # be added to a document without worry.
17
+ def strip_xml_declaration(text)
18
+ text.gsub(/^\s*<\?xml.*?\?>\s*/im, '')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,34 @@
1
+ module JsTextRails
2
+ # Register transformer in Sprockets.
3
+ class TextSprockets
4
+ def self.register_transformer(transformer)
5
+ @transformer = transformer
6
+ end
7
+
8
+ # Sprockets 3 and 4 API
9
+ def self.call(input)
10
+ name = input[:name]
11
+ data = input[:data]
12
+ run(name, data)
13
+ end
14
+
15
+ # Transform `data` into a JS object value at `key`.
16
+ def self.run(key, data)
17
+ @transformer.transform(key, data)
18
+ end
19
+
20
+ def self.install(env)
21
+ # TODO: Is the original extension for the mime type also needed as an
22
+ # extension? (Does the array override the old one, or get merged?)
23
+ env.register_mime_type(@mime_type, extensions: [".#{@extension}"])
24
+ env.register_transformer(@mime_type, 'application/javascript', self)
25
+ end
26
+
27
+ def self.uninstall(env)
28
+ # TODO: Is the original extension for the mime type also needed as an
29
+ # extension? (What would be the consequences of unregistering?)
30
+ env.unregister_mime_type(@mime_type, extensions: [".#{@extension}"])
31
+ env.unregister_transformer(@mime_type, 'application/javascript', self)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ module JsTextRails
2
+ class TextTransformer
3
+ def self.default_namespace
4
+ 'this.TEXT'
5
+ end
6
+
7
+ def initialize(options = {})
8
+ @namespace = options[:namespace] || self.class.default_namespace
9
+ end
10
+
11
+ # Take `data` and store it at a `key` in a JS namespace.
12
+ def transform(key, data)
13
+ key = sanitize_js_string(key)
14
+ data = sanitize_js_string(data)
15
+ <<-TEXT
16
+ (function() { #{@namespace} || (#{@namespace} = {}); #{@namespace}['#{key}'] = '#{data}';
17
+ }).call(this);
18
+ TEXT
19
+ end
20
+
21
+ # Can be overridden by subclasses.
22
+ def sanitize_js_string(text)
23
+ escape_js_string(text)
24
+ end
25
+
26
+ # Make text content safe and correct when evaluated in JS.
27
+ def escape_js_string(text)
28
+ text
29
+ .gsub(/(['\\])/, "\\\\\\1")
30
+ .gsub(/[\f]/, '\f')
31
+ .gsub(/[\b]/, '\b')
32
+ .gsub(/[\n]/, '\n')
33
+ .gsub(/[\t]/, '\t')
34
+ .gsub(/[\r]/, '\r')
35
+ .gsub(/[\u2028]/, '\u2028')
36
+ .gsub(/[\u2029]/, '\u2029')
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module JsTextRails
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '< 5'
6
+ gem 'sprockets', '>= 3.4.0', '< 4.0'
7
+ gem 'sprockets-rails'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '>= 5.0.0.beta2'
6
+ gem 'sprockets', '>= 4.0.0.beta1'
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-text-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jackson Ray Hamilton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-21 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ description:
70
+ email:
71
+ - jackson@jacksonrayhamilton.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - js-text-rails.gemspec
86
+ - lib/js-text-rails.rb
87
+ - lib/js-text-rails/railtie.rb
88
+ - lib/js-text-rails/svg_sprockets.rb
89
+ - lib/js-text-rails/svg_transformer.rb
90
+ - lib/js-text-rails/text_sprockets.rb
91
+ - lib/js-text-rails/text_transformer.rb
92
+ - lib/js-text-rails/version.rb
93
+ - sprockets3.gemfile
94
+ - sprockets4.gemfile
95
+ homepage: https://github.com/cloversites/js-text-rails
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.11
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Transform text files into strings on a global JavaScript object.
119
+ test_files: []