summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/tools/prepare-sphinx-html.rb
blob: 71e12f0e3cab5744a62b5f8badcba0d08e6ff8e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/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
    <meta property="fb:page_id" content="238184682903165" /><!-- mroonga -->
    <meta property="fb:admins" content="664204556" /><!-- kouhei.sutou -->
    <meta property="og:type" content="product" />
    <meta property="og:image" content="http://mroonga.org/images/logos/mroonga-icon-full-size.png" />
    <meta property="og:site_name" content="mroonga" />

    <link rel="stylesheet" href="/css/sphinx.css" type="text/css" />
  </head>
    HTML
  end
end

def insert_facebook_html_fb_root(html)
  html.gsub(/<body>/) do
    <<-HTML
  <body>
    <div id="fb-root"></div>
    HTML
  end
end

def insert_facebook_html_buttons(html)
  html.gsub(/(<div class="other-language-links">)/) do
    <<-HTML
    <div class="facebook-buttons">
      <fb:like href="http://www.facebook.com/pages/mroonga/238184682903165"
               layout="standard"
               width="290"></fb:like>
    </div>
    #{$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
    <script src="http://connect.facebook.net/#{locale}/all.js"></script>

    <script>
      FB.init({
         appId  : null,
         status : true, // check login status
         cookie : true, // enable cookies to allow the server to access the session
         xfbml  : true  // parse XFBML
      });
    </script>
  </body>
    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