albino 1.3.0 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
@@ -0,0 +1,29 @@
1
+ # Albino: a ruby wrapper for pygmentize
2
+
3
+ This project is an extraction from GitHub.
4
+
5
+ For this and other extractions, see [http://github.com/github]()
6
+
7
+ ## Installation
8
+
9
+ sudo easy_install pygments
10
+ gem install albino
11
+
12
+ ## Usage
13
+
14
+ ### Simple
15
+
16
+ require 'albino'
17
+ puts Albino.colorize('puts "Hello World"', :ruby)
18
+
19
+ ### Advanced
20
+
21
+ require 'albino'
22
+ @syntaxer = Albino.new(File.new('albino.rb'), :ruby, :bbcode)
23
+ puts @syntaxer.colorize
24
+
25
+ ### Multi
26
+
27
+ require 'albino/multi'
28
+ ruby, python = Albino::Multi.colorize([ ['1+2',:ruby], ['1-2',:python] ])
29
+
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'albino'
16
- s.version = '1.3.0'
17
- s.date = '2011-03-03'
16
+ s.version = '1.3.2'
17
+ s.date = '2011-03-06'
18
18
  s.rubyforge_project = 'albino'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
33
33
  ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
34
  s.require_paths = %w[lib]
35
35
 
36
- s.add_dependency('posix-spawn')
36
+ s.add_dependency('posix-spawn', '>= 0.3.3')
37
37
  s.add_development_dependency('mocha')
38
38
 
39
39
  ## Leave this section as-is. It will be automatically generated from the
@@ -41,8 +41,9 @@ Gem::Specification.new do |s|
41
41
  ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
42
42
  # = MANIFEST =
43
43
  s.files = %w[
44
+ Gemfile
44
45
  LICENSE
45
- README
46
+ README.md
46
47
  Rakefile
47
48
  albino.gemspec
48
49
  lib/albino.rb
@@ -10,7 +10,7 @@ require 'posix-spawn'
10
10
  #
11
11
  # Use like so:
12
12
  #
13
- # @syntaxer = Albino.new('/some/file.rb', :ruby)
13
+ # @syntaxer = Albino.new('puts "Hello World"', :ruby)
14
14
  # puts @syntaxer.colorize
15
15
  #
16
16
  # This'll print out an HTMLized, Ruby-highlighted version
@@ -18,12 +18,16 @@ require 'posix-spawn'
18
18
  #
19
19
  # To use another formatter, pass it as the third argument:
20
20
  #
21
- # @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode)
21
+ # @syntaxer = Albino.new('puts "Hello World"', :ruby, :bbcode)
22
22
  # puts @syntaxer.colorize
23
23
  #
24
24
  # You can also use the #colorize class method:
25
25
  #
26
- # puts Albino.colorize('/some/file.rb', :ruby)
26
+ # puts Albino.colorize('puts "Hello World"', :ruby)
27
+ #
28
+ # To format a file, pass a file stream:
29
+ #
30
+ # puts Albino.colorize(File.new('/some/file.rb'), :ruby)
27
31
  #
28
32
  # Another also: you get a #to_s, for somewhat nicer use in Rails views.
29
33
  #
@@ -47,14 +51,22 @@ class Albino
47
51
  class ShellArgumentError < ArgumentError; end
48
52
  include POSIX::Spawn
49
53
 
50
- VERSION = '1.3.0'
54
+ VERSION = '1.3.2'
51
55
 
52
56
  class << self
53
- attr_accessor :bin, :default_encoding, :timeout_threshold
57
+ attr_accessor :bin, :timeout_threshold
58
+ attr_reader :default_encoding
59
+
60
+ def default_encoding=(encoding)
61
+ # make sure the encoding is valid
62
+ Encoding.find(encoding) if defined?(Encoding)
63
+
64
+ @default_encoding = encoding
65
+ end
54
66
  end
55
67
 
56
68
  self.timeout_threshold = 10
57
- self.default_encoding = 'utf8'
69
+ self.default_encoding = 'utf-8'
58
70
  self.bin = 'pygmentize'
59
71
 
60
72
  def self.colorize(*args)
@@ -64,6 +76,7 @@ class Albino
64
76
  def initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding)
65
77
  @target = target
66
78
  @options = { :l => lexer, :f => format, :O => "encoding=#{encoding}" }
79
+ @encoding = encoding
67
80
  end
68
81
 
69
82
  def execute(options = {})
@@ -75,7 +88,15 @@ class Albino
75
88
  end
76
89
 
77
90
  def colorize(options = {})
78
- execute(options).out
91
+ out = execute(options).out
92
+
93
+ # markdown requires block elements on their own line
94
+ out.sub!(%r{</pre></div>\Z}, "</pre>\n</div>")
95
+
96
+ # covert output to the encoding we told pygmentize to use
97
+ out.force_encoding(@encoding) if out.respond_to?(:force_encoding)
98
+
99
+ out
79
100
  end
80
101
  alias_method :to_s, :colorize
81
102
 
@@ -1,10 +1,6 @@
1
- require 'posix-spawn'
1
+ require 'albino'
2
2
 
3
3
  class Albino
4
- if !const_defined?(:ShellArgumentError)
5
- class ShellArgumentError < ArgumentError; end
6
- end
7
-
8
4
  # Wrapper for a custom multipygmentize script. Passes multiple code
9
5
  # fragments in STDIN to Python. This assumes both Python and pygments are
10
6
  # installed.
@@ -86,8 +82,15 @@ class Albino
86
82
 
87
83
  memo << code << SEPARATOR
88
84
  end.join("")
85
+
89
86
  child = Child.new(self.class.bin, options)
90
- pieces = child.out.split(SEPARATOR)
87
+ pieces = child.out.split(SEPARATOR).each do |code|
88
+ # markdown requires block elements on their own line
89
+ code.sub!(%r{</pre></div>\Z}, "</pre>\n</div>")
90
+
91
+ # albino::multi assumes utf8 encoding
92
+ code.force_encoding('UTF-8') if code.respond_to?(:force_encoding)
93
+ end
91
94
  @multi ? pieces : pieces.first
92
95
  end
93
96
 
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  require 'rubygems'
2
4
  require 'albino'
3
5
  require 'test/unit'
@@ -23,12 +25,23 @@ class AlbinoTest < Test::Unit::TestCase
23
25
  assert_equal '', @syntaxer.colorize(:f => 'html+c#-dump')
24
26
  end
25
27
 
28
+ def test_markdown_compatible
29
+ code = Albino.colorize('1+2', :ruby)
30
+ assert_no_match %r{</pre></div>\Z}, code
31
+ end
32
+
26
33
  def test_works_with_strings
27
34
  syntaxer = Albino.new("class New\nend", :ruby)
28
35
  assert_match %r(highlight), code=syntaxer.colorize
29
36
  assert_match %(<span class="nc">New</span>\n), code
30
37
  end
31
38
 
39
+ def test_works_with_utf8_strings
40
+ code = Albino.new("# é", :bash).colorize
41
+ assert_match %r(highlight), code
42
+ assert_match %(<span class="c"># é</span>), code
43
+ end
44
+
32
45
  def test_works_with_files
33
46
  contents = "class New\nend"
34
47
  syntaxer = Albino.new(contents, :ruby)
@@ -42,6 +55,36 @@ class AlbinoTest < Test::Unit::TestCase
42
55
  end
43
56
  end
44
57
 
58
+ def test_default_encoding
59
+ assert_equal Albino.default_encoding, 'utf-8'
60
+ end
61
+
62
+ def test_change_encoding
63
+ before = Albino.default_encoding
64
+
65
+ assert_equal Albino.default_encoding, 'utf-8'
66
+ Albino.default_encoding = 'ascii'
67
+ assert_equal Albino.default_encoding, 'ascii'
68
+ ensure
69
+ Albino.default_encoding = before
70
+ end
71
+
72
+ def test_invalid_encoding
73
+ before = Albino.default_encoding
74
+ Albino.default_encoding = 'binary'
75
+
76
+ assert_equal Albino.colorize('class Baño; end', :ruby), ''
77
+ ensure
78
+ Albino.default_encoding = before
79
+ end
80
+
81
+ def test_custom_encoding
82
+ code = Albino.new('1+2', :ruby, :html, 'ascii').colorize
83
+ if code.respond_to?(:encoding)
84
+ assert_equal code.encoding.to_s, 'US-ASCII'
85
+ end
86
+ end
87
+
45
88
  def test_aliases_to_s
46
89
  syntaxer = Albino.new(File.new(__FILE__), :ruby)
47
90
  assert_equal @syntaxer.colorize, syntaxer.to_s
@@ -15,6 +15,18 @@ class MultiTest < Test::Unit::TestCase
15
15
  assert_no_match regex, syntaxer.colorize
16
16
  end
17
17
 
18
+ def test_markdown_compatible
19
+ code = Albino::Multi.colorize('1+2', :ruby)
20
+ assert_no_match %r{</pre></div>\Z}, code
21
+ end
22
+
23
+ def test_forces_utf8
24
+ code = Albino::Multi.colorize('1+2', :ruby)
25
+ if code.respond_to?(:encoding)
26
+ assert_equal 'UTF-8', code.encoding.to_s
27
+ end
28
+ end
29
+
18
30
  def test_works_with_strings
19
31
  syntaxer = Albino::Multi.new("class New\nend", :ruby)
20
32
  assert_match %r(highlight), code=syntaxer.colorize
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: albino
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
4
+ hash: 31
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 0
10
- version: 1.3.0
9
+ - 2
10
+ version: 1.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Wanstrath
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-03 00:00:00 -08:00
18
+ date: 2011-03-06 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,10 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 3
29
+ hash: 21
30
30
  segments:
31
31
  - 0
32
- version: "0"
32
+ - 3
33
+ - 3
34
+ version: 0.3.3
33
35
  type: :runtime
34
36
  version_requirements: *id001
35
37
  - !ruby/object:Gem::Dependency
@@ -55,8 +57,9 @@ extensions: []
55
57
  extra_rdoc_files: []
56
58
 
57
59
  files:
60
+ - Gemfile
58
61
  - LICENSE
59
- - README
62
+ - README.md
60
63
  - Rakefile
61
64
  - albino.gemspec
62
65
  - lib/albino.rb
@@ -94,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
97
  requirements: []
95
98
 
96
99
  rubyforge_project: albino
97
- rubygems_version: 1.3.7
100
+ rubygems_version: 1.4.2
98
101
  signing_key:
99
102
  specification_version: 2
100
103
  summary: Ruby wrapper for pygmentize.
data/README DELETED
@@ -1,3 +0,0 @@
1
- This project is an extraction from GitHub.
2
-
3
- For this and other extractions, see http://github.com/github