require "digest/md5" module Ginatra # Helpers used in the views usually, # but not exclusively. module Helpers # takes an email and returns a url to a secure gravatar # # @param [String] email the email address # @return [String] the url to the gravatar def gravatar_url(email) "https://secure.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=40" 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 # displays the actor box easily. # # Internally, it calls the +#partial+ method # @see Sinatra::Partials#partial # # @param [Grit::Actor] actor the Grit Actor Object # @param [#to_s] role the role that the object has. # @param [#strftime] date the date that the actor acted on. # # @return [String] a string that contains the box for the actor. def actor_box(actor, role, date) partial(:actor_box, :locals => { :actor => actor, :role => role, :date => date }) end # works out what actor boxes need to be displayed. # # Will always display the committer box, and will only display # the author box if it's different to the committer # # @param [Grit::Commit] commit the commit in question # @return [String] a string representing the HTML actor boxes def actor_boxes(commit) o = actor_box(commit.committer, :committer, commit.committed_date) if commit.author.name != commit.committer.name o = actor_box(commit.author, :author, commit.authored_date) + o end end # spits out a link to a certain reference. # # @param [Grit::Ref] ref grit ref object # @param [String] repo_param the url-sanitised-name for the repo # (for the link path) # # @return [String] HTML link to the given ref with class attached. def commit_ref(ref, repo_param) ref_class = ref.class.to_s.split("::")[1].to_s "#{ref.name}" end # calls +Ginatra::Helpers#commit_ref+ for each ref in the commit # # @see Ginatra::Helpers#commit_ref # # @param [Grit::Commit] commit grit commit object # @param [String] repo_param the url-sanitised-name for the repo # (for the link path) # # @return [String] HTML containing all the ref links def commit_refs(commit, repo_param) commit.refs.map{ |r| commit_ref(r, repo_param) }.join("\n") end # returns a string including the link to download a certain # tree of the repo # # @param [Grit::Tree] tree the tree you want an archive link for # @param [String] repo_param the url-sanitised-name of the repo # (for the link path) # # @return [String] the HTML link to the archive. def archive_link(tree, repo_param) "Download Archive" end # returns a string including the link to download a patch for a certain # commit to the repo # # @param [Grit::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) "Download Patch" end # returns a HTML (+