albino 1.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.
Files changed (7) hide show
  1. data/LICENSE +21 -0
  2. data/README +3 -0
  3. data/Rakefile +130 -0
  4. data/albino.gemspec +55 -0
  5. data/lib/albino.rb +95 -0
  6. data/test/albino_test.rb +48 -0
  7. metadata +77 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Chris Wanstrath
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 ADDED
@@ -0,0 +1,3 @@
1
+ This project is an extraction from GitHub.
2
+
3
+ For this and other extractions, see http://github.com/github
data/Rakefile ADDED
@@ -0,0 +1,130 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/*_test.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Open an irb session preloaded with this library"
56
+ task :console do
57
+ sh "irb -rubygems -r ./lib/#{name}.rb"
58
+ end
59
+
60
+ #############################################################################
61
+ #
62
+ # Custom tasks (add your own tasks here)
63
+ #
64
+ #############################################################################
65
+
66
+
67
+
68
+ #############################################################################
69
+ #
70
+ # Packaging tasks
71
+ #
72
+ #############################################################################
73
+
74
+ task :release => :build do
75
+ unless `git branch` =~ /^\* master$/
76
+ puts "You must be on the master branch to release!"
77
+ exit!
78
+ end
79
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
80
+ sh "git tag v#{version}"
81
+ sh "git push origin master"
82
+ sh "git push v#{version}"
83
+ sh "gem push pkg/#{name}-#{version}.gem"
84
+ end
85
+
86
+ task :build => :gemspec do
87
+ sh "mkdir -p pkg"
88
+ sh "gem build #{gemspec_file}"
89
+ sh "mv #{gem_file} pkg"
90
+ end
91
+
92
+ task :gemspec => :validate do
93
+ # read spec file and split out manifest section
94
+ spec = File.read(gemspec_file)
95
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
96
+
97
+ # replace name version and date
98
+ replace_header(head, :name)
99
+ replace_header(head, :version)
100
+ replace_header(head, :date)
101
+ #comment this out if your rubyforge_project has a different name
102
+ replace_header(head, :rubyforge_project)
103
+
104
+ # determine file list from git ls-files
105
+ files = `git ls-files`.
106
+ split("\n").
107
+ sort.
108
+ reject { |file| file =~ /^\./ }.
109
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
110
+ map { |file| " #{file}" }.
111
+ join("\n")
112
+
113
+ # piece file back together and write
114
+ manifest = " s.files = %w[\n#{files}\n ]\n"
115
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
116
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
117
+ puts "Updated #{gemspec_file}"
118
+ end
119
+
120
+ task :validate do
121
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
122
+ unless libfiles.empty?
123
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
124
+ exit!
125
+ end
126
+ unless Dir['VERSION*'].empty?
127
+ puts "A `VERSION` file at root level violates Gem best practices."
128
+ exit!
129
+ end
130
+ end
data/albino.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'albino'
16
+ s.version = '1.0'
17
+ s.date = '2010-05-04'
18
+ s.rubyforge_project = 'albino'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Ruby wrapper for pygmentize."
23
+ s.description = "Ruby wrapper for pygmentize."
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Chris Wanstrath"]
29
+ s.email = 'chris@wanstrath.com'
30
+ s.homepage = 'http://github.com/github/albino'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ s.add_development_dependency('mocha')
37
+
38
+ ## Leave this section as-is. It will be automatically generated from the
39
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
40
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
41
+ # = MANIFEST =
42
+ s.files = %w[
43
+ LICENSE
44
+ README
45
+ Rakefile
46
+ albino.gemspec
47
+ lib/albino.rb
48
+ test/albino_test.rb
49
+ ]
50
+ # = MANIFEST =
51
+
52
+ ## Test files will be grabbed from the file list. Make sure the path glob
53
+ ## matches what you actually use.
54
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
55
+ end
data/lib/albino.rb ADDED
@@ -0,0 +1,95 @@
1
+ ##
2
+ # Wrapper for the Pygments command line tool, pygmentize.
3
+ #
4
+ # Pygments: http://pygments.org/
5
+ #
6
+ # Assumes pygmentize is in the path. If not, set its location
7
+ # with Albino.bin = '/path/to/pygmentize'
8
+ #
9
+ # Use like so:
10
+ #
11
+ # @syntaxer = Albino.new('/some/file.rb', :ruby)
12
+ # puts @syntaxer.colorize
13
+ #
14
+ # This'll print out an HTMLized, Ruby-highlighted version
15
+ # of '/some/file.rb'.
16
+ #
17
+ # To use another formatter, pass it as the third argument:
18
+ #
19
+ # @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode)
20
+ # puts @syntaxer.colorize
21
+ #
22
+ # You can also use the #colorize class method:
23
+ #
24
+ # puts Albino.colorize('/some/file.rb', :ruby)
25
+ #
26
+ # Another also: you get a #to_s, for somewhat nicer use in Rails views.
27
+ #
28
+ # ... helper file ...
29
+ # def highlight(text)
30
+ # Albino.new(text, :ruby)
31
+ # end
32
+ #
33
+ # ... view file ...
34
+ # <%= highlight text %>
35
+ #
36
+ # The default lexer is 'text'. You need to specify a lexer yourself;
37
+ # because we are using STDIN there is no auto-detect.
38
+ #
39
+ # To see all lexers and formatters available, run `pygmentize -L`.
40
+ #
41
+ # Chris Wanstrath // chris@ozmm.org
42
+ # GitHub // http://github.com
43
+ #
44
+ class Albino
45
+ VERSION = '1.0'
46
+
47
+ class << self
48
+ attr_accessor :bin
49
+ end
50
+ self.bin = 'pygmentize'
51
+
52
+ def self.colorize(*args)
53
+ new(*args).colorize
54
+ end
55
+
56
+ def initialize(target, lexer = :text, format = :html)
57
+ @target = target
58
+ @options = { :l => lexer, :f => format }
59
+ end
60
+
61
+ def execute(command)
62
+ output = ''
63
+ IO.popen(command, mode='r+') do |p|
64
+ write_target_to_stream(p)
65
+ p.close_write
66
+ output = p.read.strip
67
+ end
68
+ output
69
+ end
70
+
71
+ def colorize(options = {})
72
+ execute bin + convert_options(options)
73
+ end
74
+ alias_method :to_s, :colorize
75
+
76
+ def convert_options(options = {})
77
+ @options.merge(options).inject('') do |string, (flag, value)|
78
+ string + " -#{flag} #{value}"
79
+ end
80
+ end
81
+
82
+ def write_target_to_stream(stream)
83
+ if File.exists?(@target)
84
+ File.open(@target) do |f|
85
+ f.each { |l| stream << l }
86
+ end
87
+ else
88
+ stream << @target
89
+ end
90
+ end
91
+
92
+ def bin
93
+ self.class.bin
94
+ end
95
+ end
@@ -0,0 +1,48 @@
1
+ require 'albino'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'tempfile'
5
+ require 'mocha'
6
+
7
+ class AlbinoTest < Test::Unit::TestCase
8
+ def setup
9
+ @syntaxer = Albino.new(__FILE__, :ruby)
10
+ end
11
+
12
+ def test_defaults_to_text
13
+ syntaxer = Albino.new(__FILE__)
14
+ syntaxer.expects(:execute).with('pygmentize -f html -l text').returns(true)
15
+ syntaxer.colorize
16
+ end
17
+
18
+ def test_accepts_options
19
+ @syntaxer.expects(:execute).with('pygmentize -f html -l ruby').returns(true)
20
+ @syntaxer.colorize
21
+ end
22
+
23
+ def test_works_with_strings
24
+ syntaxer = Albino.new("class New\nend", :ruby)
25
+ assert_match %r(highlight), syntaxer.colorize
26
+ end
27
+
28
+ def test_works_with_files
29
+ contents = "class New\nend"
30
+ syntaxer = Albino.new(contents, :ruby)
31
+ file_output = syntaxer.colorize
32
+
33
+ Tempfile.open 'albino-test' do |tmp|
34
+ tmp << contents
35
+ tmp.flush
36
+ syntaxer = Albino.new(tmp.path, :ruby)
37
+ assert_equal file_output, syntaxer.colorize
38
+ end
39
+ end
40
+
41
+ def test_aliases_to_s
42
+ assert_equal @syntaxer.colorize, @syntaxer.to_s
43
+ end
44
+
45
+ def test_class_method_colorize
46
+ assert_equal @syntaxer.colorize, Albino.colorize(__FILE__, :ruby)
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: albino
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ version: "1.0"
9
+ platform: ruby
10
+ authors:
11
+ - Chris Wanstrath
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-05-04 00:00:00 -07:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: mocha
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ version: "0"
29
+ type: :development
30
+ version_requirements: *id001
31
+ description: Ruby wrapper for pygmentize.
32
+ email: chris@wanstrath.com
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - LICENSE
41
+ - README
42
+ - Rakefile
43
+ - albino.gemspec
44
+ - lib/albino.rb
45
+ - test/albino_test.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/github/albino
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: albino
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Ruby wrapper for pygmentize.
76
+ test_files: []
77
+