Skip to content

Instantly share code, notes, and snippets.

@francesc
Created December 12, 2010 19:03
Show Gist options
  • Select an option

  • Save francesc/738245 to your computer and use it in GitHub Desktop.

Select an option

Save francesc/738245 to your computer and use it in GitHub Desktop.

Revisions

  1. francesc created this gist Dec 12, 2010.
    21 changes: 21 additions & 0 deletions utf8encode.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    desc "Manage the encoding header of Ruby files"
    task :utf8_encode_headers => :environment do
    files = Array.new
    ["*.rb", "*.rake"].each do |extension|
    files.concat(Dir[ File.join(Dir.getwd.split(/\\/), "**", extension) ])
    end

    files.each do |file|
    content = File.read(file)
    next if content[0..16] == "# coding: UTF-8\n\n" ||
    content[0..22] == "# -*- coding: utf-8 -*-"

    ["\n\n", "\n"].each do |file_end|
    content = content.gsub(/(# encoding: UTF-8#{file_end})|(# coding: UTF-8#{file_end})|(# -*- coding: UTF-8 -*-#{file_end})|(# -*- coding: utf-8 -*-#{file_end})/i, "")
    end

    new_file = File.open(file, "w")
    new_file.write("# coding: UTF-8\n\n"+content)
    new_file.close
    end
    end