sbfaulkner-pygmalion 0.9.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 unwwwired.net
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ Gem::manage_gems
3
+ require 'rake/gempackagetask'
4
+
5
+ load File.join(File.dirname(__FILE__),'pygmalion.gemspec')
6
+
7
+ Rake::GemPackageTask.new(SPEC) do |pkg|
8
+ end
9
+
10
+ task :default => :package
data/bin/pygmalion ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'getoptlong'
5
+
6
+ opts = GetoptLong.new(
7
+ [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
8
+ [ "--language", "-l", GetoptLong::REQUIRED_ARGUMENT ],
9
+ [ "--line-numbers", "-n", GetoptLong::NO_ARGUMENT ]
10
+ )
11
+
12
+ OPTIONS = {
13
+ :code => nil,
14
+ :type => nil,
15
+ :css => 'n',
16
+ :linenos => 'off'
17
+ }
18
+
19
+ opts.each do |opt, arg|
20
+ case opt
21
+ when '--help'
22
+ puts "Usage: #{File.basename(ARGV[0])} [options] [file ...]"
23
+ puts "\nOptions:"
24
+ puts " -l, --language=name The language for the code to be highlighted."
25
+ puts " -n, --line-numbers Include line numbers."
26
+ exit 1
27
+ when '--language'
28
+ OPTIONS[:type] = arg
29
+ when '--line-numbers'
30
+ OPTIONS[:linenos] = 'on'
31
+ end
32
+ end
33
+
34
+ if ARGV.empty?
35
+ puts STDIN.read.highlight
36
+ else
37
+ ARGV.each_with_index do |file,id|
38
+ puts File.read(file).highlight
39
+ end
40
+ end
data/lib/pygmalion.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'active_support'
4
+
5
+ module Pygmalion
6
+ PYGMENTS_HOST = 'pygments.com'
7
+
8
+ JSON_PATH = "/json/"
9
+ PUSH_PATH = "/push_chunk/"
10
+
11
+ CHUNK_SIZE = 4000
12
+ CHUNK_REGEX = Regexp.new(".{1,#{CHUNK_SIZE}}", Regexp::MULTILINE)
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :css => 'n',
16
+ :id => 'pygmalion',
17
+ :linenos => 'off'
18
+ }
19
+
20
+ def self.included(base)
21
+ base.extend ClassMethods
22
+ end
23
+
24
+ module ClassMethods
25
+ #
26
+ # build query string for pygments.com
27
+ #
28
+ def pygments_query(parameters = {}, code = nil, chunkid = nil)
29
+ parameters[:code] = code
30
+ parameters[:chunkid] = chunkid if chunkid
31
+ parameters = parameters.collect { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
32
+ query = [ chunkid ? PUSH_PATH : JSON_PATH ]
33
+ query << parameters unless parameters.empty?
34
+ query.join('?')
35
+ end
36
+ end
37
+
38
+ #
39
+ # return a syntax-highlighted version of the string
40
+ #
41
+ # options:
42
+ #
43
+ # **line_numbers**: boolean indicating whether or not to include line numbers
44
+ # **language**: language to use for highlighting
45
+ #
46
+ def highlight(options = {})
47
+ # strip out options needing translation
48
+ line_numbers = options.delete(:line_numbers)
49
+ language = options.delete(:language)
50
+
51
+ # merge provided options into the defaults
52
+ options = DEFAULT_OPTIONS.merge(options)
53
+
54
+ # translate the aforementioned options
55
+ options[:linenos] = line_numbers ? 'on' : 'off'
56
+ options[:type] = language
57
+
58
+ res = Net::HTTP::start(PYGMENTS_HOST) do |http|
59
+ if self.size <= CHUNK_SIZE
60
+ # if code is small enough, use a single request
61
+ http.get self.class.pygments_query(options, self)
62
+ else
63
+ # otherwise break into chunks and push to the server
64
+ headers = {}
65
+ self.scan(CHUNK_REGEX).each_with_index do |chunk, chunkid|
66
+ # TODO: try threads for potential speedup with multiple simultaneous requests if this proves too slow
67
+ response = http.get self.class.pygments_query(options, chunk, chunkid), headers
68
+ # the server uses a cookie-based session to deal with chunked requests
69
+ headers['cookie'] = response['set-cookie'] if response['set-cookie']
70
+ end
71
+ # get the final response once all chunks are processed
72
+ http.get self.class.pygments_query(options), headers
73
+ end
74
+ end
75
+
76
+ ActiveSupport::JSON.decode(res.body[/\{.*\}/])['output']
77
+ end
78
+ end
79
+
80
+ String.send :include, Pygmalion
data/pygmalion.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ SPEC = Gem::Specification.new do |s|
2
+ # identify the gem
3
+ s.name = "pygmalion"
4
+ s.version = "0.9.0"
5
+ s.author = "S. Brent Faulkner"
6
+ s.email = "brentf@unwwwired.net"
7
+ s.homepage = "http://www.unwwwired.net"
8
+ # platform of choice
9
+ s.platform = Gem::Platform::RUBY
10
+ # description of gem
11
+ s.summary = "A ruby implementation of an interactive SQL command-line for ODBC data sources"
12
+ s.files = %w(bin/pygmalion lib/pygmalion.rb MIT-LICENSE Rakefile README.markdown pygmalion.gemspec)
13
+ s.require_path = "lib"
14
+ s.autorequire = "pygmalion"
15
+ # s.test_file = "test/pygmalion.rb"
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README.markdown"]
18
+ s.add_dependency("fastercsv", ">= 1.2.3")
19
+ s.executables = ["pygmalion"]
20
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sbfaulkner-pygmalion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - S. Brent Faulkner
8
+ autorequire: pygmalion
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fastercsv
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.3
23
+ version:
24
+ description:
25
+ email: brentf@unwwwired.net
26
+ executables:
27
+ - pygmalion
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.markdown
32
+ files:
33
+ - bin/pygmalion
34
+ - lib/pygmalion.rb
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - README.markdown
38
+ - pygmalion.gemspec
39
+ has_rdoc: true
40
+ homepage: http://www.unwwwired.net
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: A ruby implementation of an interactive SQL command-line for ODBC data sources
65
+ test_files: []
66
+