123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- require 'erb'
- require 'yaml'
- require 'optparse'
- require 'fileutils'
- def to_h(a)
- Hash[a]
- end
- def symbolize_keys(val)
- if val.is_a?(Hash)
- to_h(val.map{|k,v|
- [k.to_sym, symbolize_keys(v)]
- })
- 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 =~ /\.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)
|