class Giblish::HtmlConverter

Converts asciidoc files to html5 output.

Public Class Methods

new(paths, deployment_info, options) click to toggle source
Calls superclass method Giblish::DocConverter::new
# File lib/giblish/docconverter.rb, line 167
def initialize(paths, deployment_info, options)
  super paths, deployment_info, options

  # validate that things are ok on the resource front
  # and copy if needed
  @dst_asset_dir = @paths.dst_root_abs.join("web_assets")
  validate_and_copy_resources @dst_asset_dir

  # identify ourselves as an html converter
  add_backend_options({ backend: "html5", fileext: "html" })
  # setup the attributes specific for this converter
  add_backend_attributes(common_attributes)
end

Protected Instance Methods

add_doc_specific_attributes(filepath, is_src_file, attributes) click to toggle source
# File lib/giblish/docconverter.rb, line 183
def add_doc_specific_attributes(filepath, is_src_file, attributes)
  doc_attrib = {}
  if @paths.resource_dir_abs
    # user has given a resource dir, use the css from that dir
    doc_attrib.merge!(
      {
        "linkcss" => 1,
        "stylesheet" => @user_style ||= "giblish.css",
        "copycss!" => 1
      }
    )
    if @deployment_info.web_path.nil?
      # user wants to deploy without web server, the css
      # link shall thus be the relative path from the
      # generated doc to the css directory
      dst_css_dir = @dst_asset_dir.join("css")
      css_rel_dir = if is_src_file
                      # the filepath is a src path
                      @paths.relpath_to_dir_after_generate(
                        filepath,
                        dst_css_dir
                      )
                    else
                      # the given file path is the destination path of
                      # the generated file, find the relative path to the
                      # css dir
                      dst_dir = PathManager.closest_dir(filepath)
                      dst_css_dir.relative_path_from(dst_dir)
                    end
      doc_attrib["stylesdir"] = css_rel_dir.to_s
    else
      # user has given a web deployment path, the css shall then
      # be linked using that path
      doc_attrib["stylesdir"] = @deployment_info.web_path.join("css").cleanpath.to_s
    end
  end
  Giblog.logger.debug { "Rendered docs expect a css at: #{doc_attrib['stylesdir']}" }
  Giblog.logger.debug { "The expected css is named: #{doc_attrib['stylesheet']}" }

  attributes.merge!(doc_attrib)
end

Private Instance Methods

common_attributes() click to toggle source
# File lib/giblish/docconverter.rb, line 227
def common_attributes
  # Setting 'data-uri' makes asciidoctor embed images in the resulting
  # html file
  {
    "data-uri" => 1
  }
end
copy_resource_dir(dst_dir) click to toggle source
# File lib/giblish/docconverter.rb, line 235
def copy_resource_dir(dst_dir)
  # create assets_dir and copy everything in the resource dir
  # to the destination
  Dir.exist?(dst_dir) || FileUtils.mkdir_p(dst_dir)

  # copy all subdirs that exist in the source tree to the
  # dst tree
  %i[css fonts images].each do |dir|
    src = "#{@paths.resource_dir_abs}/#{dir}"
    Dir.exist?(src) && FileUtils.copy_entry(src, "#{dst_dir}/#{dir}")
  end
end
validate_and_copy_resources(dst_dir) click to toggle source

make as sure as we can that the user has given a directory with valid resources

# File lib/giblish/docconverter.rb, line 250
def validate_and_copy_resources(dst_dir)
  # we don't have a resource path, which is fine, use
  # defaults
  return nil unless @paths.resource_dir_abs

  # If user has requested the use of a specific css, use that,
  # otherwise use asciidoctor default css
  if @user_style
    # Make sure that a user supplied stylesheet ends with .css or .CSS
    @user_style && @user_style =
                     /\.(css|CSS)$/ =~ @user_style ? @user_style : "#{@user_style}.css"

    # bail out if we can not find the given css file
    src_css_path = @paths.resource_dir_abs
                         .join("css").join(Pathname.new(@user_style))
    unless src_css_path.exist?
      raise "Could not find the specified "\
            "css file at: #{src_css_path}"
    end
  end

  copy_resource_dir dst_dir
end