#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
if ARGV.size != 2
puts "Usage: #{$0} SOURCE_DIR DEST_DIR"
exit(false)
end
require 'pathname'
require "fileutils"
def fix_link(text, extension, language)
send("fix_#{extension}_link", text, language)
end
def fix_link_path(text)
text.gsub(/\b_(sources|static|images)\b/, '\1')
end
def fix_language_link(url, language)
url.gsub(/\A((?:\.\.\/){2,})([a-z]{2})\/html\//) do
relative_base_path = $1
link_language = $2
close_quote = $3
if language == "en"
relative_base_path = relative_base_path.gsub(/\A\.\.\//, '')
end
if link_language != "en"
relative_base_path += "#{link_language}/"
end
"#{relative_base_path}docs/"
end
end
def fix_html_link(html, language)
html = html.gsub(/(href|src)="(.+?)"/) do
attribute = $1
link = $2
link = fix_link_path(link)
link = fix_language_link(link, language)
"#{attribute}=\"#{link}\""
end
html.gsub(/(id="top-link" href=)"(.+?)"/) do
prefix = $1
top_path = $2.gsub(/\/index\.html\z/, '/')
top_path = "./" if ["index.html", "#"].include?(top_path)
"#{prefix}\"#{top_path}../\""
end
end
def fix_js_link(js, language)
fix_link_path(js)
end
LANGUAGE_TO_LOCALE = {
"ja" => "ja_JP",
"en" => "en_US",
}
def insert_facebook_html_header(html)
html.gsub(/<\/head>/) do
<<-HTML
HTML
end
end
def insert_facebook_html_fb_root(html)
html.gsub(/
/) do
<<-HTML
HTML
end
end
def insert_facebook_html_buttons(html)
html.gsub(/(
)/) do
<<-HTML
#{$1}
HTML
end
end
def insert_facebook_html_footer(html, language)
locale = LANGUAGE_TO_LOCALE[language]
raise "unknown locale for language #{language.inspect}" if locale.nil?
html.gsub(/<\/body>/) do
<<-HTML
HTML
end
end
def insert_facebook_html(html, language)
html = insert_facebook_html_header(html)
html = insert_facebook_html_fb_root(html)
html = insert_facebook_html_buttons(html)
html = insert_facebook_html_footer(html, language)
html
end
source_dir, dest_dir = ARGV
source_dir = Pathname.new(source_dir)
dest_dir = Pathname.new(dest_dir)
language_dirs = []
source_dir.each_entry do |top_level_path|
language_dirs << top_level_path if /\A[a-z]{2}\z/ =~ top_level_path.to_s
end
language_dirs.each do |language_dir|
language = language_dir.to_s
language_source_dir = source_dir + language_dir + "html"
language_dest_dir = dest_dir + language_dir
language_source_dir.find do |source_path|
relative_path = source_path.relative_path_from(language_source_dir)
dest_path = language_dest_dir + relative_path
if source_path.directory?
dest_path.mkpath
else
case source_path.extname
when ".html", ".js"
content = source_path.read
extension = source_path.extname.gsub(/\A\./, '')
content = fix_link(content, extension, language)
if extension == "html"
content = insert_facebook_html(content, language)
end
dest_path.open("wb") do |dest|
dest.print(content.strip)
end
FileUtils.touch(dest_path, :mtime => source_path.mtime)
else
case source_path.basename.to_s
when ".buildinfo"
# ignore
else
FileUtils.cp(source_path, dest_path, :preserve => true)
end
end
end
end
end
dest_dir.find do |dest_path|
if dest_path.directory? and /\A_/ =~ dest_path.basename.to_s
normalized_dest_path = dest_path + ".."
normalized_dest_path += dest_path.basename.to_s.gsub(/\A_/, '')
FileUtils.mv(dest_path, normalized_dest_path)
end
end