SafariからダウンロードしたHTMLファイルをマージするRubyスクリプト、その2
変更点
1.<pre>〜</pre>内部で、行頭のスペースを、 に変換、行末に<br/>を付けるように変更。
2.引数が一個与えられた場合は、与引数でディレクトリを作成する。そして、元のHTMファイルやディレクトリを全部、そのディレクトリに移動する。globalstyle.cssはコピーする。
require "fileutils" def formatCode(line) line2 = line + "<br/>" len = line2.length line3 = "" len.times{|i| c = line2[i].chr if c == " " then line3 = line3 + " " else line3 = line3 + line2[i,len] break end } return line3 end def moveFiles(dst, css) Dir.mkdir(dst) unless FileTest.exist?(dst) FileUtils.cp(css, dst + "/" + css) unless FileTest.exist?(dst + "/" + css) Dir.glob("print_*").each{|o| File.rename(o, dst + "/" + o) unless FileTest.exist?(dst + "/" + o) } end # header = true : Keep header / false : delete header # footer = true : Keep footer / false : delete footer def mergeInto(src, dst, header = false, footer = false) lineno = 0 File.open(src){|f| while line = f.gets lineno = f.lineno if f.eof? end } printf "%5d(lines) : %s\n", lineno, src en_header = 57 st_footer = lineno - 5 open(dst, "a"){|f| open(src) {|io| inPre = false while line = io.gets if (footer == false && io.lineno > st_footer) then # nothing to do elsif (header == false && io.lineno < en_header) then # nothing to do elsif (header == false && io.lineno == en_header) then l2 = "<div>" + line.split(/<\/div><div>/)[1] f.write(l2) else if inPre == false && line.index("<pre>") then inPre = true end if inPre == true && line.index("</pre>") then inPre = false end if inPre == true then line = formatCode(line) end f.write(line) end end } } end # copy globalstyle.css to all directories css = "globalstyle.css" if 1 == ARGV.length then dst = "Compiled/" + ARGV[0] moveFiles(dst, css) Dir.chdir(dst) end l = Dir::pwd.split(/\//) mergedFile = l[l.length - 1] + ".html" File.delete(mergedFile) if FileTest.file?(mergedFile) Dir.glob("*_files").each{ |o| next unless FileTest.directory?(o) FileUtils.cp(css, o + "/" + css) unless FileTest.file?(o + "/" + css) } # merge *.htm to mergedFile l = Dir.glob("*.htm") lst = l.length cnt = 1 l.each{ |o| next if FileTest.directory?(o) if (cnt == 1) then # keep header but delete last part mergeInto(o, mergedFile, true, false) elsif (cnt == lst) then # delete header but keep last part mergeInto(o, mergedFile, false, true) else # delete both header and last mergeInto(o, mergedFile) end cnt += 1 } printf "%s created.\n", mergedFile
というか、gistでやれ、って感じか。
ここ--> http://gist.github.com/600823