ink 0.1.1 → 0.1.2

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.
@@ -9,20 +9,34 @@ module Ink
9
9
  # based on filename.
10
10
  #
11
11
  # Ink::Formatter.format(@code, "Rakefile")
12
+ # Ink::Formatter.format(@code, "Rakefile", :line_numbers => true)
13
+ # Ink::Formatter.format(@code, "Rakefile") {|type, html| }
12
14
  #
13
- def self.format(content, file)
15
+ # If you provide a block, the +format+ helper will yield the type
16
+ # (<tt>:textile</tt>, <tt>:markdown</tt>, <tt>:rdoc</tt> or <tt>:code</tt>)
17
+ # and the generated markup.
18
+ #
19
+ def self.format(content, file, options = {}, &block)
14
20
  basename = File.basename(file)
15
21
 
16
22
  case basename
17
23
  when /\.rdoc$/i
18
- RDoc::Markup::ToHtml.new.convert(content)
24
+ html = RDoc::Markup::ToHtml.new.convert(content)
25
+ type = :rdoc
19
26
  when /\.(markdown|mkdn)$/i
20
- RDiscount.new(content).to_html
27
+ html = RDiscount.new(content).to_html
28
+ type = :markdown
21
29
  when /\.textile$/i
22
- RedCloth.new(content).to_html
30
+ html = RedCloth.new(content).to_html
31
+ type = :textile
23
32
  else
24
- highlight(content, :file => file)
33
+ html = highlight(content, options.merge(:file => file))
34
+ type = :code
25
35
  end
36
+
37
+ yield(type, html) if block_given?
38
+
39
+ html
26
40
  end
27
41
  end
28
42
  end
@@ -11,9 +11,10 @@ module Ink
11
11
  # the Ink::Formatter.format method.
12
12
  #
13
13
  # <%= format @code, "Rakefile" %>
14
+ # <%= format @code, "Rakefile", :line_numbers => true %>
14
15
  #
15
- def format(code, file)
16
- Ink::Formatter.format(code, file)
16
+ def format(code, file, options = {})
17
+ Ink::Formatter.format(code, file, options)
17
18
  end
18
19
 
19
20
  # Return a code snippet highlighted in the specified
@@ -37,7 +38,21 @@ module Ink
37
38
  end
38
39
 
39
40
  code = Ink::Highlight.highlight(file.path, :language => language)
