ink 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ = Introduction
2
+
3
+ Ink is a wrapper for Pygments[http://pygments.org/], a nice Python syntax highlighting library, and formatters like RDoc, Markdown and Textile.
4
+
5
+ = Install
6
+
7
+ sudo gem install ink
8
+
9
+ If you want the source go to http://github.com/fnando/ink
10
+
11
+ = Usage
12
+
13
+ require "rubygems"
14
+ require "ink"
15
+
16
+ # Setting pygmentize when is not in your $PATH
17
+ Ink::Highlight.bin = "/other/path/pygmentize"
18
+
19
+ # Syntax highlighting
20
+ Ink::Highlight.highlight("Rakefile")
21
+ Ink::Highlight.highlight("Rakefile", :format => :terminal)
22
+ Ink::Highlight.highlight("Rakefile", :language => "rb")
23
+ Ink::Highlight.highlight("Rakefile", :guess => :filename)
24
+ Ink::Highlight.highlight("Rakefile", :guess => :content)
25
+
26
+ # Formatting
27
+ Ink::Formatter.format(@code, "Rakefile")
28
+
29
+ == Ruby on Rails
30
+
31
+ You can add the Ink::Helper module.
32
+
33
+ module ApplicationHelper
34
+ include Ink::Helper
35
+ end
36
+
37
+ This module adds two methods.
38
+
39
+ <%= highlight @code, :file => "Rakefile" %>
40
+ <%= highlight @code, :language => :ruby %>
41
+
42
+ <%= format @code, "Rakefile" %>
43
+
44
+ = License
45
+
46
+ (The MIT License)
47
+
48
+ Copyright © 2010:
49
+
50
+ * Nando Vieira - http://simplesideias.com.br
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ require "rdiscount"
2
+ require "RedCloth"
3
+ require "rdoc/markup"
4
+ require "rdoc/markup/to_html"
5
+ require "tempfile"
6
+ require "open3"
7
+ require "ink/version"
8
+ require "ink/helper"
9
+ require "ink/highlight"
10
+ require "ink/formatter"
@@ -0,0 +1,28 @@
1
+ module Ink
2
+ module Formatter
3
+ extend Ink::Helper
4
+
5
+ # Format the specified content based on the
6
+ # filename. It can detect Markdown (<tt>*.{markdown,mkdn}</tt>),
7
+ # Textile (<tt>*.textile</tt>), RDoc (<tt>*.rdoc</tt>).
8
+ # Any other file will be highlighted by using Pygments and language detection
9
+ # based on filename.
10
+ #
11
+ # Ink::Formatter.format(@code, "Rakefile")
12
+ #
13
+ def self.format(content, file)
14
+ basename = File.basename(file)
15
+
16
+ case basename
17
+ when /\.rdoc$/i
18
+ RDoc::Markup::ToHtml.new.convert(content)
19
+ when /\.(markdown|mkdn)$/i
20
+ RDiscount.new(content).to_html
21
+ when /\.textile$/i
22
+ RedCloth.new(content).to_html
23
+ else
24
+ highlight(content, :file => file)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,43 @@
1
+ module Ink
2
+ # The Ink::Helper module is great for shortcuts. On Rails applications,
3
+ # can be added to ApplicationHelper.
4
+ #
5
+ # module ApplicationHelper
6
+ # include InkHelper
7
+ # end
8
+ #
9
+ module Helper
10
+ # Return a code snippet formatted by using
11
+ # the Ink::Formatter.format method.
12
+ #
13
+ # <%= format @code, "Rakefile" %>
14
+ #
15
+ def format(code, file)
16
+ Ink::Formatter.format(code, file)
17
+ end
18
+
19
+ # Return a code snippet highlighted in the specified
20
+ # language. When a <tt>:file</tt> option is provided,
21
+ # then language will be guessed by the filename.
22
+ # If no language is specified will set to <tt>:text</tt>.
23
+ #
24
+ # <%= highlight @code, :file => "Rakefile" %>
25
+ # <%= highlight @code, :language => :ruby %>
26
+ #
27
+ def highlight(code, options = {})
28
+ file = Tempfile.new("highlight")
29
+ File.open(file.path, "w+") do |f|
30
+ f << "#{code}\n"
31
+ end
32
+
33
+ if options[:file]
34
+ language = Highlight.guess_by_filename(options[:file])
35
+ else
36
+ language = options[:language] || :text
37
+ end
38
+
39
+ code = Ink::Highlight.highlight(file.path, :language => language)
40
+ %[<div class="code #{language}">#{code}</div>]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,115 @@
1
+ module Ink
2
+ # Pygments is a nice syntax highlighter for Python.
3
+ # This wrapper will call the +pygmentize+ bin and return
4
+ # a highlighted source code.
5
+ #
6
+ # If the +pygmentize+ bin is not available in your <tt>$PATH</tt>,
7
+ # you can set it:
8
+ #
9
+ # Ink::Highlight.bin = "/other/path/pygmentize"
10
+ #
11
+ # To use it with Ruby on Rails, you can include it on +ApplicationHelper+.
12
+ #
13
+ # module ApplicationHelper
14
+ # include Ink::Highlight::Helper
15
+ # end
16
+ #
17
+ # Then you can use it on views:
18
+ #
19
+ # <%= highlight @code, :ruby %>
20
+ #
21
+ module Highlight
22
+ class << self
23
+ # Set the bin that should be ran. It assumes that
24
+ # the +pygmentize+ bin is available on <tt>$PATH</tt>.
25
+ attr_accessor :bin
26
+ end
27
+
28
+ # Set default binary path. It assumes that is
29
+ # present on <tt>$PATH</tt>.
30
+ self.bin = "pygmentize"
31
+
32
+ # Store all available formatters.
33
+ FORMATTERS = [:bbcode, :html, :latex, :rtf, :terminal, :text]
34
+
35
+ FILENAME_MATCHERS = {
36
+ :rb => [ "Rakefile", "Capfile", "Gemfile", /\.(rb|rbx|rxml|builder|rjs|rake|rbw|gemspec)$/i ],
37
+ :rhtml => [ /\.rhtml$/i ],
38
+ :erb => [ /\.erb$/i ],
39
+ :js => [ /\.js$/i ],
40
+ "html+php" => [ /\.(phtml|php)$/i ],
41
+ :yaml => [ /\.ya?ml$/i ],
42
+ :xml => [ /\.(xml|xsl|rss|atom|xslt|xsd|wsdl)$/i ],
43
+ :erlang => [ /\.(erl|hrl)$/i ],
44
+ :css => [ /\.css$/i ],
45
+ :cucumber => [ /\.feature$/i ],
46
+ :c => [ /\.(c|h)$/i ],
47
+ :coffeescript => [ /\.coffee$/i ],
48
+ :haml => [ /\.haml$/i ],
49
+ :html => [ /\.x?html?$/i ],
50
+ :sass => [ /\.sass$/i ],
51
+ "objective-c" => [ /\.m$/i ],
52
+ :python => [ /\.(pyw?|sc|SConstruct|SConscript|tac)$/i ],
53
+ :scala => [ /\.scala$/i ],
54
+ }
55
+
56
+ # Try to guess what's the file's language by checking its content.
57
+ # If no language is detected, returns :text.
58
+ #
59
+ # Ink::Highlight.guess("Rakefile")
60
+ # #=> "rb"
61
+ #
62
+ def self.guess(file)
63
+ execute("#{bin} -N #{file.inspect}").to_sym
64
+ end
65
+
66
+ # Try to guess what's the file's language by checking its filename.
67
+ # If no language is detected, returns :text.
68
+ #
69
+ # Ink::Highlight.guess_by_filename("Rakefile")
70
+ # #=> "rb"
71
+ #
72
+ def self.guess_by_filename(file)
73
+ basename = File.basename(file)
74
+
75
+ FILENAME_MATCHERS.each do |lexer, matchers|
76
+ matchers.each do |matcher|
77
+ case matcher
78
+ when String
79
+ return lexer.to_s if matcher == basename
80
+ when Regexp
81
+ return lexer.to_s if basename =~ matcher
82
+ end
83
+ end
84
+ end
85
+
86
+ "text"
87
+ end
88
+
89
+ # Return the highlighted code for the specified file.
90
+ # You can provide the language (try to guess by default) and format (defaults to <tt>:html</tt>).
91
+ # The available formatters are stored in Highlight::FORMATTERS.
92
+ # The available languages can be found by running <tt>pygmentize -L lexers</tt>.
93
+ #
94
+ # Ink::Highlight.highlight("Rakefile")
95
+ # Ink::Highlight.highlight("Rakefile", :format => :terminal)
96
+ # Ink::Highlight.highlight("Rakefile", :language => "rb")
97
+ # Ink::Highlight.highlight("Rakefile", :guess => :filename)
98
+ # Ink::Highlight.highlight("Rakefile", :guess => :content)
99
+ #
100
+ def self.highlight(file, options = {})
101
+ options = {
102
+ :language => (options[:guess] == :filename ? guess_by_filename(file) : guess(file)),
103
+ :format => :html
104
+ }.merge(options)
105
+
106
+ execute("#{bin} -f #{options[:format]} -O nowrap=true -l #{options[:language]} #{file.inspect}")
107
+ end
108
+
109
+ def self.execute(command) # :nodoc:
110
+ stdin, stdout, stderr = Open3.popen3(command)
111
+ stdin.close
112
+ stdout.read.chomp
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,8 @@
1
+ module Ink
2
+ module Version # :nodoc: all
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+
3
+ class Ink::FormatterTest < Test::Unit::TestCase
4
+ def test_markdown
5
+ assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("Hello\n=====", "file.markdown")
6
+ assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("Hello\n=====", "file.mkdn")
7
+ end
8
+
9
+ def test_textile
10
+ assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("h1. Hello", "file.textile")
11
+ end
12
+
13
+ def test_highlight
14
+ assert_match %r{<div class="code rb">(.*?)</div>}, Ink::Formatter.format("puts 'Hello world!'", "file.rb")
15
+ end
16
+
17
+ def test_rdoc
18
+ assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("= Hello", "file.rdoc")
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ require "test_helper"
2
+
3
+ class Ink::HelperTest < Test::Unit::TestCase
4
+ def test_highlight_helper
5
+ helper = Object.extend(Ink::Helper)
6
+ html = helper.highlight(%[puts "Hello world!"], :language => :ruby)
7
+
8
+ assert_match /<div class="code ruby">(.*?)<\/div>/, html
9
+ end
10
+
11
+ def test_highlight_helper_using_defaults
12
+ helper = Object.extend(Ink::Helper)
13
+ html = helper.highlight(%[Hello world!])
14
+
15
+ assert_match /<div class="code text">(.*?)<\/div>/, html
16
+ end
17
+
18
+ def test_highlight_helper_using_guess
19
+ helper = Object.extend(Ink::Helper)
20
+ html = helper.highlight(%[puts "Hello world!"], :file => "hello.rb")
21
+
22
+ assert_match /<div class="code rb">(.*?)<\/div>/, html
23
+ end
24
+ end
@@ -0,0 +1,133 @@
1
+ require "test_helper"
2
+
3
+ class Ink::HighlightTest < Test::Unit::TestCase
4
+ def setup
5
+ Ink::Highlight.bin = "pygmentize"
6
+ end
7
+
8
+ def test_set_different_bin
9
+ Ink::Highlight.bin = "/some/path/pygmentize"
10
+ assert_equal "/some/path/pygmentize", Ink::Highlight.bin
11
+ end
12
+
13
+ def test_guess_lexer
14
+ assert_equal :rb, Ink::Highlight.guess(__FILE__)
15
+ end
16
+
17
+ def test_highlight_file_using_defaults
18
+ Ink::Highlight.expects(:execute).with("pygmentize -N #{__FILE__.inspect}").returns("ruby")
19
+ Ink::Highlight.expects(:execute).with("pygmentize -f html -O nowrap=true -l ruby #{__FILE__.inspect}")
20
+ Ink::Highlight.highlight(__FILE__)
21
+ end
22
+
23
+ def test_highlight_file_using_specified_language
24
+ Ink::Highlight.expects(:execute).with("pygmentize -N #{__FILE__.inspect}").returns("sample")
25
+ Ink::Highlight.expects(:execute).with("pygmentize -f html -O nowrap=true -l sample #{__FILE__.inspect}")
26
+ Ink::Highlight.highlight(__FILE__, :language => "sample")
27
+ end
28
+
29
+ def test_highlight_file_using_specified_format
30
+ Ink::Highlight.expects(:execute).with("pygmentize -N #{__FILE__.inspect}").returns("rb")
31
+ Ink::Highlight.expects(:execute).with("pygmentize -f terminal -O nowrap=true -l rb #{__FILE__.inspect}")
32
+ Ink::Highlight.highlight(__FILE__, :format => "terminal")
33
+ end
34
+
35
+ def test_guess_lexer_by_filename_default
36
+ assert_equal "text", Ink::Highlight.guess_by_filename("file")
37
+ end
38
+
39
+ def test_guess_lexer_by_filename_ruby
40
+ assert_equal "rb", Ink::Highlight.guess_by_filename("Rakefile")
41
+ assert_equal "rb", Ink::Highlight.guess_by_filename("Gemfile")
42
+ assert_equal "rb", Ink::Highlight.guess_by_filename("Capfile")
43
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.rb")
44
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.rbx")
45
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.rxml")
46
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.builder")
47
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.rjs")
48
+ assert_equal "rb", Ink::Highlight.guess_by_filename("tasks.rake")
49
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.rbw")
50
+ assert_equal "rb", Ink::Highlight.guess_by_filename("file.gemspec")
51
+ end
52
+
53
+ def test_guess_lexer_by_filename_erb
54
+ assert_equal "erb", Ink::Highlight.guess_by_filename("show.html.erb")
55
+ end
56
+
57
+ def test_guess_lexer_by_filename_rhtml
58
+ assert_equal "rhtml", Ink::Highlight.guess_by_filename("show.rhtml")
59
+ end
60
+
61
+ def test_guess_lexer_by_filename_js
62
+ assert_equal "js", Ink::Highlight.guess_by_filename("jquery.js")
63
+ end
64
+
65
+ def test_guess_lexer_by_filename_php
66
+ assert_equal "html+php", Ink::Highlight.guess_by_filename("file.phtml")
67
+ assert_equal "html+php", Ink::Highlight.guess_by_filename("file.php")
68
+ end
69
+
70
+ def test_guess_lexer_by_filename_yaml
71
+ assert_equal "yaml", Ink::Highlight.guess_by_filename("file.yaml")
72
+ assert_equal "yaml", Ink::Highlight.guess_by_filename("file.yml")
73
+ end
74
+
75
+ def test_guess_lexer_by_filename_xml
76
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.xml")
77
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.xslt")
78
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.xsl")
79
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.rss")
80
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.atom")
81
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.xsd")
82
+ assert_equal "xml", Ink::Highlight.guess_by_filename("file.wsdl")
83
+ end
84
+
85
+ def test_guess_lexer_by_filename_erlang
86
+ assert_equal "erlang", Ink::Highlight.guess_by_filename("file.erl")
87
+ assert_equal "erlang", Ink::Highlight.guess_by_filename("file.hrl")
88
+ end
89
+
90
+ def test_guess_lexer_by_filename_css
91
+ assert_equal "css", Ink::Highlight.guess_by_filename("file.css")
92
+ end
93
+
94
+ def test_guess_lexer_by_filename_cucumber
95
+ assert_equal "cucumber", Ink::Highlight.guess_by_filename("file.feature")
96
+ end
97
+
98
+ def test_guess_lexer_by_filename_c
99
+ assert_equal "c", Ink::Highlight.guess_by_filename("file.c")
100
+ assert_equal "c", Ink::Highlight.guess_by_filename("file.h")
101
+ end
102
+
103
+ def test_guess_lexer_by_filename_coffeescript
104
+ assert_equal "coffeescript", Ink::Highlight.guess_by_filename("file.coffee")
105
+ end
106
+
107
+ def test_guess_lexer_by_filename_haml
108
+ assert_equal "haml", Ink::Highlight.guess_by_filename("file.haml")
109
+ end
110
+
111
+ def test_guess_lexer_by_filename_sass
112
+ assert_equal "sass", Ink::Highlight.guess_by_filename("file.sass")
113
+ end
114
+
115
+ def test_guess_lexer_by_filename_html
116
+ assert_equal "html", Ink::Highlight.guess_by_filename("file.html")
117
+ assert_equal "html", Ink::Highlight.guess_by_filename("file.htm")
118
+ assert_equal "html", Ink::Highlight.guess_by_filename("file.xhtml")
119
+ end
120
+
121
+ def test_guess_lexer_by_filename_objective_c
122
+ assert_equal "objective-c", Ink::Highlight.guess_by_filename("file.m")
123
+ end
124
+
125
+ def test_guess_lexer_by_filename_python
126
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.py")
127
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.pyw")
128
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.sc")
129
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.SConstruct")
130
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.SConscript")
131
+ assert_equal "python", Ink::Highlight.guess_by_filename("file.tac")
132
+ end
133
+ end
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ gem "test-unit"
3
+ require "test/unit"
4
+ require "mocha"
5
+ require "ink"
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ink
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-12 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: RedCloth
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rdiscount
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: rdoc
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :runtime
55
+ version_requirements: *id003
56
+ description: Ink is a wrapper for Pygments, a nice Python syntax highlighting library and formatters like RDoc, Markdown and Textile.
57
+ email: fnando.vieira@gmail.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - README.rdoc
64
+ files:
65
+ - README.rdoc
66
+ - lib/ink.rb
67
+ - lib/ink/formatter.rb
68
+ - lib/ink/helper.rb
69
+ - lib/ink/highlight.rb
70
+ - lib/ink/version.rb
71
+ - test/ink/formatter_test.rb
72
+ - test/ink/helper_test.rb
73
+ - test/ink/highlight_test.rb
74
+ - test/test_helper.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/fnando/ink
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Ink is a wrapper for Pygments, a nice Python syntax highlighting library and formatters like RDoc, Markdown and Textile.
105
+ test_files:
106
+ - test/ink/formatter_test.rb
107
+ - test/ink/helper_test.rb
108
+ - test/ink/highlight_test.rb
109
+ - test/test_helper.rb