Module: Ink::Helper

Included in:
Formatter
Defined in:
lib/ink/helper.rb

Overview

The Ink::Helper module is great for shortcuts. On Rails applications, can be added to ApplicationHelper.

module ApplicationHelper
  include InkHelper
end

Instance Method Summary collapse

Instance Method Details

#format(code, file, options = {}, &block) ⇒ Object

Return a code snippet formatted by using the Ink::Formatter.format method.

<%= format @code, "Rakefile" %>
<%= format @code, "Rakefile", :line_numbers => true %>


16
17
18
# File 'lib/ink/helper.rb', line 16

def format(code, file, options = {}, &block)
  Ink::Formatter.format(code, file, options, &block)
end

#highlight(code, options = {}) ⇒ Object

Return a code snippet highlighted in the specified language. When a :file option is provided, then language will be guessed by the filename. If no language is specified will set to :text.

<%= highlight @code, :file => "Rakefile" %>
<%= highlight @code, :language => :ruby %>


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ink/helper.rb', line 28

def highlight(code, options = {})
  file = Tempfile.new("highlight")
  File.open(file.path, "w+") do |f|
    f << "#{code}\n"
  end

  if options[:file]
    language = Highlight.guess_by_filename(options[:file])
  else
    language = options[:language] || :text
  end

  code = Ink::Highlight.highlight(file.path, :language => language)

  if options[:line_numbers]
    io = StringIO.new(code)

    rows = ""

    io.lines.each_with_index do |line, i|
      rows << %[<tr><td class="line">#{i+1}</td>]
      rows << %[<td class="code">#{line}</td></tr>]
    end

    %[<table class="highlight">#{rows}</table>]
  else
    %[<pre>#{code}</pre>]
  end
end