40
- %[<div class="code #{language}"><pre>#{code}</pre></div>]
41
+
42
+ if options[:line_numbers]
43
+ io = StringIO.new(code)
44
+
45
+ rows = ""
46
+
47
+ io.lines.each_with_index do |line, i|
48
+ rows << %[<tr><td class="line">#{i+1}</td>]
49
+ rows << %[<td class="code">#{line}</td></tr>]
50
+ end
51
+
52
+ %[<table class="highlight">#{rows}</table>]
53
+ else
54
+ %[<pre>#{code}</pre>]
55
+ end
41
56
  end
42
57
  end
43
58
  end
@@ -2,7 +2,7 @@ module Ink
2
2
  module Version # :nodoc: all
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 2
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -1,20 +1,50 @@
1
1
  require "test_helper"
2
2
 
3
3
  class Ink::FormatterTest < Test::Unit::TestCase
4
- def test_markdown
4
+ def test_markdown_rendering
5
5
  assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("Hello\n=====", "file.markdown")
6
6
  assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("Hello\n=====", "file.mkdn")
7
7
  end
8
8
 
9
- def test_textile
9
+ def test_markdown_rendering_with_block
10
+ Ink::Formatter.format("# Hello", "file.markdown") do |type, html|
11
+ assert_equal :markdown, type
12
+ assert_match %r{<h1>.*?</h1>}, html
13
+ end
14
+ end
15
+
16
+ def test_textile_rendering
10
17
  assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("h1. Hello", "file.textile")
11
18
  end
12
19
 
13
- def test_highlight
14
- assert_match %r{<div class="code rb">(.*?)</div>}, Ink::Formatter.format("puts 'Hello world!'", "file.rb")
20
+ def test_textile_rendering_with_block
21
+ Ink::Formatter.format("h1. Hello", "file.textile") do |type, html|
22
+ assert_equal :textile, type
23
+ assert_match %r{<h1>.*?</h1>}, html
24
+ end
15
25
  end
16
26
 
17
- def test_rdoc
27
+ def test_rdoc_rendering
18
28
  assert_match %r{<h1>.*?</h1>}, Ink::Formatter.format("= Hello", "file.rdoc")
19
29
  end
20
- end
30
+
31
+ def test_rdoc_rendering_with_block
32
+ Ink::Formatter.format("= Hello", "file.rdoc") do |type, html|
33
+ assert_equal :rdoc, type
34
+ assert_match %r{<h1>.*?</h1>}, html
35
+ end
36
+ end
37
+
38
+ def test_highlight_rendering
39
+ assert_match %r{<pre>(.*?)</pre>}, Ink::Formatter.format("puts 'Hello world!'", "file.rb")
40
+ end
41
+
42
+ def test_highlight_rendering_with_line_numbers
43
+ html = Ink::Formatter.format(%[o = Object.new\ns = String.new], "file.rb", :line_numbers => true)
44
+
45
+ assert_match %r{<td class="line">1</td>}, html
46
+ assert_match %r{<td class="line">2</td>}, html
47
+
48
+ assert_equal 2, html.scan(%r{<td class="code">(.*?)</td>}m).count
49
+ end
50
+ end
@@ -1,24 +1,33 @@
1
1
  require "test_helper"
2
2
 
3
3
  class Ink::HelperTest < Test::Unit::TestCase
4
+ def setup
5
+ @helper = Object.extend(Ink::Helper)
6
+ end
7
+
4
8
  def test_highlight_helper
5
- helper = Object.extend(Ink::Helper)
6
- html = helper.highlight(%[puts "Hello world!"], :language => :ruby)
9
+ html = @helper.highlight(%[puts "Hello world!"], :language => :ruby)
7
10
 
8
- assert_match /<div class="code ruby">(.*?)<\/div>/, html
11
+ assert_match /<pre>(.*?)<\/pre>/, html
9
12
  end
10
13
 
11
- def test_highlight_helper_using_defaults
12
- helper = Object.extend(Ink::Helper)
13
- html = helper.highlight(%[Hello world!])
14
+ def test_highlight_with_line_numbers
15
+ html = @helper.highlight(%[o = Object.new\ns = String.new], :language => :ruby, :line_numbers => true)
14
16
 
15
- assert_match /<div class="code text">(.*?)<\/div>/, html
17
+ assert_match %r{<td class="line">1</td>}, html
18
+ assert_match %r{<td class="line">2</td>}, html
19
+
20
+ assert_equal 2, html.scan(%r{<td class="code">(.*?)</td>}m).count
16
21
  end
17
22
 
18
- def test_highlight_helper_using_guess
19
- helper = Object.extend(Ink::Helper)
20
- html = helper.highlight(%[puts "Hello world!"], :file => "hello.rb")
23
+ def test_format
24
+ html = @helper.format(%[Object.new], "file.rb")
25
+ assert_match %r{<pre>(.*?)</pre>}, html
26
+ end
21
27
 
22
- assert_match /<div class="code rb">(.*?)<\/div>/, html
28
+ def test_format_with_line_numbers
29
+ html = @helper.format(%[Object.new], "file.rb", :line_numbers => true)
30
+ assert_match %r{<td class="line">1</td>}, html
31
+ assert_match %r{<td class="code">(.*?)</td>}, html
23
32
  end
24
33
  end
@@ -130,4 +130,4 @@ class Ink::HighlightTest < Test::Unit::TestCase
130
130
  assert_equal "python", Ink::Highlight.guess_by_filename("file.SConscript")
131
131
  assert_equal "python", Ink::Highlight.guess_by_filename("file.tac")
132
132
  end
133
- end
133
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-12 00:00:00 -03:00
17
+ date: 2010-04-15 00:00:00 -03:00
18
18
  default_executable: ink
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency