lib/shinmun/helpers.rb in shinmun-0.2 vs lib/shinmun/helpers.rb in shinmun-0.5

- old
+ new

@@ -1,88 +1,9 @@ module Shinmun module Helpers - # Render a hash as attributes for a HTML tag. - def attributes(attributes) - attributes.map { |k, v| %Q{#{k}="#{v}"} }.join(' ') - end - - # Render a HTML tag with given name. - # The last argument specifies the attributes of the tag. - # The second argument may be the content of the tag. - def tag(name, *args) - text, attributes = args.first.is_a?(Hash) ? [nil, args.first] : args - "<#{name} #{attributes(attributes)}>#{text}</#{name}>" - end - - # Render stylesheet link tag - def stylesheet_link_tag(*names) - options = names.last.is_a?(Hash) ? names.pop : {} - options[:media] ||= 'screen' - names.map { |name| - mtime = File.mtime("assets/#{blog.stylesheets_path}/#{name}.css").to_i - path = "/#{blog.stylesheets_path}/#{name}.css?#{mtime}" - tag :link, :href => path, :rel => 'stylesheet', :media => options[:media] - }.join("\n") - end - - # Render javascript tag - def javascript_tag(*names) - names.map { |name| - mtime = File.mtime("assets/#{blog.javascripts_path}/#{name}.js").to_i - path = "/#{blog.javascripts_path}/#{name}.js?#{mtime}" - tag :script, :src => path, :type => 'text/javascript' - }.join("\n") - end - - # Render an image tag - def image_tag(file, options = {}) - mtime = File.mtime("assets/#{blog.images_path}/#{file}").to_i - path = "/#{blog.images_path}/#{file}?#{mtime}" - tag :img, options.merge(:src => path) - end - - # Render a link - def link_to(text, path, options = {}) - tag :a, text, options.merge(:href => path) - end - - # Render a link to a post - def post_link(post) - link_to post.title, "#{blog.base_path}/#{post.path}.html" - end - - # Render a link to an archive page. - def archive_link(year, month) - link_to "#{Date::MONTHNAMES[month]} #{year}", "#{blog.base_path}/#{year}/#{month}/index.html" - end - - # Render a date or time in a nice human readable format. - def date(time) - "%s %d, %d" % [Date::MONTHNAMES[time.month], time.day, time.year] - end - - # Render a date or time in rfc822 format. This will be used for rss rendering. - def rfc822(time) - time.strftime("%a, %d %b %Y %H:%M:%S %z") - end - - def markdown(text, *args) - BlueCloth.new(text, *args).to_html - rescue => e - "#{text}<br/><br/><strong style='color:red'>#{e.message}</strong>" - end - - def strip_tags(str) - str.gsub(/<\/?[^>]*>/, "") - end - - def urlify(string) - string.downcase.gsub(/[ -]+/, '-').gsub(/[^-a-z0-9_]+/, '') - end - # taken form ActionView::Helpers def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) distance_in_minutes = (((to_time - from_time).abs)/60).round @@ -109,8 +30,53 @@ when 86400..525599 then "#{(distance_in_minutes / 43200).round} months" when 525600..1051199 then 'about 1 year' else "over #{(distance_in_minutes / 525600).round} years" end end + + def post_path(post) + "#{base_path}/#{post.year}/#{post.month}/#{post.name}" + end + + def archive_path(year, month) + "#{base_path}/#{year}/#{month}" + end + + # Render a link to a post + def post_link(post) + link_to post.title, post_path(post) + end + + # Render a link to an archive page. + def archive_link(year, month) + link_to "#{Date::MONTHNAMES[month]} #{year}", archive_path(year, month) + end + + # Render a date or time in a nice human readable format. + def human_date(time) + "%s %d, %d" % [Date::MONTHNAMES[time.month], time.day, time.year] + end + + # Render a date or time in rfc822 format. + def rfc822(time) + time.strftime("%a, %d %b %Y %H:%M:%S %z") + end + + # Render a link for the navigation bar. If the text of the link + # matches the @header variable, the css class will be set to acitve. + def navi_link(text, path) + link_to text, path, :class => (request.path_info == path) ? 'active' : nil + end + + def html_escape(s) + s.to_s.gsub(/>/, '&gt;').gsub(/</n, '&lt;') + end + + def diff_line_class(line) + case line[0, 1] + when '+' then 'added' + when '-' then 'deleted' + end + end + end - end