yatex

annotate newpage.rb @ 138:b7b54906ac3b

add newpage.rb
author yuuji@gentei.org
date Wed, 07 Jul 2010 22:27:25 +0900
parents
children e9c1e80f232e
rev   line source
yuuji@138 1 #!/usr/bin/env ruby
yuuji@138 2 # THIS is very very tentative. Insufficient examination of function.
yuuji@138 3 # Create new HTML file referring other HTML file in the same directory.
yuuji@138 4 # Example:
yuuji@138 5 # newpage.rb Create new index.html by copying template.
yuuji@138 6 # newpage.rb foo.html Create new foo.html whose by copying header
yuuji@138 7 # and footer from index.html.
yuuji@138 8 # newpage.rb d/sub.html Create new directory d (if necessary) and
yuuji@138 9 # d/sub.html by copying header/footer from
yuuji@138 10 # index.html in a same directory or parent
yuuji@138 11 # directory rewriting href to css file
yuuji@138 12 # considering relative path.
yuuji@138 13 # newpage.rb -o [file] Forcibly overwrite existing file.
yuuji@138 14 # newpage.rb -c cssfile Set `cssfile' as defualt css.
yuuji@138 15 # newpage.rb -t template Set `template' as HTML template.
yuuji@138 16 require 'fileutils'
yuuji@138 17
yuuji@138 18 mydir=File.dirname($0)
yuuji@138 19 myname=File.basename($0, ".rb")
yuuji@138 20
yuuji@138 21
yuuji@138 22 index = 'index.html'
yuuji@138 23 cssdefault = nil
yuuji@138 24 overwrite = nil
yuuji@138 25 template = __FILE__ #File.expand_path(myname+".html", mydir)
yuuji@138 26
yuuji@138 27 def guesscss(dir)
yuuji@138 28
yuuji@138 29 end
yuuji@138 30
yuuji@138 31 while ARGV[0] && /^-/ =~ (a0=ARGV[0].dup) && ARGV.shift
yuuji@138 32 break if /^--$/ =~ a0
yuuji@138 33 while /^-[A-Za-z]/ =~ a0
yuuji@138 34 case a0
yuuji@138 35 when "-c" # css
yuuji@138 36 ARGV.shift; cssdefault = ARGV[0]
yuuji@138 37 when "-t" # template
yuuji@138 38 ARGV.shift; cssdefault = ARGV[0]
yuuji@138 39 when "-o" # overwrite
yuuji@138 40 overwrite = true
yuuji@138 41 end
yuuji@138 42 a0.sub!(/-.(.*)/, '-\\1')
yuuji@138 43 end
yuuji@138 44 end
yuuji@138 45
yuuji@138 46 outfile = ARGV[0]||index
yuuji@138 47 if !overwrite && test(?s, outfile) then
yuuji@138 48 STDERR.printf("File \`%s' exists. Use -o option to overwrite.\n", outfile)
yuuji@138 49 exit 1
yuuji@138 50 end
yuuji@138 51
yuuji@138 52 # set css default file
yuuji@138 53 dots = 0
yuuji@138 54 of = outfile
yuuji@138 55 dots+=1 while "." != (of=File.dirname(of))
yuuji@138 56 cssdir = "../"*dots
yuuji@138 57
yuuji@138 58 # set copy source
yuuji@138 59 outdir = File.dirname(outfile)
yuuji@138 60 if "index.html" == File.basename(outfile)
yuuji@138 61 src = (dots == 0 ? template : "index.html")
yuuji@138 62 elsif test(?s, outdir+"/index.html")
yuuji@138 63 src = outdir+"/index.html"
yuuji@138 64 else
yuuji@138 65 src = template
yuuji@138 66 end
yuuji@138 67
yuuji@138 68 FileUtils.mkdir_p(outdir)
yuuji@138 69
yuuji@138 70 cssfile = cssdir+"main.css"
yuuji@138 71 name = File.basename(outfile, ".html")
yuuji@138 72 begin
yuuji@138 73 open(outfile, "w") do |out|
yuuji@138 74 #IO.foreach(src) do |line|
yuuji@138 75 if src == __FILE__
yuuji@138 76 input = DATA
yuuji@138 77 else
yuuji@138 78 input = open(src, "r")
yuuji@138 79 end
yuuji@138 80 begin
yuuji@138 81 html = input.readlines.join
yuuji@138 82 html.sub!(%r|^<h1.*<\/h1>|i, sprintf("<h1>%s</h1>\n", name))
yuuji@138 83 if !html.gsub!("__CSSFILE__", cssfile)
yuuji@138 84 html.gsub!(/href=(['\"])(.*\.css)\1/, 'href="\1"')
yuuji@138 85 end
yuuji@138 86 html.gsub!("__TITLE__", name)
yuuji@138 87 out.print html
yuuji@138 88 ensure
yuuji@138 89 input.close
yuuji@138 90 end
yuuji@138 91 end
yuuji@138 92 printf(<<_EOS_, outfile, name)
yuuji@138 93 <a href="%s">%s</a>
yuuji@138 94 _EOS_
yuuji@138 95 rescue
yuuji@138 96 p $!
yuuji@138 97 STDERR.printf(<<'_EOS_', outfile, outfile)
yuuji@138 98 Cannot output to [%s]. Do
yuuji@138 99 chmod +w %s
yuuji@138 100 or
yuuji@138 101 chmod +w .
yuuji@138 102 or change output directory.
yuuji@138 103 _EOS_
yuuji@138 104 exit 1
yuuji@138 105 end
yuuji@138 106
yuuji@138 107 __END__
yuuji@138 108 <html>
yuuji@138 109 <head>
yuuji@138 110 <title>__TITLE__</title>
yuuji@138 111 <style type="text/css">
yuuji@138 112 <!--
yuuji@138 113 /* Local CSS here */
yuuji@138 114 -->
yuuji@138 115 </style>
yuuji@138 116 <link rel="stylesheet" type="text/css" href="__CSSFILE__">
yuuji@138 117 </head>
yuuji@138 118
yuuji@138 119 <body>
yuuji@138 120 <h1>__TITLE__</h1>
yuuji@138 121
yuuji@138 122 <!--#include virtual="/~yuuji/signature.html"-->
yuuji@138 123 </body>
yuuji@138 124 </html>