138
|
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'
|
|
17
|
|
18 mydir=File.dirname($0)
|
|
19 myname=File.basename($0, ".rb")
|
|
20
|
|
21
|
|
22 index = 'index.html'
|
|
23 cssdefault = nil
|
|
24 overwrite = nil
|
|
25 template = __FILE__ #File.expand_path(myname+".html", mydir)
|
|
26
|
|
27 def guesscss(dir)
|
|
28
|
|
29 end
|
|
30
|
|
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
|
|
45
|
|
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
|
|
51
|
|
52 # set css default file
|
|
53 dots = 0
|
|
54 of = outfile
|
|
55 dots+=1 while "." != (of=File.dirname(of))
|
|
56 cssdir = "../"*dots
|
|
57
|
|
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
|
|
67
|
|
68 FileUtils.mkdir_p(outdir)
|
|
69
|
|
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
|
|
106
|
|
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>
|
|
118
|
|
119 <body>
|
|
120 <h1>__TITLE__</h1>
|
|
121
|
|
122 <!--#include virtual="/~yuuji/signature.html"-->
|
|
123 </body>
|
|
124 </html>
|