class Giblish::FileTreeConverter

Parse a directory tree and convert all asciidoc files matching the supplied critera to the supplied format

Attributes

converter[R]

Public Class Methods

new(options) click to toggle source

Required options:

srcDirRoot
dstDirRoot
resourceDir
# File lib/giblish/core.rb, line 23
def initialize(options)
  @options = options.dup

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

  # set the path to the search data that will be sent to the cgi search script
  deploy_search_path = if @options[:makeSearchable]
                         if @options[:searchAssetsDeploy].nil?
                           @paths.search_assets_abs
                         else
                           Pathname.new(@options[:searchAssetsDeploy]).join("search_assets")
                         end
                       end

  @deploy_info = Giblish::DeploymentPaths.new(
    @options[:webPath],
    deploy_search_path
  )
  @processed_docs = []
  @converter = converter_factory
end

Public Instance Methods

convert() click to toggle source

convert all adoc files return true if all conversions went ok, false if at least one failed

# File lib/giblish/core.rb, line 53
def convert
  # collect all doc ids and enable replacement of known doc ids with
  # valid references to adoc files
  manage_doc_ids if @options[:resolveDocid]

  # register add-on for handling searchability
  manage_searchability(@options) if @options[:makeSearchable]

  # traverse the src file tree and convert all files deemed as
  # adoc files
  conv_error = false
  if @paths.src_root_abs.directory?
    Find.find(@paths.src_root_abs) do |path|
      p = Pathname.new(path)
      begin
        to_asciidoc(p) if adocfile? p
      rescue StandardError => e
        str = String.new("Error when converting file "\
                         "#{path}: #{e.message}\nBacktrace:\n")
        e.backtrace.each { |l| str << "   #{l}\n" }
        Giblog.logger.error { str }
        conv_error = true
      end
    end
  end

  # create necessary search assets if needed
  create_search_assets if @options[:makeSearchable]

  # build index and other fancy stuff if not suppressed
  unless @options[:suppressBuildRef]
    # build a dependency graph (only if we resolve docids...)
    dep_graph_exist = @options[:resolveDocid] && build_graph_page

    # build a reference index
    build_index_page(dep_graph_exist)
  end
  conv_error
end

Protected Instance Methods

add_doc(adoc, adoc_stderr) click to toggle source

creates a DocInfo instance, fills it with basic info and returns the filled in instance so that derived implementations can add more data

# File lib/giblish/core.rb, line 161
def add_doc(adoc, adoc_stderr)
  Giblog.logger.debug do
    "Adding adoc: #{adoc} Asciidoctor stderr: #{adoc_stderr}"
  end
  Giblog.logger.debug { "Doc attributes: #{adoc.attributes}" }

  info = DocInfo.new(adoc: adoc, dst_root_abs: @paths.dst_root_abs, adoc_stderr: adoc_stderr)
  @processed_docs << info
  info
end
add_doc_fail(filepath, exception) click to toggle source
# File lib/giblish/core.rb, line 172
def add_doc_fail(filepath, exception)
  info = DocInfo.new

  # the only info we have is the source file name
  info.converted = false
  info.src_file = filepath.to_s
  info.error_msg = exception.message

  @processed_docs << info
  info
end
build_graph_page() click to toggle source
# File lib/giblish/core.rb, line 95
def build_graph_page
  begin
    adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
    gb = graph_builder_factory
    errors = @converter.convert_str(
      gb.source(make_searchable: @options[:makeSearchable]),
      @paths.dst_root_abs,
      "graph",
      logger: adoc_logger
    )
    gb.cleanup
    !errors
  rescue StandardError => e
    Giblog.logger.warn { e.message }
    Giblog.logger.warn { "The dependency graph will not be generated !!" }
  end
  false
end
build_index_page(dep_graph_exist) click to toggle source
# File lib/giblish/core.rb, line 114
def build_index_page(dep_graph_exist)
  # build a reference index
  adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
  ib = index_factory
  @converter.convert_str(
    ib.source(
      dep_graph_exists: dep_graph_exist,
      make_searchable: @options[:makeSearchable]
    ),
    @paths.dst_root_abs,
    @options[:indexBaseName],
    logger: adoc_logger
  )

  # clean up cached files and adoc resources
  GC.start
