dashing_json 0.0.1

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: 42c149cc3c2aa76644d5e8cbf19d56c993792e8b
4
+ data.tar.gz: cec0c1a4b5702969e8764b31ffbf4b3b50283496
5
+ SHA512:
6
+ metadata.gz: 5c3c0cb881ccb794b3a9564da98fd0e4cd84baed744612687abe5a15bdef1923bf908038cc27e04a1beed86d12f04944de306d09697f1ada0cf7de5fbfc11a9f
7
+ data.tar.gz: c4908470aa1a027252b86d8a5c6390a7472a76dc920c683f692fa5a27edc8c7950b1f413bccc2233fb9447c4a7ae1db430a636b5531a6da9f7944ae3fdda8d5c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dashing_json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 John Otander
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Dashing JSON
2
+
3
+ Make your JSON absolutely dashing in your rails HTML views since, sometimes, you need to display a raw blob.
4
+
5
+ Dashing JSON uses a `dashing-json` data element to store the JSON string, which is then parsed into a `ul` with specific `li` elements and corresponding classes: key, string, boolean, null, and number.
6
+
7
+ It comes with default styling so your JSON blob is sure to look dashing right out of the box.
8
+
9
+ ### Before
10
+
11
+ ```
12
+ {"data": { "key": null, "can_read": false } }
13
+ ```
14
+
15
+ ### After
16
+
17
+ ```json
18
+ {
19
+ "data": {
20
+ "key": null,
21
+ "can_read": false
22
+ }
23
+ }
24
+ ```
25
+
26
+ Yeah, that's right. Egyptian curly braces (you can change that if you want).
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'dashing_json'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ $ bundle
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install dashing_json
43
+
44
+ #### Next, you need to require the `.js.coffee` and `.css.scss` assets:
45
+
46
+ If you're using sprockets, add the following to your `application.css`:
47
+
48
+ *= require dashing_json
49
+
50
+ If you're using an `application.css.scss` file:
51
+
52
+ ```scss
53
+ @import "dashing_json";
54
+ ```
55
+
56
+ And, include the scripts with the following in your `application.js`
57
+
58
+ *= require dashing_json
59
+
60
+ ## Usage
61
+
62
+ Now, you simply need to instantiate the JSON blob in you controller:
63
+
64
+ ```ruby
65
+ class ModelController < ApplicationController
66
+
67
+ # ...
68
+
69
+ def show
70
+ @model = Model.find(params[:id])
71
+ @json_blob = @model.to_json
72
+ end
73
+
74
+ # ...
75
+ ```
76
+
77
+ And, then call the `dashing_json` method in your view:
78
+
79
+ ```erb
80
+ <%= # ... %>
81
+
82
+ <%= dashing_json(@json_blob) %>
83
+
84
+ <%= # ... %>
85
+ ```
86
+
87
+ That's it, your JSON is now dashing.
88
+
89
+ ## Customization
90
+
91
+ Overriding the default colors of the syntax highlighting is simple since they're just variables. If you're using scss, and would like to customize the styles, you can add the following before you import the stylesheet in `plugins/_dashing_json.scss`:
92
+
93
+ ```scss
94
+ $dashing-json-key-color: $your-color;
95
+ $dashing-json-string-color: $your-color;
96
+ $dashing-json-number-color: $your-color;
97
+ $dashing-json-boolean-color: $your-color;
98
+
99
+ @import "dashing_json";
100
+ ```
101
+
102
+ Be sure to import this file in your `application.css.scss`.
103
+
104
+ If you aren't using sass, the selectors would be something like:
105
+
106
+ ```css
107
+ .dashing-json .key,
108
+ .dashing-json .null,
109
+ .dashing-json .string,
110
+ .dashing-json .boolean,
111
+ .dashing-json .number {
112
+ /* custom styling here */
113
+ }
114
+ ```
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ # Let's make the json extra pretty.
2
+ #
3
+ # Adapted from Pumbaa80's answer on:
4
+ # http://stackoverflow.com/questions/4810841/json-pretty-print-using-javascript
5
+
6
+ $ ->
7
+ json.syntaxHighlight()
8
+
9
+
10
+ json =
11
+ syntaxHighlight: ->
12
+ for jsonElem in $('.dashing-json-raw')
13
+ do (jsonElem) ->
14
+ jsonData = $(jsonElem).attr('data-dashing-json')
15
+ if(typeof jsonData != 'string')
16
+ jsonData = JSON.stringify(jsonData, undefined, 2)
17
+ jsonData = jsonData.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
18
+ $('.dashing-json-parsed').append(json.doSyntaxHighlighting(jsonData))
19
+
20
+ doSyntaxHighlighting: (jsonData) ->
21
+ jsonData.replace /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, (match) ->
22
+ if(/^"/.test(match))
23
+ cls = if /:$/.test(match) then 'key' else 'string'
24
+ else if(/true|false/.test(match))
25
+ cls = 'boolean'
26
+ else if (/null/.test(match))
27
+ cls = 'null'
28
+ else if (/[0-9]+/.test(match))
29
+ cls = 'number'
30
+
31
+ return '<li class="' + cls + '">' + match + '</li>'
@@ -0,0 +1,15 @@
1
+ $dashing-json-key-color: #999;
2
+ $dashing-json-string-color: #666;
3
+ $dashing-json-number-color: #222;
4
+ $dashing-json-boolean-color: #444;
5
+
6
+ .dashing-json .key,
7
+ .dashing-json .null,
8
+ .dashing-json .string,
9
+ .dashing-json .boolean,
10
+ .dashing-json .number { display: inline; }
11
+
12
+ .dashing-json .key { color: $dashing-json-key-color; }
13
+ .dashing-json .string { color: $dashing-json-string-color; }
14
+ .dashing-json .number { color: $dashing-json-number-color; }
15
+ .dashing-json .boolean { color: $dashing-json-boolean-color; }
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dashing_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dashing_json"
8
+ spec.version = DashingJson::VERSION
9
+ spec.authors = ["John Otander"]
10
+ spec.email = ["johnotander@gmail.com"]
11
+ spec.description = %q{Make your JSON absolutely dashing in your rails HTML views.}
12
+ spec.summary = %q{Make your JSON absolutely dashing in your rails HTML views.}
13
+ spec.homepage = "https://github.com/johnotander/dashing_json"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'dashing_json/version'
2
+ require 'dashing_json/engine'
3
+ require 'dashing_json/view_helpers'
4
+
5
+ module DashingJson
6
+ include ViewHelpers
7
+ end
8
+
9
+ defined?(Rails::Railtie) ?
10
+ require('dashing_json/railtie') :
11
+ raise('dashing_json requires Rails::Railtie')
@@ -0,0 +1,6 @@
1
+ module DashingJson
2
+ if defined?(::Rails::Engine)
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ require 'dashing_json'
2
+
3
+ module DashingJson
4
+ class Railtie < Rails::Railtie
5
+ initializer 'dashing_json' do |app|
6
+ ActionView::Base.send :include, DashingJson
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DashingJson
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ module DashingJson
2
+ module ViewHelpers
3
+
4
+ def dashing_json(data)
5
+ generate_html(data).html_safe
6
+ end
7
+
8
+ def generate_html(data)
9
+ data = data.to_json unless data and data.is_a?(String)
10
+
11
+ <<-HTML
12
+ <div class='dashing-json'>
13
+ <pre>
14
+ <ul class='dashing-json-parsed'></ul>
15
+ <div class='dashing-json-raw' data-dashing-json='#{ JSON.pretty_generate(JSON.parse(data)) }'></div>
16
+ </pre>
17
+ </div>
18
+ HTML
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dashing_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Otander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-07 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Make your JSON absolutely dashing in your rails HTML views.
42
+ email:
43
+ - johnotander@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - app/assets/javascripts/dashing_json.js.coffee
54
+ - app/assets/stylesheets/dashing_json.css.scss
55
+ - dashing_json.gemspec
56
+ - lib/dashing_json.rb
57
+ - lib/dashing_json/engine.rb
58
+ - lib/dashing_json/railtie.rb
59
+ - lib/dashing_json/version.rb
60
+ - lib/dashing_json/view_helpers.rb
61
+ homepage: https://github.com/johnotander/dashing_json
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.1.11
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Make your JSON absolutely dashing in your rails HTML views.
85
+ test_files: []