Skip to content
Extraits de code Groupes Projets
application_helper.rb 7,39 ko
Newer Older
  • Learn to ignore specific revisions
  • Raphael's avatar
    Raphael a validé
    #   Copyright (c) 2010, Diaspora Inc.  This file is
    
    Raphael's avatar
    Raphael a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    
    Raphael's avatar
    Raphael a validé
    #   the COPYRIGHT file.
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
    module ApplicationHelper
    
      @@youtube_title_cache = Hash.new("no-title")
    
    
      def timeago(time, options = {})
        options[:class] ||= "timeago"
    
        content_tag(:abbr, time.to_s, options.merge(:title => time.iso8601)) if time
    
      def modern_browser?
        false
      end
    
    
    danielvincent's avatar
    danielvincent a validé
      def page_title text=nil
        title = ""
        if text.blank?
          title = "#{current_user.name} | " if current_user
        else
          title = "#{text} | "
        end
        title += "DIASPORA*"
      end
    
    
      def aspects_with_post aspects, post
    
          aspect.post_ids.include?(post.id)
    
      def aspects_without_post aspects, post
    
          aspect.post_ids.include?(post.id)
    
        aspects.each do |aspect|
    
      def aspect_badge aspect
        str = "<span class='aspect_badge single'>"
        str << link_for_aspect(aspect, 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
        str << "</span>"
      end
    
    
      def aspect_links aspects, opts={}
        str = ""
    
        aspects.each do |aspect|
          str << '<li>'
          str << link_for_aspect(aspect, :params => opts, 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
          str << '</li>'
    
        end
        str.html_safe
      end
    
      def aspect_li aspect, opts= {}
        param_string = ""
        if opts.size > 0
          param_string << '?'
    
          opts.each_pair do |k, v|
    
            param_string << "#{k}=#{v}"
          end
        end
    
      <a href='/aspects/#{aspect.id}#{param_string}'>
        #{aspect.name}
      </a>
    </li>".html_safe
      end
    
    
    Raphael's avatar
    Raphael a validé
      def current_aspect?(aspect)
    
        !@aspect.nil? && !@aspect.is_a?(Symbol) && @aspect.id == aspect.id
    
    Raphael's avatar
    Raphael a validé
      def aspect_or_all_path aspect
        if @aspect.is_a? Aspect
          aspect_path @aspect
        else
          aspects_path
        end
      end
    
        object = object.person if object.is_a? User
    
        eval("#{object.class.name.underscore}_path(object, opts)")
    
        object.attributes.keys
    
      def type_partial(post)
        class_name = post.class.name.to_s.underscore
        "#{class_name.pluralize}/#{class_name}"
      end
    
    maxwell's avatar
    maxwell a validé
      def how_long_ago(obj)
    
        if is_mobile_device?
          time_ago_in_words(obj.created_at)
        else
          timeago(obj.created_at)
        end
    
    maxwell's avatar
    maxwell a validé
      end
    
    
      def person_url(person)
        case person.class.to_s
    
          when "User"
            user_path(person)
          when "Person"
            person_path(person)
          else
            I18n.t('application.helper.unknown_person')
    
      def profile_photo(person)
        person_image_link(person, :size => :thumb_large, :to => :photos)
      end
    
      def owner_image_tag(size=nil)
        person_image_tag(current_user.person, size)
    
      end
    
      def owner_image_link
    
        "<img alt=\"#{h(person.name)}\" class=\"avatar\" #{("data-owner_id="+@user_id.to_s) if @user_id} data-person_id=\"#{person.id}\" src=\"#{person.profile.image_url(size)}\" title=\"#{h(person.name)}\">".html_safe
    
      end
    
      def person_link(person)
    
        "<a href='/people/#{person.id}'>
    
    Dorian's avatar
    Dorian a validé
      #{h(person.name)}
    
    </a>".html_safe
    
    maxwell's avatar
    maxwell a validé
      def hard_link(string, path)
    
        link_to string, path, :rel => 'external'
    
    maxwell's avatar
    maxwell a validé
      end
    
    maxwell's avatar
    maxwell a validé
      def person_image_link(person, opts = {})
    
    maxwell's avatar
    maxwell a validé
        if opts[:to] == :photos
    
          link_to person_image_tag(person, opts[:size]), person_photos_path(person)
    
    maxwell's avatar
    maxwell a validé
        else
    
          "<a href='/people/#{person.id}'>
    
      #{person_image_tag(person)}
    </a>".html_safe
    
    maxwell's avatar
    maxwell a validé
        end
    
      def post_yield_tag(post)
        (':' + post.id.to_s).to_sym
      end
    
      def markdownify(message, options = {})
        message = h(message).html_safe
    
    
        if !options.has_key?(:newlines)
          options[:newlines] = true
    
        message = process_links(message)
    
        message = process_youtube(message, options[:youtube_maps])
    
        message = process_vimeo(message, options[:vimeo_maps])
    
        message = process_autolinks(message)
        message = process_emphasis(message)
    
        if options[:newlines]
          message.gsub!(/\n+/, '<br />')
    
        return message
      end
    
      def process_links(message)
        message.gsub!(/\[([^\[]+)\]\(([^ ]+) \&quot;(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\&quot;\)/) do |m|
          escape = "\\"
    
          link = $1
          url = $2
          title = $3
          url.gsub!("_", "\\_")
          url.gsub!("*", "\\*")
          protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
          res    = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\" title=\"#{title}\">#{link}</a>"
    
          res
        end
        message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
          escape = "\\"
    
          link = $1
          url = $2
          url.gsub!("_", "\\_")
          url.gsub!("*", "\\*")
          protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
          res    = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\">#{link}</a>"
    
      def process_youtube(message, youtube_maps)
        regex = /( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_\-]+)(&[^ ]*|)/
        while youtube = message.match(regex)
          video_id = youtube[3]
          if youtube_maps && youtube_maps[video_id]
            title = h(CGI::unescape(youtube_maps[video_id]))
          else
            title = I18n.t 'application.helper.video_title.unknown'
          end
          message.gsub!(youtube[0], '<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="#video">Youtube: ' + title + '</a>')
    
      def process_autolinks(message)
        message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
        message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
          if !$1.nil?
            m
          else
            res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
            res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
            res
    
      def process_emphasis(message)
        message.gsub!("\\**", "-^doublestar^-")
        message.gsub!("\\__", "-^doublescore^-")
        message.gsub!("\\*", "-^star^-")
        message.gsub!("\\_", "-^score^-")
        message.gsub!(/(\*\*\*|___)(.+?)\1/m, '<em><strong>\2</strong></em>')
        message.gsub!(/(\*\*|__)(.+?)\1/m, '<strong>\2</strong>')
        message.gsub!(/(\*|_)(.+?)\1/m, '<em>\2</em>')
        message.gsub!("-^doublestar^-", "**")
        message.gsub!("-^doublescore^-", "__")
        message.gsub!("-^star^-", "*")
        message.gsub!("-^score^-", "_")
        return message
      end
    
      def process_vimeo(message, vimeo_maps)
        regex = /https?:\/\/(?:w{3}\.)?vimeo.com\/(\d{6,})/
        while vimeo = message.match(regex)
          video_id = vimeo[1]
          if vimeo_maps && vimeo_maps[video_id]
    
            title = h(CGI::unescape(vimeo_maps[video_id]))
    
          else
            title = I18n.t 'application.helper.video_title.unknown'
          end
    
          message.gsub!(vimeo[0], '<a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="#video">Vimeo: ' + title + '</a>')
    
        image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
    
      def get_javascript_strings_for(language)
    
        translations = I18n.t('javascripts', :locale => language)
        defaults = I18n.t('javascripts', :locale => DEFAULT_LANGUAGE)
        defaults.update(translations)
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
    end