Class: ReVIEW::WEBMaker

Inherits:
Object show all
Includes:
ERB::Util, MakerHelper
Defined in:
lib/review/webmaker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MakerHelper

bindir, #cleanup_mathimg, copy_images_to_dir, #default_imgmath_preamble, #make_math_images, #make_math_images_dvipng, #make_math_images_pdfcrop

Constructor Details

#initializeWEBMaker

Returns a new instance of WEBMaker.



32
33
34
35
# File 'lib/review/webmaker.rb', line 32

def initialize
  @basedir = nil
  @logger = ReVIEW.logger
end

Instance Attribute Details

#basedirObject

Returns the value of attribute basedir.



30
31
32
# File 'lib/review/webmaker.rb', line 30

def basedir
  @basedir
end

#configObject

Returns the value of attribute config.



30
31
32
# File 'lib/review/webmaker.rb', line 30

def config
  @config
end

Class Method Details

.execute(*args) ⇒ Object



46
47
48
# File 'lib/review/webmaker.rb', line 46

def self.execute(*args)
  self.new.execute(*args)
end

Instance Method Details

#build_body(basetmpdir, _yamlfile) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/review/webmaker.rb', line 138

def build_body(basetmpdir, _yamlfile)
  base_path = Pathname.new(@basedir)
  builder = ReVIEW::HTMLBuilder.new
  @converter = ReVIEW::Converter.new(@book, builder)
  @book.parts.each do |part|
    if part.name.present?
      if part.file?
        build_chap(part, base_path, basetmpdir, true)
      else
        htmlfile = "part_#{part.number}.#{@config['htmlext']}"
        build_part(part, basetmpdir, htmlfile)
        # title = ReVIEW::I18n.t('part', part.number)
        # title += ReVIEW::I18n.t('chapter_postfix') + part.name.strip unless part.name.strip.empty?
      end
    end

    part.chapters.each { |chap| build_chap(chap, base_path, basetmpdir, false) }
  end
end

#build_chap(chap, base_path, basetmpdir, ispart) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/review/webmaker.rb', line 181

def build_chap(chap, base_path, basetmpdir, ispart)
  filename = ''

  if ispart.present?
    filename = chap.path
  else
    filename = Pathname.new(chap.path).relative_path_from(base_path).to_s
  end
  id = File.basename(filename).sub(/\.re\Z/, '')

  if @buildonly && !@buildonly.include?(id)
    warn "skip #{id}.re"
    return
  end

  htmlfile = "#{id}.#{@config['htmlext']}"

  if @config['params'].present?
    warn %Q('params:' in config.yml is obsoleted.)
  end

  begin
    @converter.convert(filename, File.join(basetmpdir, htmlfile))
  rescue => e
    warn "compile error in #{filename} (#{e.class})"
    warn e.message
  end
end

#build_indexpage(basetmpdir) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/review/webmaker.rb', line 261

def build_indexpage(basetmpdir)
  File.open("#{basetmpdir}/index.html", 'w') do |f|
    if @config['coverimage']
      file = File.join(@config['imagedir'], @config['coverimage'])
      @body = <<-EOT
  <div id="cover-image" class="cover-image">
<img src="#{file}" class="max"/>
  </div>
    EOT
    else
      @body = ''
    end
    @language = @config['language']
    @stylesheets = @config['stylesheet']
    @toc = ReVIEW::WEBTOCPrinter.book_to_string(@book)
    @next = @book.chapters[0]
    @next_title = @next ? @next.title : ''
    tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
    f.write ReVIEW::Template.load(tmplfile).result(binding)
  end
end

#build_part(part, basetmpdir, htmlfile) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/review/webmaker.rb', line 158

