module Asciidoctor::Helpers

def self.encode_uri(str)

returns an encoded version of the str

str - the string to encode

Public: Encode a string for inclusion in a URI
def self.encode_uri(str)
  str.gsub(REGEXP_ENCODE_URI_CHARS) do
    $&.each_byte.map {|c| sprintf '%%%02X', c}.join
  end
end

def self.mkdir_p(dir)

def self.mkdir_p(dir)
  unless ::File.directory? dir
    parent_dir = ::File.dirname(dir)
    if !::File.directory?(parent_dir = ::File.dirname(dir)) && parent_dir != '.'
      mkdir_p(parent_dir)
    end
    ::Dir.mkdir(dir)
  end
end

def self.normalize_lines data

returns a String Array of normalized lines

Delegates to Helpers#normalize_lines_array if data is a String Array.
Delegates to Helpers#normalize_lines_from_string if data is a String.

Public: Normalize the data to prepare for parsing
def self.normalize_lines data
  data.class == ::String ? (normalize_lines_from_string data) : (normalize_lines_array data)
end

def self.normalize_lines_array data

returns a String Array of normalized lines

data - a String Array of lines to normalize

is made to encode from the specified encoding to UTF-8.
If a BOM is present at the beginning of the data, a best attempt

Force encodes the data to UTF-8 and removes trailing whitespace from each line.

Public: Normalize the array of lines to prepare them for parsing
def self.normalize_lines_array data
  return [] if data.empty?
  # NOTE if data encoding is UTF-*, we only need 0..1
  leading_bytes = (first_line = data[0])[0..2].bytes.to_a
  if COERCE_ENCODING
    utf8 = ::Encoding::UTF_8
    if (leading_2_bytes = leading_bytes[0..1]) == BOM_BYTES_UTF_16LE
      # Ruby messes up trailing whitespace on UTF-16LE, so take a different route
      return ((data.join.force_encoding ::Encoding::UTF_16LE)[1..-1].encode utf8).lines.map {|line| line.rstrip }
    elsif leading_2_bytes == BOM_BYTES_UTF_16BE
      data[0] = (first_line.force_encoding ::Encoding::UTF_16BE)[1..-1]
      return data.map {|line| "#{((line.force_encoding ::Encoding::UTF_16BE).encode utf8).rstrip}" }
    elsif leading_bytes[0..2] == BOM_BYTES_UTF_8
      data[0] = (first_line.force_encoding utf8)[1..-1]
    end
    data.map {|line| line.encoding == utf8 ? line.rstrip : (line.force_encoding utf8).rstrip }
  else
    # Ruby 1.8 has no built-in re-encoding, so no point in removing the UTF-16 BOMs
    if leading_bytes == BOM_BYTES_UTF_8
      data[0] = first_line[3..-1]
    end
    data.map {|line| line.rstrip }
  end
end

def self.normalize_lines_from_string data

returns a String Array of normalized lines

data - a String of lines to normalize

is made to encode from the specified encoding to UTF-8.
If a BOM is present at the beginning of the data, a best attempt

Converts the data to a String Array.
Force encodes the data to UTF-8 and removes trailing whitespace from each line.

Public: Normalize the String and split into lines to prepare them for parsing
def self.normalize_lines_from_string data 
  return [] if data.nil_or_empty?
  if COERCE_ENCODING
    utf8 = ::Encoding::UTF_8
    # NOTE if data encoding is UTF-*, we only need 0..1
    leading_bytes = data[0..2].bytes.to_a
    if (leading_2_bytes = leading_bytes[0..1]) == BOM_BYTES_UTF_16LE
      data = (data.force_encoding ::Encoding::UTF_16LE)[1..-1].encode utf8
    elsif leading_2_bytes == BOM_BYTES_UTF_16BE
      data = (data.force_encoding ::Encoding::UTF_16BE)[1..-1].encode utf8
    elsif leading_bytes[0..2] == BOM_BYTES_UTF_8
      data = data.encoding == utf8 ? data[1..-1] : (data.force_encoding utf8)[1..-1]
    else
      data = data.force_encoding utf8 unless data.encoding == utf8
    end
  else
    # Ruby 1.8 has no built-in re-encoding, so no point in removing the UTF-16 BOMs
    if data[0..2].bytes.to_a == BOM_BYTES_UTF_8
      data = data[3..-1]
    end
  end
  data.each_line.map {|line| line.rstrip }
end

def self.require_library name, gem = true

otherwise Kernel#fail is called with an appropriate message.
returns the return value of Kernel#require if the library is available,

(default: true)
or the String name of the RubyGem if it differs from the library name
gem - a Boolean that indicates whether this library is provided by a RubyGem,
name - the String name of the library to require.

communicates that a required gem is not installed.
is being aborted. If a gem_name is specified, the failure message
passes a message to Kernel#fail to communicate to the user that processing
Kernel#require. Rescues the LoadError if the library is not available and
Attempts to load the library specified in the first argument using the

Internal: Require the specified library using Kernel#require.
def self.require_library name, gem = true
  require name
rescue ::LoadError => e
  if gem
    fail %(asciidoctor: FAILED: required gem '#{gem == true ? name : gem}' is not installed. Processing aborted.)
  else
    fail %(asciidoctor: FAILED: #{e.message.chomp '.'}. Processing aborted.)
  end
end

def self.rootname(file_name)

Returns the String filename with the file extension removed

# => "part1/chapter1"
Helpers.rootname('part1/chapter1.adoc')

Examples

file_name - The String file name to process

Public: Removes the file extension from filename and returns the result
def self.rootname(file_name)
  # alternatively, this could be written as ::File.basename file_name, ((::File.extname file_name) || '')
  (ext = ::File.extname(file_name)).empty? ? file_name : file_name[0...-ext.length]
end