module Slideshow::TextHelper

Public Instance Methods

code( *args, &blk ) click to toggle source

Usage example:

<%= code 'code/file.rb#section', :lang => 'ruby', :class => 'small' %>
  or
<% code :lang => 'ruby' do %>
  code goes here
<% end %>
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 = {} ) click to toggle source
# File lib/slideshow/helpers/text_helper.rb, line 6
def s9_include( name, opts = {} )
  puts "  Including '#{name}'..." 
  content = File.read_utf8( name )
end