1
0
Fork 0
mirror of synced 2024-05-18 11:52:17 +12:00
waifu2x/webgen/gen.rb
nagadomi 122ffd37fe Web page
- Add `webgen/gen.rb` to generate web page from template
- Add support for mobile browser
- Change layouts
2016-02-07 05:58:27 +09:00

57 lines
1.5 KiB
Ruby

require 'erb'
require 'yaml'
require 'optparse'
require 'fileutils'
def symbolize_keys(val)
if val.is_a?(Hash)
val.map{|k,v|
[k.to_sym, symbolize_keys(v)]
}.to_h
elsif val.is_a?(Array)
val = val.map{|v| symbolize_keys(v)}
else
val
end
end
def load_locales(dir)
locales = {}
Dir.entries(dir).each do |ent|
if ent =~ /^\w\w.yml$/
lang = File.basename(ent, ".yml")
yml = YAML.load_file(File.join(dir, ent))
if yml
locales[lang.to_sym] = symbolize_keys(yml)
else
locales[lang.to_sym] = {}
end
end
end
locales
end
def copy(indir, outdir)
files = Dir.entries(indir).to_a.map{|ent|
File.join(indir, ent)
}.select{|ent|
File.file?(ent)
}
FileUtils.copy(files, outdir, preserve: true)
end
DIR = File.dirname(__FILE__)
DEFAULT_LANG = :en
LANG_DIR = File.join(DIR, "locales")
OUTPUT_DIR = File.join(DIR, "..", "assets")
DONT_MAKE_CHANGE = "This file was automatically generated by webgen/gen.rb. Do not make changes to this file manually."
locales = load_locales(LANG_DIR)
template = File.read(File.join(DIR, "templates", "index.html.erb"))
erb = ERB.new(template)
locales.each do |lang, locale|
output_path = File.join(OUTPUT_DIR, lang == DEFAULT_LANG ? "index.html" : "index.#{lang}.html")
t = locales[DEFAULT_LANG].merge(locale)
t[:dont_make_change] = DONT_MAKE_CHANGE
t[:lang] = lang.to_s
File.write(output_path, erb.result(binding))
end
copy(File.join(DIR, "assets"), OUTPUT_DIR)