end
converter_factory() click to toggle source

get the correct converter type

# File lib/giblish/core.rb, line 147
def converter_factory
  case @options[:format]
  when "html"
    HtmlConverter.new @paths, @deploy_info, @options
  when "pdf"
    PdfConverter.new @paths, @deploy_info, @options
  else
    raise ArgumentError, "Unknown conversion format: #{@options[:format]}"
  end
end
graph_builder_factory() click to toggle source
# File lib/giblish/core.rb, line 141
def graph_builder_factory
  Giblish::GraphBuilderGraphviz.new @processed_docs, @paths, @deploy_info,
                                    @converter.converter_options
end
index_factory() click to toggle source

get the correct index builder type depending on supplied user options

# File lib/giblish/core.rb, line 134
def index_factory
  raise "Internal logic error!" if @options[:suppressBuildRef]

  SimpleIndexBuilder.new(@processed_docs, @converter, @paths, @deploy_info,
                         @options[:resolveDocid])
end

Private Instance Methods

adocfile?(path) click to toggle source

predicate that decides if a path is a asciidoc file or not

# File lib/giblish/core.rb, line 198
def adocfile?(path)
  fs = path.to_s
  unless @options[:excludeRegexp].nil?
    # exclude file if user wishes
    er = Regexp.new @options[:excludeRegexp]
    return false unless er.match(fs).nil?
  end

  # only include files matching the include regexp
  ir = Regexp.new @options[:includeRegexp]
  !ir.match(fs).nil?
end
create_search_assets() click to toggle source

top_dir |- web_assets |- branch_1_top_dir | |- index.html | |- file1.html | |- dir_1 | | |- file2.html |- search_assets | |- branch_1 | |- heading_index.json | |- file1.adoc | |- dir_1 | | |- file2.html | |- … | |- branch_2 | | … |- branch_2_top_dir | …

# File lib/giblish/core.rb, line 245
def create_search_assets
  # get the proper dir for the search assets
  assets_dir = @paths.search_assets_abs

  # store the JSON file
  IndexHeadings.serialize assets_dir, @paths.src_root_abs

  # traverse the src file tree and copy all published adoc files
  # to the search_assets dir
  return unless @paths.src_root_abs.directory?

  Find.find(@paths.src_root_abs) do |path|
    p = Pathname.new(path)
    next unless adocfile? p

    dst_dir = assets_dir.join(@paths.reldir_from_src_root(p))
    FileUtils.mkdir_p(dst_dir)
    FileUtils.cp(p.to_s, dst_dir)
  end
end
manage_doc_ids() click to toggle source

Register the asciidoctor extension that handles doc ids and traverse the source tree to collect all :docid: attributes found in document headers.

# File lib/giblish/core.rb, line 269
def manage_doc_ids
  # Register the docid preprocessor hook
  Giblish.register_docid_extension

  # Make sure that no prior docid's are hangning around
  DocidCollector.clear_cache
  DocidCollector.clear_deps
  idc = DocidCollector.new

  # traverse the src file tree and collect ids from all
  # .adoc or .ADOC files
  if @paths.src_root_abs.directory?
    Find.find(@paths.src_root_abs) do |path|
      p = Pathname.new(path)
      idc.parse_file(p) if adocfile? p
    end
  end
  idc
end
manage_searchability(opts) click to toggle source
# File lib/giblish/core.rb, line 211
def manage_searchability(opts)
  # register the extension
  Giblish.register_index_heading_extension

  # make sure we start from a clean slate
  IndexHeadings.clear_index

  # propagate user-given id attributes to the indexing class
  # if there are any
  attr = opts[:attributes]
  return if attr.nil?

  IndexHeadings.id_elements[:id_prefix] = attr["idprefix"] if attr.key?("idprefix")
  IndexHeadings.id_elements[:id_separator] = attr["idseparator"] if attr.key?("idseparator")
end
to_asciidoc(filepath) click to toggle source

convert a single adoc doc to whatever the user wants

# File lib/giblish/core.rb, line 187
def to_asciidoc(filepath)
  adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
  adoc = @converter.convert(filepath, logger: adoc_logger)

  add_doc(adoc, adoc_logger.user_info_str.string)
rescue StandardError => e
  add_doc_fail(filepath, e)
  raise
end