diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 64c8c27e4..59f49f11a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -24,21 +24,24 @@ module ApplicationHelper post.aspect_ids.include?(a.id) end end + def aspects_without_post aspects, post aspects.reject do |a| post.aspect_ids.include?(a.id) end end + def aspect_badge aspects str = '' if aspects.count > 1 str = "#{I18n.t('application.helper.aspect_badge.all_aspects')}" elsif aspects.count == 1 aspect = aspects.first - str = "#{aspect.name}" + str = "#{aspect.name}" end str.html_safe end + def aspect_links aspects, opts={} str = "" aspects.each do |a| @@ -46,15 +49,16 @@ module ApplicationHelper end str.html_safe end + def aspect_li aspect, opts= {} param_string = "" if opts.size > 0 param_string << '?' - opts.each_pair do |k,v| + opts.each_pair do |k, v| param_string << "#{k}=#{v}" end end -"
  • + "
  • #{aspect.name} @@ -98,12 +102,12 @@ module ApplicationHelper 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') + when "User" + user_path(person) + when "Person" + person_path(person) + else + I18n.t('application.helper.unknown_person') end end @@ -124,28 +128,28 @@ module ApplicationHelper end def person_link(person) -" + " #{person.name} ".html_safe end def image_or_default(person, size=:thumb_large) image_location = person.profile.image_url(size) if person.profile - image_location ||= person.profile.image_url(:thumb_large) if person.profile #backwards compatability for old profile pictures + image_location ||= person.profile.image_url(:thumb_large) if person.profile #backwards compatability for old profile pictures image_location ||= "/images/user/default.png" image_location end - + def hard_link(string, path) - link_to string, path, :rel => 'external' + link_to string, path, :rel => 'external' end def person_image_link(person, opts = {}) return "" if person.nil? if opts[:to] == :photos - link_to person_image_tag(person,opts[:size]), person_photos_path(person) + link_to person_image_tag(person, opts[:size]), person_photos_path(person) else -" + " #{person_image_tag(person)} ".html_safe end @@ -158,78 +162,22 @@ module ApplicationHelper def person_photos_path person person_id = person.id if person.respond_to?(:id) person_id ||= person - + "#{photos_path}?person_id=#{person_id}" end def markdownify(message, options = {}) message = h(message).html_safe - [:autolinks, :youtube, :emphasis, :links, :newlines].each do |k| - if !options.has_key?(k) - options[k] = true - end + if !options.has_key?(:newlines) + options[:newlines] = true end - if options[:links] - message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m| - escape = (options[:emphasis]) ? "\\" : "" - res = "#{$1}" - res - end - message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m| - escape = (options[:emphasis]) ? "\\" : "" - res = "#{$1}" - res - end - end - - if options[:youtube] - message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m| - res = "#{$1}youtube.com::#{$3}" - res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis] - res - end - end - - if options[:autolinks] - message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2') - message.gsub!(/(#{$3}} - res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis] - res - end - end - end - - if options[:emphasis] - message.gsub!("\\**", "-^doublestar^-") - message.gsub!("\\__", "-^doublescore^-") - message.gsub!("\\*", "-^star^-") - message.gsub!("\\_", "-^score^-") - message.gsub!(/(\*\*\*|___)(.+?)\1/m, '\2') - message.gsub!(/(\*\*|__)(.+?)\1/m, '\2') - message.gsub!(/(\*|_)(.+?)\1/m, '\2') - message.gsub!("-^doublestar^-", "**") - message.gsub!("-^doublescore^-", "__") - message.gsub!("-^star^-", "*") - message.gsub!("-^score^-", "_") - end - - if options[:youtube] - while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/) - video_id = youtube[1] - if options[:youtube_maps] && options[:youtube_maps][video_id] - title = options[:youtube_maps][video_id] - else - title = I18n.t 'application.helper.youtube_title.unknown' - end - message.gsub!('youtube.com::'+video_id, 'Youtube: ' + title + '') - end - end + message = process_links(message) + message = process_youtube(message) + message = process_autolinks(message) + message = process_emphasis(message) + message = process_youtube_again(message, options[:youtube_maps]) if options[:newlines] message.gsub!(/\n+/, '
    ') @@ -238,6 +186,72 @@ module ApplicationHelper return message end + def process_links(message) + message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m| + escape = "\\" + res = "#{$1}" + res + end + message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m| + escape = "\\" + res = "#{$1}" + res + end + + return message + end + + def process_youtube(message) + message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m| + res = "#{$1}youtube.com::#{$3}" + res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } + res + end + return message + end + + def process_autolinks(message) + message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2') + message.gsub!(/(#{$3}} + res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } + res + end + end + return message + end + + def process_emphasis(message) + message.gsub!("\\**", "-^doublestar^-") + message.gsub!("\\__", "-^doublescore^-") + message.gsub!("\\*", "-^star^-") + message.gsub!("\\_", "-^score^-") + message.gsub!(/(\*\*\*|___)(.+?)\1/m, '\2') + message.gsub!(/(\*\*|__)(.+?)\1/m, '\2') + message.gsub!(/(\*|_)(.+?)\1/m, '\2') + message.gsub!("-^doublestar^-", "**") + message.gsub!("-^doublescore^-", "__") + message.gsub!("-^star^-", "*") + message.gsub!("-^score^-", "_") + return message + end + + def process_youtube_again(message, youtube_maps) + while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/) + video_id = youtube[1] + if youtube_maps && youtube_maps[video_id] + title = youtube_maps[video_id] + else + title = I18n.t 'application.helper.youtube_title.unknown' + end + message.gsub!('youtube.com::'+video_id, 'Youtube: ' + title + '') + end + return message + end + def info_text(text) image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 17fb722b9..936264916 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -72,24 +72,22 @@ describe ApplicationHelper do markdownify(proto+"://"+url).should == ""+url+"" end - it "recognizes youtube links" do - proto="http" - videoid = "0x__dDWdf23" - url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" - title = "UP & down & UP & down &" - res = markdownify(proto+'://'+url) - res.should =~ /data-host="youtube.com"/ - res.should =~ /data-video-id="#{videoid}"/ - end - - it "recognizes youtube links with hyphens" do - proto="http" - videoid = "ABYnqp-bxvg" - url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" - title = "UP & down & UP & down &" - res = markdownify(proto+'://'+url) - res.should =~ /data-host="youtube.com"/ - res.should =~ /data-video-id="#{videoid}"/ + describe "video links" do + it "recognizes youtube links" do + video_id = "0x__dDWdf23" + url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" + res = markdownify(url) + res.should =~ /data-host="youtube.com"/ + res.should =~ /data-video-id="#{video_id}"/ + end + + it "recognizes youtube links with hyphens" do + video_id = "ABYnqp-bxvg" + url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" + res = markdownify(url) + res.should =~ /data-host="youtube.com"/ + res.should =~ /data-video-id="#{video_id}"/ + end end it "recognizes multiple links of different types" do @@ -174,35 +172,13 @@ describe ApplicationHelper do markdownify(message).should == "some text *some text **some text some text _some text __some text" end - describe "options" do - before do - @message = "http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ [link](www.url.com) [link](url.com \"title\")" - end - - it "can render only autolinks" do - res = markdownify(@message, :youtube => false, :emphasis => false, :links => false) - res.should == "url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ [link](www.url.com) [link](url.com "title")" - end - - it "can render only youtube" do - res = markdownify(@message, :autolinks => false, :emphasis => false, :links => false) - res.should_not =~ /a href="http:\/\/url.com"/ - res.should_not =~ /a href="http:\/\/www.url.com"/ - res.should_not =~ /emphasis<\/strong>/ - end - - it "can render only emphasis tags" do - res = markdownify(@message, :autolinks => false, :youtube => false, :links => false) - res.should == "http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related emphasis emphasis [link](www.url.com) [link](url.com "title")" - end - - it "can render only links tags" do - res = markdownify(@message, :autolinks => false, :youtube => false, :emphasis => false) - res.should == "http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ link link" - end - end - describe "newlines" do + it 'skips inserting newlines if you pass the newlines option' do + message = "These\nare\n\some\nnew\lines" + res = markdownify(message, :newlines => false) + res.should == message + end + it 'generates breaklines' do message = "These\nare\nsome\nnew\nlines" res = markdownify(message)