Class: Tracks::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/tracks/utils.rb

Constant Summary collapse

%r{message://<[^>]+>}

Class Method Summary collapse

Class Method Details

Converts message:// links to href. This URL scheme is used on Mac OS X to link to a mail message in Mail.app.


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tracks/utils.rb', line 10

def self.auto_link_message(text)
  text.gsub(AUTO_LINK_MESSAGE_RE) do
    href = $&
    left, right = $`, $'
    # detect already linked URLs and URLs in the middle of a tag
    if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
      # do not change string; URL is alreay linked
      href
    else
      content = helpers.(:a, h(href), :href => h(href))
    end
  end
end

.helpersObject (private)


48
49
50
# File 'lib/tracks/utils.rb', line 48

def self.helpers
  ActionController::Base.helpers
end

.render_text(text) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tracks/utils.rb', line 24

def self.render_text(text)
  rendered = auto_link_message(text)
  rendered = textile(rendered)
  rendered = helpers.auto_link(rendered, :link => :urls)

  # add onenote and message protocols
  config = Sanitize::Config.merge(Sanitize::Config::RELAXED,
    :protocols => { 'a' => {'href' => Sanitize::Config::RELAXED[:protocols]['a']['href'] + ['onenote', 'message']}}
    )
  
  rendered = Sanitize.clean(rendered, config)
  return rendered.html_safe
end

.sanitize_filename(filename) ⇒ Object


42
43
44
# File 'lib/tracks/utils.rb', line 42

def self.sanitize_filename(filename)
  filename.gsub(/[^0-9A-z.\-]/, '_')
end

.textile(text) ⇒ Object


38
39
40
# File 'lib/tracks/utils.rb', line 38

def self.textile(text)
  RedCloth.new(text).to_html
end