ddollar-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 (3) hide show
  1. data/README.rdoc +51 -0
  2. data/lib/albino.rb +124 -0
  3. metadata +54 -0
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = Albino
2
+
3
+ == Installation
4
+
5
+ $ gem sources -a http://gems.github.com
6
+ $ gem install ddollar-albino
7
+
8
+ == Usage
9
+
10
+ To print out an HTMLized, Ruby-highlighted version of '/some/file.rb'
11
+
12
+ @syntaxer = Albino.new('/some/file.rb', :ruby)
13
+ puts @syntaxer.colorize
14
+
15
+ To use another formatter, pass it as the third argument:
16
+
17
+ @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode)
18
+ puts @syntaxer.colorize
19
+
20
+ You can also use the #colorize class method:
21
+
22
+ puts Albino.colorize('/some/file.rb', :ruby)
23
+
24
+ Another also: you get a #to_s, for somewhat nicer use in Rails views.
25
+
26
+ ... helper file ...
27
+ def highlight(text)
28
+ Albino.new(text, :ruby)
29
+ end
30
+
31
+ ... view file ...
32
+ <%= highlight text %>
33
+
34
+ The default lexer is 'text'. You need to specify a lexer yourself;
35
+ because we are using STDIN there is no auto-detect.
36
+
37
+ To see all lexers and formatters available, run `pygmentize -L`.
38
+
39
+ == Documentation
40
+
41
+ RDoc can be found at http://ddollar.github.com/albino
42
+
43
+ == Credits
44
+
45
+ This project is an extraction from GitHub.
46
+
47
+ For this and other extractions, see http://github.com/github
48
+
49
+ The Albino code was all written by Chris Wanstrath chris@ozmm.org
50
+
51
+ Wrapped in a gem by David Dollar http://daviddollar.org
data/lib/albino.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'open4'
2
+
3
+ #
4
+ # Wrapper for the Pygments command line tool, pygmentize.
5
+ #
6
+ # Pygments: http://pygments.org
7
+ #
8
+ # Assumes pygmentize is in the path. If not, set its location
9
+ # with Albino.bin = '/path/to/pygmentize'
10
+ #
11
+ # Use like so:
12
+ #
13
+ # @syntaxer = Albino.new('/some/file.rb', :ruby)
14
+ # puts @syntaxer.colorize
15
+ #
16
+ # This'll print out an HTMLized, Ruby-highlighted version
17
+ # of '/some/file.rb'.
18
+ #
19
+ # To use another formatter, pass it as the third argument:
20
+ #
21
+ # @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode)
22
+ # puts @syntaxer.colorize
23
+ #
24
+ # You can also use the #colorize class method:
25
+ #
26
+ # puts Albino.colorize('/some/file.rb', :ruby)
27
+ #
28
+ # Another also: you get a #to_s, for somewhat nicer use in Rails views.
29
+ #
30
+ # ... helper file ...
31
+ # def highlight(text)
32
+ # Albino.new(text, :ruby)
33
+ # end
34
+ #
35
+ # ... view file ...
36
+ # <%= highlight text %>
37
+ #
38
+ # The default lexer is 'text'. You need to specify a lexer yourself;
39
+ # because we are using STDIN there is no auto-detect.
40
+ #
41
+ # To see all lexers and formatters available, run `pygmentize -L`.
42
+ #
43
+ # Chris Wanstrath // chris@ozmm.org
44
+ #
45
+ # GitHub // http://github.com
46
+ #
47
+ class Albino
48
+
49
+ def self.rails_in_development?
50
+ Rails.development?
51
+ rescue
52
+ false
53
+ end
54
+
55
+ @@bin = rails_in_development? ? 'pygmentize' : '/usr/bin/pygmentize'
56
+
57
+ def self.bin=(path)
58
+ @@bin = path
59
+ end
60
+
61
+ def self.colorize(*args)
62
+ new(*args).colorize
63
+ end
64
+
65
+ def initialize(target, lexer = :text, format = :html)
66
+ @target = File.exists?(target) ? File.read(target) : target rescue target
67
+ @options = { :l => lexer, :f => format }
68
+ end
69
+
70
+ def execute(command)
71
+ pid, stdin, stdout, stderr = Open4.popen4(command)
72
+ stdin.puts @target
73
+ stdin.close
74
+ stdout.read.strip
75
+ end
76
+
77
+ def colorize(options = {})
78
+ execute @@bin + convert_options(options)
79
+ end
80
+ alias_method :to_s, :colorize
81
+
82
+ def convert_options(options = {})
83
+ @options.merge(options).inject('') do |string, (flag, value)|
84
+ string + " -#{flag} #{value}"
85
+ end
86
+ end
87
+ end
88
+
89
+ if $0 == __FILE__
90
+ require 'rubygems'
91
+ require 'test/spec'
92
+ require 'mocha'
93
+ begin require 'redgreen'; rescue LoadError; end
94
+
95
+ context "Albino" do
96
+ setup do
97
+ @syntaxer = Albino.new(__FILE__, :ruby)
98
+ end
99
+
100
+ specify "defaults to text" do
101
+ syntaxer = Albino.new(__FILE__)
102
+ syntaxer.expects(:execute).with('pygmentize -f html -l text').returns(true)
103
+ syntaxer.colorize
104
+ end
105
+
106
+ specify "accepts options" do
107
+ @syntaxer.expects(:execute).with('pygmentize -f html -l ruby').returns(true)
108
+ @syntaxer.colorize
109
+ end
110
+
111
+ specify "works with strings" do
112
+ syntaxer = Albino.new('class New; end', :ruby)
113
+ assert_match %r(highlight), syntaxer.colorize
114
+ end
115
+
116
+ specify "aliases to_s" do
117
+ assert_equal @syntaxer.colorize, @syntaxer.to_s
118
+ end
119
+
120
+ specify "class method colorize" do
121
+ assert_equal @syntaxer.colorize, Albino.colorize(__FILE__, :ruby)
122
+ end
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ddollar-albino
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - David Dollar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Albino is an interface to the Python-based Pygments library. The code was all written by Chris Wanstrath and can be found at http://github.com/github/albino David Dollar wrapped it in a gem.
17
+ email: ddollar@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/albino.rb
26
+ - README.rdoc
27
+ has_rdoc: true
28
+ homepage: http://github.com/ddollar/albino
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project: albino
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Ruby wrapper for Pygments syntax-highlighting library
53
+ test_files: []
54
+