Class: TreeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TreeConverter

Required options:

srcDirRoot
dstDirRoot
resourceDir


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/giblish/core.rb', line 238

def initialize(options)
  @options = options.dup

  @paths = Giblish::PathManager.new(
    @options[:srcDirRoot],
    @options[:dstDirRoot],
    @options[:resourceDir]
  )

  # init_dst_root

  # prepare the index page if requested
  unless @options[:suppressBuildRef]
    @index_builder = if @options[:gitRepoRoot]
                       GitRepoIndexBuilder.new(@paths,
                                               options[:resolveDocid],
                                               options[:gitRepoRoot])
                     else
                       SimpleIndexBuilder.new(@paths,
                                              options[:resolveDocid])
                     end
  end

  @conversion =
    case options[:format]
    when "html" then HtmlConverter.new options
    when "pdf" then PdfConverter.new options
    else
      raise ArgumentError, "Unknown conversion format: #{options[:format]}"
    end
end

Instance Method Details

#generate_index(src_str, dst_dir) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/giblish/core.rb', line 270

def generate_index(src_str, dst_dir)
  # use the same options as when converting all docs
  # in the tree but make sure we don't write to file
  index_opts = @conversion.converter_options.dup
  index_opts.delete(:to_file)
  index_opts.delete(:to_dir)

  # load and convert the document using the converter options
  doc = Asciidoctor.load src_str, index_opts
  output = doc.convert index_opts

  # write the converted document to an index file located at the
  # destination root
  index_filepath = dst_dir + "index.#{index_opts[:fileext]}"
  doc.write output, index_filepath.to_s
end

#to_asciidoc(filepath) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/giblish/core.rb', line 287

def to_asciidoc(filepath)
  adoc = nil
  begin
    # do the conversion and capture eventual errors that
    # the asciidoctor lib writes to stderr
    adoc_stderr = Giblish.with_captured_stderr do
      adoc = @conversion.convert filepath
    end

    # build the reference index if the user wants it
    @options[:suppressBuildRef] || @index_builder.add_doc(adoc, adoc_stderr)
  rescue Exception => e
    str = "Error when converting doc: #{e.message}\n"
    e.backtrace.each { |l| str << "#{l}\n" }
    Giblog.logger.error { str }
    @options[:suppressBuildRef] || @index_builder.add_doc_fail(filepath, e)
  end
end

#walk_dirsObject



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/giblish/core.rb', line 306

def walk_dirs
  # collect all doc ids and enable replacement of known doc ids with
  # valid references to adoc files
  manage_doc_ids if @options[:resolveDocid]

  # traverse the src file tree and convert all files that ends with
  # .adoc or .ADOC
  Find.find(@paths.src_root_abs) do |path|
    p = Pathname.new(path)
    to_asciidoc(p) if adocfile? p
  end

  # check if we shall build index or not
  return if @options[:suppressBuildRef]

  # build a reference index
  generate_index @index_builder.index_source, @paths.dst_root_abs

  # clean up adoc resources
  @index_builder = nil
  GC.start
end