yatex

view newpage.rb @ 138:b7b54906ac3b

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