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

Creates <a> tags for all urls. IMPORTANT: make sure you’ve used #urls_to_images method first if you wanted all images urls to become <img> tags.



294
295
296
297
298
299
300
# File 'lib/rad/lib/text_utils.rb', line 294

def urls_to_links html
  # becouse it finds only one url in such string "http://some_domain.com http://some_domain.com" we need to aply it twice
  regexp, sub = /(\s|^|\A|\n|\t|\r)(http:\/\/.*?)([,.])?(\s|$|\n|\Z|\t|\r|<)/, '\1<a href="\2">\2</a>\3\4'
  html = html.gsub regexp, sub
  html.gsub regexp, sub
  html
end