text_article.rb
1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'noosfero/translatable_content'
# a base class for all text article types.
class TextArticle < Article
  xss_terminate :only => [ :name ], :on => 'validation'
  def self.type_name
    _('Article')
  end
  include Noosfero::TranslatableContent
  def self.icon_name(article = nil)
    if article && !article.parent.nil? && article.parent.kind_of?(Blog)
      Blog.icon_name
    else
      Article.icon_name
    end
  end
  def can_display_versions?
    true
  end
  before_save :set_relative_path
  def set_relative_path
    parsed = Nokogiri::HTML.fragment(self.body.to_s)
    parsed.css('img[src]').each { |i| change_element_path(i, 'src') }
    parsed.css('a[href]').each { |i| change_element_path(i, 'href') }
    self.body = parsed.to_html
  end
  def change_element_path(el, attribute)
    fullpath = /(https?):\/\/(#{environment.default_hostname})(:\d+)?(\/.*)/.match(el[attribute])
    if fullpath
      domain = fullpath[2]
      path = fullpath[4]
      el[attribute] = path if domain == environment.default_hostname
    end
  end
end