Skip to content

Instantly share code, notes, and snippets.

@grauwoelfchen
Created November 11, 2011 11:13
Show Gist options
  • Select an option

  • Save grauwoelfchen/92ad255cabf2b773c4e5 to your computer and use it in GitHub Desktop.

Select an option

Save grauwoelfchen/92ad255cabf2b773c4e5 to your computer and use it in GitHub Desktop.
sub
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'tempfile'
file = ARGV.shift
temp = Tempfile.new('sub', '/tmp')
File.open("#{file}", "r") { |f|
f.read.each { |line|
# head
r = Regexp.new('head\((".*")\s*,\s*".*"\s*,\s".*"\)')
if r =~ line
print line.chomp + " \#=> "
puts line.sub!(r, "head(#{$1})")
end
# img/dot2.gif => img/dot/dot_2.gif
# img/dot2.jpg => img/dot/dot_2.gif
r = Regexp.new('(img\("img/dot)2\.(jpg|gif)"\)')
if r =~ line
print line.chomp + " \#=> "
puts line.sub!(r, "#{$1}/dot_2.gif\")")
end
# img/dot_line.jpg => img/dot/dot_m.gif
# img/line_menu.jpg => img/dot/dot_m.gif
r = Regexp.new('img\("img/(dot_line|line_menu)\.jpg"\)')
if r =~ line
print line.chomp + " \#=> "
puts line.sub!(r, 'img("img/dot/dot_m.gif")')
end
# <div><img ...></div>
r = Regexp.new('^(\t*)(<img\ssrc="<\?php\simg\("img/dot/.*"\);\s*\?>"\s*/>)$')
if r =~ line
print line.chomp + " \#=> "
puts line.sub!(r, "#{$1}<div>#{$2}</div>")
end
temp.puts(line)
}
}
temp.close
temp.open
File.open(file, 'w') { |f|
puts(">>> " + file)
temp.each { |line|
f.puts(line)
}
}
temp.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment