Module: Slideshow::TextHelper
- Defined in:
- lib/slideshow/helpers/text_helper.rb
Instance Method Summary collapse
-
#code(*args, &blk) ⇒ Object
Usage example: <%= code ‘code/file.rb#section’, :lang => ‘ruby’, :class => ‘small’ %> or <% code :lang => ‘ruby’ do %> code goes here <% end %>.
- #find_content_from(name, part) ⇒ Object
- #find_part_lines_in(content, part) ⇒ Object
- #format_code(code, opts) ⇒ Object
- #help ⇒ Object
- #s9_include(name, opts = {}) ⇒ Object
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 %>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/slideshow/helpers/text_helper.rb', line 43 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/slideshow/helpers/text_helper.rb', line 73 def find_content_from( name, part ) begin content = File.read( 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/slideshow/helpers/text_helper.rb', line 94 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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/slideshow/helpers/text_helper.rb', line 114 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 |
#help ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/slideshow/helpers/text_helper.rb', line 9 def help() puts " Adding HTML for Slide Show help instructions..." text = "*Slide Show Keyboard Controls (Help)*\n\n| Action | Key |\n| Go to next slide | Space Bar, Right Arrow, Down Arrow, Page Down, Click Heading |\n| Go to previous slide | Left Arrow, Up Arrow, Page Up |\n| Toggle between slideshow and outline view (Ø) | T |\n| Show/hide slide controls (Ø « ») | C, Move mouse to bottom right corner |\n| Zoom in, zoom out, zoom reset (100%) | Control[@+@]Plus, Control[@+@]Minus, Control[@+@]@0@ |\n" html = "<!-- begin help -->\n<div class='help projection'>\n\#{textile_to_html( text )}\n</div>\n<!-- end help -->\n" guard_block( html ) end |
#s9_include(name, opts = {}) ⇒ Object
4 5 6 7 |
# File 'lib/slideshow/helpers/text_helper.rb', line 4 def s9_include( name, opts = {} ) puts " Including '#{name}'..." content = File.read( name ) end |