diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb index f55ac0d..f142afa 100644 --- a/app/helpers/tags_helper.rb +++ b/app/helpers/tags_helper.rb @@ -20,12 +20,12 @@ module TagsHelper # # options can include one or more of the following: # - # * :max_size: font size for the tag with largest count - # * :min_size: font size for the tag with smallest count + # * :max_size: font size for the tag with largest count + # * :min_size: font size for the tag with smallest count # * :show_count: whether to show the count of contents for each tag. Defauls to false. - # + # # The algorithm for generating the different sizes and positions is a - # courtesy of Aurelio: http://www.colivre.coop.br/Aurium/Nuvem + # courtesy of Aurelio: http://www.colivre.coop.br/Aurium/Nuvem # (pt_BR only). def tag_cloud(tags, tagname_option, url, options = {}) @@ -41,7 +41,15 @@ module TagsHelper max = tags.values.max.to_f min = tags.values.min.to_f - tags.sort_by{ |k,v| k.downcase }.map do |tag,count| + # Uses iconv to translate utf8 strings to ascii. + # If can't translate a character, ignore it. + require 'iconv' + utf8_to_ascii = Iconv.new("ASCII//TRANSLIT//IGNORE","UTF8") + # Sorts first based on translated strings and then, if they are equal, based on the original form. + # This way variant characters falls on the same level as their base characters and don't end up + # at the end of the tag list. + # Example: AA ÁA AB Z instead of AA AB Z ÁA + tags.collect{ |k,v| [utf8_to_ascii.iconv(k).downcase, [k,v]] }.sort.collect { |ascii, t| t }.map do |tag,count| if ( max == min ) v = 0.5 else diff --git a/test/unit/tags_helper_test.rb b/test/unit/tags_helper_test.rb index da5f0ae..e6aa1d7 100644 --- a/test/unit/tags_helper_test.rb +++ b/test/unit/tags_helper_test.rb @@ -18,4 +18,14 @@ class TagsHelperTest < Test::Unit::TestCase assert_equal %w(aTag beTag tag1 Tag2 Tag3).join("\n"), result end + should 'order tags alphabetically with special characters' do + result = tag_cloud( + { 'aula'=>9, 'área'=>2, 'area'=>2, 'avião'=>2, 'armário'=>2, + 'A'=>1, 'Á'=>1, 'AB'=>1, 'ÁA'=>1 }, + :id, + { :host=>'noosfero.org', :controller=>'test', :action=>'tag' } + ) + assert_equal %w(A Á ÁA AB area área armário aula avião).join("\n"), result + end + end -- libgit2 0.21.2