pizzazz 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/Rakefile +9 -0
- data/Readme.markdown +79 -0
- data/lib/pizzazz.rb +11 -0
- data/lib/pizzazz/colorer.rb +71 -0
- data/lib/pizzazz/version.rb +3 -0
- data/pizzazz.gemspec +17 -0
- data/test/test_helper.rb +6 -0
- data/test/units/colorer_test.rb +9 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Sam Soffes
|
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/Rakefile
ADDED
data/Readme.markdown
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Pizzazz
|
2
|
+
|
3
|
+
Pizzazz is a simple pure Ruby implementation of code coloring, but just for JSON. Basically, if you have a ruby object and want to show it converted to JSON and add HTML around it so you can color it.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Pizzazzifing an object is simple:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
object = { name: 'Sam Soffes', website: 'http://samsoff.es' }
|
11
|
+
Pizzazz.ify(object)
|
12
|
+
#=> "{\n <span class=\"string\">\"name\"</span>: <span class=\"string\">\"Sam Soffes\"</span>,\n <span class=\"string\">\"website\"</span>: <span class=\"string\">\"http://samsoff.es\"</span>\n}"
|
13
|
+
```
|
14
|
+
|
15
|
+
You can optionally limit arrays as well:
|
16
|
+
|
17
|
+
``` ruby
|
18
|
+
Pizzazz.ify(all_of_the_things, limit: 1)
|
19
|
+
```
|
20
|
+
|
21
|
+
This will add an ellipses after the first element.
|
22
|
+
|
23
|
+
### HTML
|
24
|
+
|
25
|
+
Spans are added around various elements. Here's the classes:
|
26
|
+
|
27
|
+
* `string`
|
28
|
+
* `constant` (for `true` or `false`
|
29
|
+
* `null`
|
30
|
+
* `number`
|
31
|
+
|
32
|
+
Everything else is left alone. It is recommended to wrap the output like this:
|
33
|
+
|
34
|
+
``` html
|
35
|
+
<pre><%= Pizzazz.ify(object).html_safe %></pre>
|
36
|
+
```
|
37
|
+
|
38
|
+
Here's my stylesheet if you're interested:
|
39
|
+
|
40
|
+
``` css
|
41
|
+
pre {
|
42
|
+
border-radius: 5px;
|
43
|
+
background: #f7f7f7;
|
44
|
+
padding: 0.5em;
|
45
|
+
margin-bottom: 2em;
|
46
|
+
border: 1px solid #ddd;
|
47
|
+
}
|
48
|
+
|
49
|
+
.string {
|
50
|
+
color: #ee6132;
|
51
|
+
}
|
52
|
+
|
53
|
+
.null, .number, .constant {
|
54
|
+
color: #8e75c3;
|
55
|
+
}
|
56
|
+
|
57
|
+
.comment {
|
58
|
+
color: #999;
|
59
|
+
font-style: italic;
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
## Installation
|
64
|
+
|
65
|
+
Add this line to your application's Gemfile:
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
gem 'pizzazz'
|
69
|
+
```
|
70
|
+
|
71
|
+
And then execute:
|
72
|
+
|
73
|
+
$ bundle
|
74
|
+
|
75
|
+
Or install it yourself as:
|
76
|
+
|
77
|
+
$ gem install pizzazz
|
78
|
+
|
79
|
+
Simple as that. Enjoy.
|
data/lib/pizzazz.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
class Pizzazz::Colorer
|
5
|
+
def initialize(object, options = nil)
|
6
|
+
options ||= {}
|
7
|
+
@object = object
|
8
|
+
@indent = 0
|
9
|
+
@limit = (options[:limit] or 0)
|
10
|
+
end
|
11
|
+
|
12
|
+
def ify
|
13
|
+
node(@object, @limit)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def tab
|
19
|
+
" " * @indent * Pizzazz::TAB_SIZE
|
20
|
+
end
|
21
|
+
|
22
|
+
def node(object, limit = 0)
|
23
|
+
case object
|
24
|
+
when String
|
25
|
+
%Q{<span class="string">"#{::ERB::Util.h(object)}"</span>}
|
26
|
+
when Time
|
27
|
+
%Q{<span class="string">#{object.to_json}</span>}
|
28
|
+
when TrueClass
|
29
|
+
%Q{<span class="constant">true</span>}
|
30
|
+
when FalseClass
|
31
|
+
%Q{<span class="constant">false</span>}
|
32
|
+
when NilClass
|
33
|
+
%Q{<span class="null">null</span>}
|
34
|
+
when Numeric
|
35
|
+
%Q{<span class="number">#{object}</span>}
|
36
|
+
when Hash
|
37
|
+
s = "{\n"
|
38
|
+
@indent += 1
|
39
|
+
rows = []
|
40
|
+
object.keys.collect(&:to_s).sort.each do |key|
|
41
|
+
value = (object[key] != nil ? object[key] : object[key.to_sym])
|
42
|
+
rows << %Q{#{tab}<span class="string">"#{key}"</span>: #{node(value)}}
|
43
|
+
end
|
44
|
+
s << rows.join(",\n") + "\n"
|
45
|
+
@indent -= 1
|
46
|
+
s << "#{tab}}"
|
47
|
+
s
|
48
|
+
when Array
|
49
|
+
if object.length == 0
|
50
|
+
"[]"
|
51
|
+
else
|
52
|
+
s = "[\n"
|
53
|
+
@indent += 1
|
54
|
+
rows = []
|
55
|
+
array = @limit > 0 ? object[0...limit] : object
|
56
|
+
array.each do |value|
|
57
|
+
rows << tab + node(value)
|
58
|
+
end
|
59
|
+
|
60
|
+
if limit > 0 and object.length > limit
|
61
|
+
rows << tab + (object[0].is_a?(Hash) ? '{ … }' : '…')
|
62
|
+
end
|
63
|
+
|
64
|
+
s << rows.join(",\n") + "\n"
|
65
|
+
@indent -= 1
|
66
|
+
s << "#{tab}]"
|
67
|
+
s
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/pizzazz.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path('../lib/pizzazz/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Sam Soffes']
|
6
|
+
gem.email = ['sam@samsoff.es']
|
7
|
+
gem.description = %q{Add some pizzazz to your documentation}
|
8
|
+
gem.summary = %q{A simple gem to code color documentation}
|
9
|
+
gem.homepage = 'http://github.com/samsoffes/pizzazz'
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = 'pizzazz'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = Pizzazz::VERSION
|
17
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestColorer < MiniTest::Unit::TestCase
|
4
|
+
def test_that_it_colors_hashes
|
5
|
+
assert_equal Pizzazz.ify({foo: 'bar'}), %Q{{\n <span class="string">"foo"</span>: <span class="string">"bar"</span>\n}}
|
6
|
+
end
|
7
|
+
|
8
|
+
# These need to be expanded
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pizzazz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sam Soffes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Add some pizzazz to your documentation
|
15
|
+
email:
|
16
|
+
- sam@samsoff.es
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- Rakefile
|
25
|
+
- Readme.markdown
|
26
|
+
- lib/pizzazz.rb
|
27
|
+
- lib/pizzazz/colorer.rb
|
28
|
+
- lib/pizzazz/version.rb
|
29
|
+
- pizzazz.gemspec
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/units/colorer_test.rb
|
32
|
+
homepage: http://github.com/samsoffes/pizzazz
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
hash: -3216687736282638724
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: -3216687736282638724
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.23
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A simple gem to code color documentation
|
62
|
+
test_files:
|
63
|
+
- test/test_helper.rb
|
64
|
+
- test/units/colorer_test.rb
|