def build_part(part, basetmpdir, htmlfile)
  File.open("#{basetmpdir}/#{htmlfile}", 'w') do |f|
    @body = ''
    @body << %Q(<div class="part">\n)
    @body << %Q(<h1 class="part-number">#{ReVIEW::I18n.t('part', part.number)}</h1>\n)
    @body << %Q(<h2 class="part-title">#{part.name.strip}</h2>\n) if part.name.strip.present?
    @body << "</div>\n"

    @language = @config['language']
    @stylesheets = @config['stylesheet']
    tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
    f.write ReVIEW::Template.load(tmplfile).result(binding)
  end
end

#build_pathObject



73
74
75
# File 'lib/review/webmaker.rb', line 73

def build_path
  @config['docroot'] || 'webroot'
end

#build_titlepage(basetmpdir, htmlfile) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/review/webmaker.rb', line 283

def build_titlepage(basetmpdir, htmlfile)
  File.open("#{basetmpdir}/#{htmlfile}", 'w') do |f|
    @body = ''
    @body << %Q(<div class="titlepage">)
    @body << %Q(<h1 class="tp-title">#{CGI.escapeHTML(@config.name_of('booktitle'))}</h1>)
    if @config['aut']
      @body << %Q(<h2 class="tp-author">#{join_with_separator(@config.names_of('aut'), ReVIEW::I18n.t('names_splitter'))}</h2>)
    end
    if @config['pbl']
      @body << %Q(<h3 class="tp-publisher">#{join_with_separator(@config.names_of('pbl'), ReVIEW::I18n.t('names_splitter'))}</h3>)
    end
    @body << '</div>'

    @language = @config['language']
    @stylesheets = @config['stylesheet']
    tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
    f.write ReVIEW::Template.load(tmplfile).result(binding)
  end
end

#clean_mathdirObject



132
133
134
135
136
# File 'lib/review/webmaker.rb', line 132

def clean_mathdir
  if @config['imgmath'] && File.exist?("#{@config['imagedir']}/_review_math")
    FileUtils.rm_rf("#{@config['imagedir']}/_review_math")
  end
end

#copy_backmatter(_basetmpdir) ⇒ Object



303
304
305
306
307
308
309
310
# File 'lib/review/webmaker.rb', line 303

def copy_backmatter(_basetmpdir)
  copy_file_with_param('profile')
  copy_file_with_param('advfile')
  if @config['colophon'] && @config['colophon'].is_a?(String)
    copy_file_with_param('colophon', "colophon.#{@config['htmlext']}")
  end
  copy_file_with_param('backcover')
end

#copy_file_with_param(name, target_file = nil) ⇒ Object



312
313
314
315
316
# File 'lib/review/webmaker.rb', line 312

def copy_file_with_param(name, target_file = nil)
  return if @config[name].nil? || !File.exist?(@config[name])
  target_file ||= File.basename(@config[name])
  FileUtils.cp(@config[name], File.join(@path, target_file))
end

#copy_frontmatter(basetmpdir) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/review/webmaker.rb', line 246

def copy_frontmatter(basetmpdir)
  build_indexpage(basetmpdir)

  if @config['titlepage']
    if @config['titlefile']
      FileUtils.cp(@config['titlefile'], "#{basetmpdir}/titlepage.#{@config['htmlext']}")
    else
      build_titlepage(basetmpdir, "titlepage.#{@config['htmlext']}")
    end
  end

  copy_file_with_param('creditfile')
  copy_file_with_param('originaltitlefile')
end

#copy_images(resdir, destdir) ⇒ Object



210
211
212
213
214
215
# File 'lib/review/webmaker.rb', line 210

def copy_images(resdir, destdir)
  return nil unless File.exist?(resdir)
  allow_exts = @config['image_ext']
  FileUtils.mkdir_p(destdir)
  recursive_copy_files(resdir, destdir, allow_exts)
end

#copy_resources(resdir, destdir, allow_exts = nil) ⇒ Object



217
218
219
220
221
222
# File 'lib/review/webmaker.rb', line 217

def copy_resources(resdir, destdir, allow_exts = nil)
  return nil if !resdir || !File.exist?(resdir)
  allow_exts ||= @config['image_ext']
  FileUtils.mkdir_p(destdir)
  recursive_copy_files(resdir, destdir, allow_exts)
end

#copy_stylesheet(basetmpdir) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/review/webmaker.rb', line 238

def copy_stylesheet(basetmpdir)
  if @config['stylesheet'].size > 0
    @config['stylesheet'].each do |sfile|
      FileUtils.cp(sfile, basetmpdir)
    end
  end
end

#error(msg) ⇒ Object



37
38
39
40
# File 'lib/review/webmaker.rb', line 37

def error(msg)
  @logger.error msg
  exit 1
end

#execute(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/review/webmaker.rb', line 82

def execute(*args)
  @config = ReVIEW::Configure.values
  @config.maker = 'webmaker'
  cmd_config, yamlfile = parse_opts(args)
  error "#{yamlfile} not found." unless File.exist?(yamlfile)

  begin
    loader = ReVIEW::YAMLLoader.new
    @config.deep_merge!(loader.load_file(yamlfile))
  rescue => e
    error "yaml error #{e.message}"
  end
  # YAML configs will be overridden by command line options.
  @config.deep_merge!(cmd_config)
  @config['htmlext'] = 'html'
  I18n.setup(@config['language'])
  begin
    generate_html_files(yamlfile)
  rescue ApplicationError => e
    raise if @config['debug']
    error(e.message)
  end
end

#generate_html_files(yamlfile) ⇒ Object



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
# File 'lib/review/webmaker.rb', line 106

def generate_html_files(yamlfile)
  @basedir = File.dirname(yamlfile)
  @path = build_path
  remove_old_files(@path)
  Dir.mkdir(@path)

  @book = ReVIEW::Book.load(@basedir)
  @book.config = @config

  copy_stylesheet(@path)
  copy_frontmatter(@path)
  build_body(@path, yamlfile)
  copy_backmatter(@path)

  math_dir = "./#{@config['imagedir']}/_review_math"
  if @config['imgmath'] && File.exist?("#{math_dir}/__IMGMATH_BODY__.tex")
    make_math_images(math_dir)
  end

  copy_images(@config['imagedir'], "#{@path}/#{@config['imagedir']}")

  copy_resources('covers', "#{@path}/#{@config['imagedir']}")
  copy_resources('adv', "#{@path}/#{@config['imagedir']}")
  copy_resources(@config['fontdir'], "#{@path}/fonts", @config['font_ext'])
end

#join_with_separator(value, sep) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/review/webmaker.rb', line 318

def join_with_separator(value, sep)
  if value.is_a?(Array)
    value.join(sep)
  else
    value
  end
end

#parse_opts(args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/review/webmaker.rb', line 50

def parse_opts(args)
  cmd_config = {}
  opts = OptionParser.new
  @buildonly = nil

  opts.banner = 'Usage: review-webmaker [option] configfile'
  opts.version = ReVIEW::VERSION
  opts.on('--help', 'Prints this message and quit.') do
    puts opts.help
    exit 0
  end
  opts.on('--ignore-errors', 'Ignore review-compile errors.') { cmd_config['ignore-errors'] = true }
  opts.on('-y', '--only file1,file2,...', 'Build only specified files.') { |v| @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') } }

  opts.parse!(args)
  if args.size != 1
    puts opts.help
    exit 0
  end

  [cmd_config, args[0]]
end

#recursive_copy_files(resdir, destdir, allow_exts) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/review/webmaker.rb', line 224

def recursive_copy_files(resdir, destdir, allow_exts)
  Dir.open(resdir) do |dir|
    dir.each do |fname|
      next if fname.start_with?('.')
      if FileTest.directory?("#{resdir}/#{fname}")
        recursive_copy_files("#{resdir}/#{fname}", "#{destdir}/#{fname}", allow_exts)
      elsif fname =~ /\.(#{allow_exts.join('|')})\Z/i
        FileUtils.mkdir_p(destdir)
        FileUtils.cp("#{resdir}/#{fname}", destdir)
      end
    end
  end
end

#remove_old_files(path) ⇒ Object



77
78
79
80
# File 'lib/review/webmaker.rb', line 77

def remove_old_files(path)
  cleanup_mathimg
  FileUtils.rm_rf(path)
end

#template_nameObject



173
174
175
176
177
178
179
# File 'lib/review/webmaker.rb', line 173

def template_name
  if @config['htmlversion'].to_i == 5
    'web/html/layout-html5.html.erb'
  else
    'web/html/layout-xhtml1.html.erb'
  end
end

#warn(msg) ⇒ Object



42
43
44
# File 'lib/review/webmaker.rb', line 42

def warn(msg)
  @logger.warn msg
end