Skip to content

Instantly share code, notes, and snippets.

@soramugi
Created May 26, 2014 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soramugi/c294d310da5c4ddf1756 to your computer and use it in GitHub Desktop.
Save soramugi/c294d310da5c4ddf1756 to your computer and use it in GitHub Desktop.
markdown内のimageタグをlazy load形式に書き換え
require 'tempfile'
post_dir = 'source/_posts'
files = Dir.glob("#{File.expand_path(post_dir)}/*")
files.each do |file|
p file
temp = Tempfile::new('temp')
open(file, 'r') do |f|
f.each do |line|
if line.index(/!\[[0-9]+\..+\]/)
if line.index(/!\[1\..+\]/)
else
line.gsub!(/!\[([0-9]+\..+)\]\((\S+).+\)/) do
"<img alt='#{$1}' data-original='#{$2}'>"
end
end
end
temp.puts(line)
end
end
temp.close
temp.open
open(file, "w") {|f| temp.each {|line| f.puts(line) }}
temp.close(true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment