diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/i18n_fixes.rb | 41 | ||||
-rw-r--r-- | lib/mail_handler/backends/mail_backend.rb | 3 | ||||
-rw-r--r-- | lib/mail_handler/backends/tmail_backend.rb | 10 | ||||
-rw-r--r-- | lib/make_html_4_compliant.rb | 3 | ||||
-rw-r--r-- | lib/tasks/gettext.rake | 15 | ||||
-rw-r--r-- | lib/tasks/rspec.rake | 5 | ||||
-rw-r--r-- | lib/tasks/translation.rake | 6 | ||||
-rw-r--r-- | lib/timezone_fixes.rb | 4 | ||||
-rw-r--r-- | lib/use_spans_for_errors.rb | 2 |
9 files changed, 47 insertions, 42 deletions
diff --git a/lib/i18n_fixes.rb b/lib/i18n_fixes.rb index 6e684d44a..a85faddcb 100644 --- a/lib/i18n_fixes.rb +++ b/lib/i18n_fixes.rb @@ -5,39 +5,36 @@ # override behaviour in fast_gettext/translation.rb # so that we can interpolate our translation strings nicely +# TODO: We could simplify a lot of this code (as in remove it) if we moved from using the {{value}} +# convention in the translation strings for interpolation to %{value}. This is apparently the newer +# convention. + def _(key, options = {}) - translation = FastGettext._(key) || key + translation = (FastGettext._(key) || key).html_safe gettext_interpolate(translation, options) end -INTERPOLATION_RESERVED_KEYS = %w(scope default) -MATCH = /(\\\\)?\{\{([^\}]+)\}\}/ +MATCH = /\{\{([^\}]+)\}\}/ def gettext_interpolate(string, values) return string unless string.is_a?(String) - if values.is_a?(Hash) - string.gsub(MATCH) do - escaped, pattern, key = $1, $2, $2.to_sym - - if escaped - pattern - elsif INTERPOLATION_RESERVED_KEYS.include?(pattern) - raise I18n::ReservedInterpolationKey.new(pattern, string) - elsif !values.include?(key) - raise I18n::MissingInterpolationArgument.new(pattern, string) + # $1, $2 don't work with SafeBuffer so casting to string as workaround + safe = string.html_safe? + string = string.to_str.gsub(MATCH) do + pattern, key = $1, $1.to_sym + + if !values.include?(key) + raise I18n::MissingInterpolationArgument.new(pattern, string) + else + v = values[key].to_s + if safe && !v.html_safe? + ERB::Util.h(v) else - values[key].to_s + v end end - else - reserved_keys = if defined?(I18n::RESERVED_KEYS) # rails 3+ - I18n::RESERVED_KEYS - else - I18n::Backend::Base::RESERVED_KEYS - end - - string % values.except(*reserved_keys) end + safe ? string.html_safe : string end diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index b75e6ed63..0a12ab3bb 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -8,8 +8,7 @@ module MailHandler 'Mail' end - # Note that the decode flag is not yet used - def mail_from_raw_email(data, decode=true) + def mail_from_raw_email(data) Mail.new(data) end diff --git a/lib/mail_handler/backends/tmail_backend.rb b/lib/mail_handler/backends/tmail_backend.rb index 02124cdb1..1e241f261 100644 --- a/lib/mail_handler/backends/tmail_backend.rb +++ b/lib/mail_handler/backends/tmail_backend.rb @@ -8,14 +8,12 @@ module MailHandler # Turn raw data into a structured TMail::Mail object # Documentation at http://i.loveruby.net/en/projects/tmail/doc/ - def mail_from_raw_email(data, decode=true) + def mail_from_raw_email(data) # Hack round bug in TMail's MIME decoding. # Report of TMail bug: # http://rubyforge.org/tracker/index.php?func=detail&aid=21810&group_id=4512&atid=17370 copy_of_raw_data = data.gsub(/; boundary=\s+"/im,'; boundary="') - mail = TMail::Mail.parse(copy_of_raw_data) - mail.base64_decode if decode - mail + TMail::Mail.parse(copy_of_raw_data) end # Extracts all attachments from the given TNEF file as a TMail::Mail object @@ -105,12 +103,12 @@ module MailHandler if part.content_type == 'message/rfc822' # An email attached as text # e.g. http://www.whatdotheyknow.com/request/64/response/102 - part.rfc822_attachment = mail_from_raw_email(part.body, decode=false) + part.rfc822_attachment = mail_from_raw_email(part.body) elsif part.content_type == 'application/vnd.ms-outlook' || part_filename && AlaveteliFileTypes.filename_to_mimetype(part_filename) == 'application/vnd.ms-outlook' # An email attached as an Outlook file # e.g. http://www.whatdotheyknow.com/request/chinese_names_for_british_politi msg = Mapi::Msg.open(StringIO.new(part.body)) - part.rfc822_attachment = mail_from_raw_email(msg.to_mime.to_s, decode=false) + part.rfc822_attachment = mail_from_raw_email(msg.to_mime.to_s) elsif part.content_type == 'application/ms-tnef' # A set of attachments in a TNEF file part.rfc822_attachment = mail_from_tnef(part.body) diff --git a/lib/make_html_4_compliant.rb b/lib/make_html_4_compliant.rb index 214eb9f1f..8926d5873 100644 --- a/lib/make_html_4_compliant.rb +++ b/lib/make_html_4_compliant.rb @@ -3,7 +3,6 @@ ActionView::Helpers::TagHelper.module_eval do def tag(name, options = nil, open = false, escape = true) - "<#{name}#{tag_options(options, escape) if options}" + (open ? ">" : ">") + "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : ">"}".html_safe end end - diff --git a/lib/tasks/gettext.rake b/lib/tasks/gettext.rake index 787b4029f..c73c2584e 100644 --- a/lib/tasks/gettext.rake +++ b/lib/tasks/gettext.rake @@ -4,6 +4,15 @@ Dir["#{Gem.searcher.find('gettext_i18n_rails').full_gem_path}/lib/tasks/**/*.rak namespace :gettext do + desc 'Rewrite .po files into a consistent msgmerge format' + task :clean do + load_gettext + + Dir.glob("locale/*/app.po") do |po_file| + GetText::msgmerge(po_file, po_file, 'alaveteli', :msgmerge => [:sort_output, :no_location, :no_wrap]) + end + end + desc "Update pot file only, without fuzzy guesses (these are done by Transifex)" task :findpot => :environment do load_gettext @@ -17,12 +26,12 @@ namespace :gettext do #merge tmp.pot and existing pot FileUtils.mkdir_p('locale') - GetText::msgmerge("locale/app.pot", temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ]) + GetText::msgmerge("locale/app.pot", temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ]) Dir.glob("locale/*/app.po") do |po_file| - GetText::msgmerge(po_file, temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ]) + GetText::msgmerge(po_file, temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ]) end File.delete(temp_pot) - end + end def files_to_translate Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml,rhtml}") diff --git a/lib/tasks/rspec.rake b/lib/tasks/rspec.rake index 1eee74aee..d4fd4a9ff 100644 --- a/lib/tasks/rspec.rake +++ b/lib/tasks/rspec.rake @@ -23,6 +23,9 @@ rescue MissingSourceFile module Spec module Rake class SpecTask + if defined?(::Rake::DSL) + include ::Rake::DSL + end def initialize(name) task name do # if rspec-rails is a configured gem, this will output helpful material and exit ... @@ -52,8 +55,6 @@ end task :default => :spec task :stats => "spec:statsetup" -# XXX commonlib tests are not Ruby 1.9 compatible -#task :spec => ['spec:commonlib'] task :test => ['spec'] task :cruise => ['spec'] diff --git a/lib/tasks/translation.rake b/lib/tasks/translation.rake index 273c12bfa..ff07fc6f6 100644 --- a/lib/tasks/translation.rake +++ b/lib/tasks/translation.rake @@ -4,7 +4,7 @@ namespace :translation do include Usage def write_email(email, email_description, output_file) - mail_object = MailHandler.mail_from_raw_email(email.to_s, decode=false) + mail_object = MailHandler.mail_from_raw_email(email.to_s) output_file.write("\n") output_file.write("Description of email: #{email_description}\n") output_file.write("Subject line: #{mail_object.subject}\n") @@ -49,7 +49,7 @@ namespace :translation do write_email(followup_email, 'Follow up', output_file) # contact mailer - contact_email = ContactMailer.create_message(info_request.user_name, + contact_email = ContactMailer.create_to_admin_message(info_request.user_name, info_request.user.email, 'A test message', 'Hello!', @@ -86,7 +86,7 @@ namespace :translation do 'fixtures', 'files', 'incoming-request-plain.email')) - response_mail = MailHandler.mail_from_raw_email(content, decode=false) + response_mail = MailHandler.mail_from_raw_email(content) response_mail.from = "authority@example.com" stopped_responses_email = RequestMailer.create_stopped_responses(info_request, diff --git a/lib/timezone_fixes.rb b/lib/timezone_fixes.rb index e6d2f9470..1bf326ccd 100644 --- a/lib/timezone_fixes.rb +++ b/lib/timezone_fixes.rb @@ -4,7 +4,9 @@ # Otherwise times get stored wrong during British Summer Time -# Hopefully fixed in later Rails. There is a test in spec/libs/timezone_fixes.rb +# Hopefully fixed in later Rails. There is a test in spec/lib/timezone_fixes_spec.rb + +# This fix is applied in Rails 3.x. So, should be possible to remove this then! # Monkeypatch! module ActiveRecord diff --git a/lib/use_spans_for_errors.rb b/lib/use_spans_for_errors.rb index cda05c588..135453f78 100644 --- a/lib/use_spans_for_errors.rb +++ b/lib/use_spans_for_errors.rb @@ -8,5 +8,5 @@ # # See http://dev.rubyonrails.org/ticket/2210 -ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| %(<span class="fieldWithErrors">#{html_tag}</span>)} +ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| %(<span class="fieldWithErrors">#{html_tag}</span>).html_safe} |