Module: Slideshow::TextHelper

Defined in:
lib/slideshow/helpers/text_helper.rb

Instance Method Summary collapse

Instance Method Details

#code(*args, &blk) ⇒ Object

Usage example:

<%= code 'code/file.rb#section', :lang => 'ruby', :class => 'small' %>
  or
<% code :lang => 'ruby' do %>
  code goes here
<% end %>


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/slideshow/helpers/text_helper.rb', line 20

def code( *args, &blk )
  # check for optional hash for options
  opts = args.last.kind_of?(Hash) ? args.pop : {}
  
  name_plus_part = args.first   # check for optional filename
    
  if name_plus_part 
    name, part = name_plus_part.split( '#' ) # split off optional part/anchor 
    
    content = find_content_from( name, part ) 
    
    # add name and part to opts so engine can use paras too  
    opts[ :name ] = name  
    opts[ :part ] = part if part
    
    return format_code( content, opts )
  elsif blk # inline code via block?
    content = capture_erb(&blk)
    return if content.empty?

    concat_erb( format_code( content, opts ), blk.binding )
    return
  else
    msg = '*** warning: empty code directive; inline code or file para expected'
    puts msg 
    return wrap_markup( "<!-- #{msg} -->" )
  end
end

#find_content_from(name, part) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slideshow/helpers/text_helper.rb', line 50

def find_content_from( name, part )
  begin
    content = File.read_utf8( name )

    # note: for now content with no parts selected gets filtered too and (part) marker lines get removed from source
    lines = find_part_lines_in( content, part )

    if part
      puts "  Including code part '#{part}' in '#{name}' [#{lines.size} lines]..."
      ## todo: issue warning if no lines found?
    else
      puts "  Including code in '#{name}' [#{lines.size} lines]..."
    end
  
    return lines.join
  rescue Exception => e
    puts "*** error: reading '#{name}': #{e}"
    exit 2
  end
end

#find_part_lines_in(content, part) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/slideshow/helpers/text_helper.rb', line 71

def find_part_lines_in( content, part )
  result = []
  state = part.nil? ? :normal : :skipping 
  content.each_line do |line|
    if line =~ /(START|END):(\w+)/
      if $2 == part
        if $1 == "START"
          state = :normal
        else
          state = :skipping
        end
      end
      next 
    end
    result << line unless state == :skipping
  end
  result
end

#format_code(code, opts) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slideshow/helpers/text_helper.rb', line 91

def format_code( code, opts )
  
  engine    = opts.fetch( :engine, headers.code_engine ).to_s.downcase  
  
  if engine == 'uv' || engine == 'ultraviolet'
    if respond_to?( :uv_worker )
      code_highlighted = uv_worker( code, opts )
    else
      puts "** error: ultraviolet gem required for syntax highlighting; install with gem install ultraviolet (or use a different engine)"
      exit 2
    end
  elsif engine == 'cr' || engine == 'coderay'
    if respond_to?( :coderay_worker )
      code_highlighted = coderay_worker( code, opts )
    else
      puts "*** error: coderay gem required for syntax highlighting; install with gem install coderay (or use a different engine)"
      exit 2
    end
  else
    method_name = "#{engine}_worker".to_sym
    if respond_to?( method_name )
      # try to call custom syntax highlighting engine
      code_highlighted = send( method_name, code, opts )
    else
      msg = "*** error: unkown syntax highlighting engine '#{engine}'; available built-in options include: uv, ultraviolet, cr, coderay"
      puts msg
      code_highlighted = "<!-- #{msg} -->"
    end
  end
 
  out =  %{<!-- begin code#{opts.inspect} -->\n}
  out << code_highlighted
  out << %{<!-- end code -->\n}
  
  guard_block( out ) # saveguard with notextile wrapper etc./no further processing needed
end

#s9_include(name, opts = {}) ⇒ Object



6
7
8
9
# File 'lib/slideshow/helpers/text_helper.rb', line 6

def s9_include( name, opts = {} )
  puts "  Including '#{name}'..." 
  content = File.read_utf8( name )
end