require 'digest/md5' module Ginatra # Helpers used in the views, and not only. module Helpers # Escapes string to HTML entities def h(text) Rack::Utils.escape_html(text) end # Checks X-PJAX header def is_pjax? request.env['HTTP_X_PJAX'] end # Sets title for pages def title(*args) @title ||= [] @title_options ||= { headline: nil, sitename: h(Ginatra.config.sitename) } options = args.last.is_a?(Hash) ? args.pop : {} @title += args @title_options.merge!(options) t = @title.clone t << @title_options[:headline] t << @title_options[:sitename] t.compact.join ' - ' end # Constructs the URL used in the layout's base tag def prefix_url(rest_of_url='') prefix = Ginatra.config.prefix.to_s if prefix.length > 0 && prefix[-1].chr == '/' prefix.chop! end "#{prefix}/#{rest_of_url}" end # Returns hint to set repository description def empty_description_hint_for(repo) return '' unless repo.description.empty? hint_text = "Edit `#{repo.path}description` file to set the repository description." "hint" end # Returns file icon depending on filemode def file_icon(filemode) case filemode # symbolic link (120000) when 40960 then "symbolic link" # executable file (100755) when 33261 then "executable file" else "file" end end # Masks original email def secure_mail(email) local, domain = email.split('@') "#{local[0..3]}...@#{domain}" end # Takes an email and returns an image tag with gravatar # # @param [String] email the email address # @param [Hash] options alt, class and size options for image tag # @return [String] html image tag def gravatar_image_tag(email, options={}) alt = options.fetch(:alt, email.gsub(/@\S*/, '')) size = options.fetch(:size, 40) url = "https://secure.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}" if options[:lazy] placeholder = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' tag = "#{h alt}" else tag = "#{h alt}" end tag end # Reformats the date into a user friendly date with html entities # # @param [#strftime] date object to format nicely # @return [String] html string formatted using # +"%b %d, %Y – %H:%M"+ def nicetime(date) date.strftime("%b %d, %Y – %H:%M") end # Returns an html time tag for the given time # # @param [Time] time object # @return [String] time tag formatted using # +"%B %d, %Y %H:%M"+ def time_tag(time) datetime = time.strftime('%Y-%m-%dT%H:%M:%S%z') title = time.strftime('%Y-%m-%d %H:%M:%S') "" end # Returns a string including the link to download a patch for a certain # commit to the repo # # @param [Rugged::Commit] commit the commit you want a patch for # @param [String] repo_param the url-sanitised name for the repo # (for the link path) # # @return [String] the HTML link to the patch def patch_link(commit, repo_param) patch_url = prefix_url("#{repo_param}/commit/#{commit.oid}.patch") "Download Patch" end # Spits out a HTML link to the atom feed for a given ref of a given repo # # @param [Sting] repo_param the url-sanitised-name of a given repo # @param [String] ref the ref to link to. # # @return [String] the HTML containing the link to the feed. def atom_feed_url(repo_param, ref=nil) ref.nil? ? prefix_url("#{repo_param}.atom") : prefix_url("#{repo_param}/#{ref}.atom") end # Returns a HTML (+