Created
May 26, 2014 08:24
-
-
Save soramugi/c294d310da5c4ddf1756 to your computer and use it in GitHub Desktop.
markdown内のimageタグをlazy load形式に書き換え
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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