pry-highlight 0.0.1

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.
Files changed (4) hide show
  1. data/LICENSE.MIT +19 -0
  2. data/README.markdown +81 -0
  3. data/lib/pry-highlight.rb +66 -0
  4. metadata +124 -0
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Conrad Irwin <conrad.irwin@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ The Pry-higlight gem (`gem install pry-highlight`) adds a `higlight` command (aka. `>>`) to Pry.
2
+
3
+ This intelligently formats and colourizes the result, so you don't have to.
4
+
5
+ Features
6
+ ========
7
+
8
+ By default this pretty-prints the previous output value:
9
+
10
+
11
+ [1] pry(main)> '{"hi":"world"}'
12
+ => "{\"hi\":\"world\"}"
13
+ [2] pry(main)> >>
14
+ ```javascript
15
+ {
16
+ "hi": "world"
17
+ }
18
+ ```
19
+
20
+ It can auto-detect JSON, XML and Ruby code by default:
21
+
22
+
23
+ [3] pry(main)> JSON.method(:pretty_print).source
24
+ => " def pretty_print(q)\n q.text inspect\n end\n"
25
+ [4] pry(main)> >>
26
+ ```ruby
27
+ def pretty_print(q)
28
+ q.text inspect
29
+ end
30
+ ```
31
+
32
+ You can also tell it which highlighter to use, see [CodeRay's homepage](http://coderay.rubychan.de/) for the full list.
33
+
34
+ [5] pry(main)> "function () {\n return 'meh';\n}"
35
+ => "function () {\n return 'meh';\n}"
36
+ [6] pry(main)> >> -t javascript
37
+ ```javascript
38
+ function () {
39
+ return 'meh';
40
+ }
41
+ ```
42
+
43
+ You can give it the value to higlight explicitly:
44
+
45
+ [7] pry(main)> >> "<meta><foo/></meta>"
46
+ ```xml
47
+ <?xml version="1.0"?>
48
+ <meta>
49
+ <foo/>
50
+ </meta>
51
+ ```
52
+
53
+ In fact, any combination of ruby and a -t flag works :)
54
+
55
+ [8] pry(main)> >> -t html File.read("/var/www/foo.html")
56
+ ```html
57
+ <html xmlns="http://www.w3.org/1999/xhtml">
58
+ <body>
59
+ <h1>It works!</h1>
60
+ </body>
61
+ </html>
62
+ ```
63
+
64
+ Even if you try to `>>` a String that isn't understood, you'll still get nice results because newlines will be interpreted correctly:
65
+
66
+ [9] pry(main)> "1 2 3\n4 5 6\n7 8 9\n"
67
+ => "1 2 3\n4 5 6\n7 8 9\n"
68
+ [10] pry(main)> >>
69
+ 1 2 3
70
+ 4 5 6
71
+ 7 8 9
72
+
73
+ Issues
74
+ =====
75
+
76
+ The guessing of type based on the string could do with improvement.
77
+
78
+ Meta-foo
79
+ ========
80
+
81
+ Licensed under the MIT license, contributions and bug-reports welcome :).
@@ -0,0 +1,66 @@
1
+
2
+ Pry::Commands.command ">>", "highlight string output", :shellwords => false do |*args|
3
+ require 'json'
4
+ require 'nokogiri'
5
+
6
+ opts = Slop.parse!(args) do |opt|
7
+ opt.banner unindent <<-BANNER
8
+ Usage: >> [ -t <type>] <optional ruby code>
9
+ BANNER
10
+ opt.on :h, :help do
11
+ puts opt.help
12
+ end
13
+ opt.on :t, :type, true
14
+ end
15
+ next if opts[:h]
16
+
17
+ value = target.eval args.join(" ").sub(/\A *\z/, '_')
18
+ type = opts[:t] ? opts[:t].to_sym : nil
19
+
20
+
21
+ begin
22
+ value = value.to_str
23
+ rescue NoMethodError
24
+ Pry.config.print.call(output, value) unless value.respond_to?(:to_str)
25
+ next
26
+ end
27
+
28
+
29
+ if type == :json || !type && value.start_with?("{") || value.start_with?("[")
30
+ begin
31
+ value = JSON.pretty_generate(JSON.parse(value))
32
+ type = :json
33
+ rescue Pry::RescuableException => e
34
+ output.puts "#{e}"
35
+ end
36
+ elsif type == :html || !type && value.start_with?("<html")
37
+ begin
38
+ value = Nokogiri::HTML(value).to_xhtml.lines.drop(1).join
39
+ type = :html
40
+ rescue Pry::RescuableException => e
41
+ output.puts "#{e}"
42
+ end
43
+ elsif type == :xml || !type && value.start_with?("<")
44
+ begin
45
+ value = Nokogiri::XML(value).to_xml
46
+ type = :xml
47
+ rescue Pry::RescuableException => e
48
+ output.puts "#{e}"
49
+ end
50
+ elsif !type
51
+ if _pry_.respond_to?(:valid_expression?) && _pry_.valid_expression?(value) ||
52
+ _pry_.respond_to?(:complete_expression?) && (begin _pry_.complete_expression?(value); true; rescue SyntaxError; false; end)
53
+ type = :ruby
54
+ end
55
+ end
56
+
57
+ if type
58
+ output.puts CodeRay.scan(value, type).term
59
+ elsif String === value
60
+ output.puts value
61
+ else
62
+ Pry::Config.print.call(output, value)
63
+ end
64
+ end
65
+
66
+ Pry::Commands.alias_command "highlight", ">>"
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-highlight
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Conrad Irwin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-15 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: pry
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: nokogiri
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: coderay
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: Uses JSON, Nokogiri and CodeRay to highlight and prettify JSON, XML, HTML, Ruby and anything else CodeRay supports!
78
+ email: conrad.irwin@gmail.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - lib/pry-highlight.rb
87
+ - README.markdown
88
+ - LICENSE.MIT
89
+ has_rdoc: true
90
+ homepage: http://github.com/ConradIrwin/pry-highlight
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.6.2
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Adds a >> command to pretty-print Strings
123
+ test_files: []
124
+