Module: TextUtils::StringParser

Defined in:
lib/rad/lib/text_utils.rb

Overview

Class Method Summary collapse

Class Method Details

.highlight_code(options = {}) ⇒ Object

Highlights code using 'uv' library. Make sure you have ultraviolet gem installed.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/rad/lib/text_utils.rb', line 304

def highlight_code(options={})
  require 'uv'

  wrap_with = options[:wrap_with] || ['','']
  text = @modified_string

  languages_syntax_list = File.readlines(
    File.expand_path(File.dirname(__FILE__) + '/../config/languages_syntax_list')
  ).map { |l| l.chomp }

  text.gsub!(/<code(\s*?lang=["']?(.*?)["']?)?>(.*?)<\/code>/) do
    if languages_syntax_list.include?($2)
      lang = $2
    else
      lang = 'ruby'
    end
    unless $3.blank?
      result = Uv.parse($3.gsub('<br/>', "\n").gsub('&lt;', '<').gsub('&gt;', '>').gsub('&quot;', '"'), 'xhtml', lang, false, 'active4d')
      "#{wrap_with[0].gsub('$lang', lang)}#{result}#{wrap_with[1]}"
    end
  end

  # TODO: split string longer than 80 characters

  @modified_string = text
  self

end