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