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