module HomesteadingPublisher module ApplicationHelper def shorturl_link_tag(post) if index_action? url = "http://#{setting :short_domain}/#{setting(:post_short_code)}" elsif show_action? url = short_url(post) else url = "http://#{setting :short_domain}" end tag(:link, id: "shortlink", rel: "shortlink", type: "text/html", href: url) end def short_url(post) pieces = [setting(:post_short_code)] pieces << Date.parse("#{post.year}-#{post.month}-#{post.day}").to_sxg pieces << nth_of_day(post) "http://#{setting :short_domain}/#{pieces.join}" end def nth_of_day(post) nth = Post.where( year: post.year, month: post.month, day: post.day ).all.sort_by{|n| n.published_at}.index(post) + 1 if nth.nil? nth = 1 end nth.to_sxg end def canonical_url(post=nil) if post.nil? "http://#{setting :long_domain}" else "http://#{setting :long_domain}/#{post.path}" end end def rel_canonical_link_tag(post) url = "http://#{setting :long_domain}" if index_action? url = "http://#{setting :long_domain}/#{setting(:post_type).pluralize.downcase}" elsif show_action? url = canonical_url(post) end tag(:link, id: "canonical", rel: "canonical", type: "text/html", href: url) end def page_description(post=nil) if index_action? page_description = "#{setting(:post_type).pluralize.capitalize} by #{setting :author_name}" elsif show_action? page_description = post.content else page_description = setting(:site_description) end page_description end def space " " end def index_action? action_name == "index" end def show_action? action_name == "show" end def write_action? unless controller_name == "settings" action_name =~ /new|edit/ end end def editing? action_name == "edit" end def human_readable_date(datetime) datetime.strftime("%F") end def human_readable_time(datetime) datetime.strftime("%l:%M%p").downcase end def small_word_tag(word) content_tag(:b, word, class: "small-word small-word-#{word}") end def logged_in? Rails.env.development? end def autofocus_value !editing? end def post_type setting(:post_type) end def site_title title = "" if setting(:site_title).blank? title << post_type.capitalize.pluralize else title << setting(:site_title) end if @page_title title << " - #{@page_title}" end title.html_safe end def setting(key) ::Setting.where(key: key).first.content end def in_reply_to_urls(post) html = [] post.in_reply_to.split(" ").each do |in_reply_to| text = in_reply_to.gsub(/^https?:\/\//, "").gsub(/\//, "/").html_safe url = in_reply_to unless url.match(/^http/) url = "http://#{url}" end rel = show_action? ? "in-reply-to external" : "external" html << link_to(text, url, class: "u-in-reply-to h-cite", rel: rel).html_safe end html.join(" ").html_safe end def link_to_syndication(post) html = content_tag(:p, "Also posted to") links = [] external_silos.each do |external| if post.respond_to?(external_silo_url_key(external)) && post.send(external_silo_url_key(external)) && !post.send(external_silo_url_key(external)).blank? rel = "external " rel << "syndication" if show_action? links << content_tag(:li, link_to(external, post.send(external_silo_url_key(external)), rel: rel, class: "u-syndication")) end end if links.blank? space else html << content_tag(:ul, links.flatten.join.html_safe) end end def external_silos %w[ Facebook Foursquare Instagram Medium Tumblr Twitter Vimeo Vine WordPress YouTube ] end def external_silo_url_key(name) "#{name.downcase}_url".to_sym end def is_a_reply?(post) !post.in_reply_to.blank? end # TODO: do this without #eval ? def site_url eval "#{setting(:post_type).pluralize}_url" end def tag_url(tag) site_url + "/tags/" + tag.gsub(/ /, "+") end def license_html output = [] # license license = ::License.find(setting(:license)) if license.name == "All Rights Reserved" # Default output << "#{license.name} #{license.short_code}" else # Creative Commons and Public Domain (CC0) output << link_to("#{license.name} (#{license.short_code})", license.url, rel: "license") end # range of years years_range = Time.now.year first_post = Post.first first_post_year = first_post.nil? ? years_range : first_post.published_at.year if first_post_year == years_range output << years_range else output << "#{first_post_year}–#{years_range}" end # author's name output << link_to(setting(:author_name), setting(:author_url), class: "p-author h-card") output.join(" ").html_safe end def license_text output = [] # license license = ::License.find(setting(:license)) if license.name == "All Rights Reserved" # Default output << "#{license.name} #{license.short_code}" else # Creative Commons and Public Domain (CC0) output << "#{license.name} (#{license.short_code})" output << license.url end # range of years years_range = Time.now.year first_post = Post.first first_post_year = first_post.nil? ? years_range : first_post.published_at.year if first_post_year == years_range output << years_range else output << "#{first_post_year}–#{years_range}" end # author's name output << setting(:author_name) output.join("\n") end end end