]> git.openstreetmap.org Git - rails.git/commitdiff
Fixed wikipedia link reference to specific sections
authorJoão G. Packer <jgpacker@users.noreply.github.com>
Fri, 28 Feb 2014 01:30:30 +0000 (22:30 -0300)
committerTom Hughes <tom@compton.nu>
Sat, 1 Mar 2014 00:26:37 +0000 (00:26 +0000)
When creating  a wikipedia link from a tag, the function
is (correctly) appending "?userlang=#{I18N.lang}" to the URL,
but doing this breaks the reference to a specific section
of a wikipedia article (if there is any).

For example, if the tag is "wikipedia=Article#Section", the function
would create a link to "../Article#Section?uselang=xx", and then the
browser wouldn't be able to correctly find the section. The correct
link result should be "../Article?uselang=xx#Section".

This commit fixes this by verifying if there is a reference to a
specific section of the article, and then putting "?uselang=#{I18N.lang}"
between the article's name and the section name.

app/helpers/browse_helper.rb

index cf13c27958943a6438b0fdcf99a947416667709a..70f724cca5bd63abe1e6fab4546ddd830108f8f8 100644 (file)
@@ -114,16 +114,25 @@ private
         lang = 'en'
       end
     elsif key =~ /^wikipedia:(\S+)$/
-      # Language is in the key, so assume value is a simple title
+      # Language is in the key, so assume value is the title
       lang = $1
     else
       # Not a wikipedia key!
       return nil
     end
 
+    if value =~ /^([^#]*)(#.*)/ then
+      # Contains a reference to a section of the wikipedia article
+      # Must break it up to correctly build the url
+      value = $1
+      section = $2
+    else
+      section = ""
+    end
+
     return {
-      :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}",
-      :title => value
+      :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{section}",
+      :title => value + section
     }
   end
 end