public
Created

convert files/directories to ensure utf8 marker.

  • Download Gist
utf8.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#! /usr/bin/env ruby
# encoding: utf-8
 
#
# converts files to include a utf8 marker
#
# usage: utf8 files_or_directories
 
##
#
argv = ARGV.map{|arg| "#{ arg }"}
stdin = argv.delete('-')
argv.push(STDIN.readlines) if stdin
 
argv.flatten!
argv.compact!
argv.uniq!
 
files, directories = argv.partition{|arg| test(?f, arg)}
 
directories.each do |directory|
glob = File.join(File.expand_path(directory), '**/**')
 
Dir.glob(glob) do |entry|
files.push(File.expand_path(entry)) if test(?f, entry)
end
end
 
argv.flatten!
argv.compact!
argv.uniq!
 
##
#
utf_8 = "# encoding: utf-8"
 
files.each do |file|
head = []
tail = []
 
shebang = nil
had_encoding_line = false
 
open(file, 'r+') do |fd|
in_tail = false
 
fd.each do |line|
if in_tail
tail.push(line)
next
end
 
head.push(line)
 
is_blank = line.strip.empty?
is_shebang = line =~ /^\s*#\s*!/
is_encoding = line =~ /^\s*#.*(encoding|coding)/
 
if is_blank
next
end
 
if is_shebang
shebang ||= line
next
end
 
if is_encoding
had_encoding_line ||= line
next
end
 
in_tail = true
end
end
 
unless had_encoding_line
open(file, 'w') do |fd|
fd.puts(shebang) if shebang
fd.puts(utf_8)
fd.puts
fd.write(tail.join)
end
 
puts(file)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.