yatex

view newpage.rb @ 526:8eb15c0f9627

Preview image generation modified
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 24 Feb 2018 16:44:25 +0900
parents d69fd7b1ac4d
children
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 # (C)2010 by HIROSE Yuuji [yuuji@yatex.org]
5 # Last modified Mon Sep 6 16:16:33 2010 on firestorm
6 # $Id$
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.
20 require 'fileutils'
22 mydir=File.dirname($0)
23 myname=File.basename($0, ".rb")
26 index = 'index.html'
27 cssdefault = nil
28 overwrite = nil
29 template = __FILE__ #File.expand_path(myname+".html", mydir)
31 def guesscss(dir)
33 end
35 while 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
48 end
50 outfile = ARGV[0]||index
51 if !overwrite && test(?s, outfile) then
52 STDERR.printf("File \`%s' exists. Use -o option to overwrite.\n", outfile)
53 exit 1
54 end
56 # set css default file
57 dots = 0
58 of = outfile
59 dots+=1 while "." != (of=File.dirname(of))
60 cssdir = "../"*dots
62 # set copy source
63 outdir = File.dirname(outfile)
64 if "index.html" == File.basename(outfile)
65 src = (dots == 0 ? template : "index.html")
66 elsif test(?s, outdir+"/index.html")
67 src = outdir+"/index.html"
68 else
69 src = template
70 end
72 FileUtils.mkdir_p(outdir)
74 cssfile = cssdir+"main.css"
75 name = File.basename(outfile, ".html")
76 begin
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_
99 rescue
100 p $!
101 STDERR.printf(<<'_EOS_', outfile, outfile)
102 Cannot output to [%s]. Do
103 chmod +w %s
104 or
105 chmod +w .
106 or change output directory.
107 _EOS_
108 exit 1
109 end
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>
123 <body>
124 <h1>__TITLE__</h1>
126 <!--#include virtual="/~yuuji/signature.html"-->
127 </body>
128 </html>