yuuji@138: #!/usr/bin/env ruby yuuji@138: # THIS is very very tentative. Insufficient examination of function. yuuji@138: # Create new HTML file referring other HTML file in the same directory. yuuji@139: # (C)2010 by HIROSE Yuuji [yuuji@yatex.org] yuuji@140: # Last modified Mon Sep 6 16:16:33 2010 on firestorm yuuji@366: # $Id$ yuuji@139: # http://www.yatex.org yuuji@138: # Example: yuuji@138: # newpage.rb Create new index.html by copying template. yuuji@138: # newpage.rb foo.html Create new foo.html whose by copying header yuuji@138: # and footer from index.html. yuuji@138: # newpage.rb d/sub.html Create new directory d (if necessary) and yuuji@138: # d/sub.html by copying header/footer from yuuji@138: # index.html in a same directory or parent yuuji@138: # directory rewriting href to css file yuuji@138: # considering relative path. yuuji@138: # newpage.rb -o [file] Forcibly overwrite existing file. yuuji@138: # newpage.rb -c cssfile Set `cssfile' as defualt css. yuuji@138: # newpage.rb -t template Set `template' as HTML template. yuuji@138: require 'fileutils' yuuji@138: yuuji@138: mydir=File.dirname($0) yuuji@138: myname=File.basename($0, ".rb") yuuji@138: yuuji@138: yuuji@138: index = 'index.html' yuuji@138: cssdefault = nil yuuji@138: overwrite = nil yuuji@138: template = __FILE__ #File.expand_path(myname+".html", mydir) yuuji@138: yuuji@138: def guesscss(dir) yuuji@138: yuuji@138: end yuuji@138: yuuji@138: while ARGV[0] && /^-/ =~ (a0=ARGV[0].dup) && ARGV.shift yuuji@138: break if /^--$/ =~ a0 yuuji@138: while /^-[A-Za-z]/ =~ a0 yuuji@138: case a0 yuuji@138: when "-c" # css yuuji@138: ARGV.shift; cssdefault = ARGV[0] yuuji@138: when "-t" # template yuuji@138: ARGV.shift; cssdefault = ARGV[0] yuuji@138: when "-o" # overwrite yuuji@138: overwrite = true yuuji@138: end yuuji@138: a0.sub!(/-.(.*)/, '-\\1') yuuji@138: end yuuji@138: end yuuji@138: yuuji@138: outfile = ARGV[0]||index yuuji@138: if !overwrite && test(?s, outfile) then yuuji@138: STDERR.printf("File \`%s' exists. Use -o option to overwrite.\n", outfile) yuuji@138: exit 1 yuuji@138: end yuuji@138: yuuji@138: # set css default file yuuji@138: dots = 0 yuuji@138: of = outfile yuuji@138: dots+=1 while "." != (of=File.dirname(of)) yuuji@138: cssdir = "../"*dots yuuji@138: yuuji@138: # set copy source yuuji@138: outdir = File.dirname(outfile) yuuji@138: if "index.html" == File.basename(outfile) yuuji@138: src = (dots == 0 ? template : "index.html") yuuji@138: elsif test(?s, outdir+"/index.html") yuuji@138: src = outdir+"/index.html" yuuji@138: else yuuji@138: src = template yuuji@138: end yuuji@138: yuuji@138: FileUtils.mkdir_p(outdir) yuuji@138: yuuji@138: cssfile = cssdir+"main.css" yuuji@138: name = File.basename(outfile, ".html") yuuji@138: begin yuuji@138: open(outfile, "w") do |out| yuuji@138: #IO.foreach(src) do |line| yuuji@138: if src == __FILE__ yuuji@138: input = DATA yuuji@138: else yuuji@138: input = open(src, "r") yuuji@138: end yuuji@138: begin yuuji@138: html = input.readlines.join yuuji@138: html.sub!(%r|^|i, sprintf("

%s

\n", name)) yuuji@138: if !html.gsub!("__CSSFILE__", cssfile) yuuji@140: html.gsub!(/href=(['\"])(.*\.css)\1/, 'href="'+cssdir+'\2"') yuuji@138: end yuuji@138: html.gsub!("__TITLE__", name) yuuji@138: out.print html yuuji@138: ensure yuuji@138: input.close yuuji@138: end yuuji@138: end yuuji@138: printf(<<_EOS_, outfile, name) yuuji@138: %s yuuji@138: _EOS_ yuuji@138: rescue yuuji@138: p $! yuuji@138: STDERR.printf(<<'_EOS_', outfile, outfile) yuuji@138: Cannot output to [%s]. Do yuuji@138: chmod +w %s yuuji@138: or yuuji@138: chmod +w . yuuji@138: or change output directory. yuuji@138: _EOS_ yuuji@138: exit 1 yuuji@138: end yuuji@138: yuuji@138: __END__ yuuji@138: yuuji@138: yuuji@138: __TITLE__ yuuji@138: yuuji@138: yuuji@138: yuuji@138: yuuji@138: yuuji@138:

__TITLE__

yuuji@138: yuuji@138: yuuji@138: yuuji@138: