1#!/usr/bin/env ruby
2
3require 'optparse'
4
5template_directory = File.dirname(__FILE__)
6template_suffix = nil
7target_language = nil
8site_title = nil
9opts = OptionParser.new do |opts|
10  opts.on("--help", "Show this message") do
11    puts opts
12    exit 0
13  end
14
15  opts.on("--template-directory=DIRECTORY", "Template directory") do |dir|
16    template_directory = dir
17  end
18
19  opts.on("--suffix=SUFFIX", "Template file suffix") do |suffix|
20    suffix = nil if suffix.empty?
21    template_suffix = suffix
22  end
23
24  opts.on("--language=LANGUAGE", "Target language") do |language|
25    target_language = language
26  end
27
28  opts.on("--title=TITLE", "Site title") do |title|
29    site_title = title
30  end
31end
32opts.parse!
33
34def read_template(template_directory, type, suffix, language)
35  file = [type, suffix, language].compact.join("-") + ".html"
36  File.read(File.join(template_directory, file))
37end
38
39head = read_template(template_directory, "head",
40                     template_suffix, target_language)
41header = read_template(template_directory, "header",
42                       template_suffix, target_language)
43footer = read_template(template_directory, "footer",
44                       template_suffix, target_language)
45ARGV.each do |target|
46  File.open(target, "r+") do |input|
47    content = input.read
48    content = content.sub(/(<\/title>)/, " - #{site_title}\\1") if site_title
49    content = content.sub(/(<\/head>)/, head + "\n\\1")
50    content = content.sub(/(<body\s.+?>)/, "\\1\n" + header)
51    content = content.sub(/(<\/body>)/, footer + "\n\\1")
52    input.seek(0)
53    input.print(content)
54  end
55end
56