lib/asciidoctor/helpers.rb in asciidoctor-1.5.6.2 vs lib/asciidoctor/helpers.rb in asciidoctor-1.5.7

- old
+ new

@@ -14,43 +14,45 @@ # gem_name - a Boolean that indicates whether this library is provided by a RubyGem, # or the String name of the RubyGem if it differs from the library name # (default: true) # on_failure - a Symbol that indicates how to handle a load failure (:abort, :warn, :ignore) (default: :abort) # - # returns The return value of Kernel#require if the library is available and can be, or was previously, loaded. - # Otherwise, Kernel#raise is called with an appropriate message if on_failure is :abort. - # Otherwise, Kernel#warn is called with an appropriate message and nil returned if on_failure is :warn. + # Returns The [Boolean] return value of Kernel#require if the library can be loaded. + # Otherwise, if on_failure is :abort, Kernel#raise is called with an appropriate message. + # Otherwise, if on_failure is :warn, Kernel#warn is called with an appropriate message and nil returned. # Otherwise, nil is returned. def self.require_library name, gem_name = true, on_failure = :abort require name rescue ::LoadError => e + include Logging unless include? Logging if gem_name gem_name = name if gem_name == true case on_failure when :abort raise ::LoadError, %(asciidoctor: FAILED: required gem '#{gem_name}' is not installed. Processing aborted.) when :warn - warn %(asciidoctor: WARNING: optional gem '#{gem_name}' is not installed. Functionality disabled.) + logger.warn %(optional gem '#{gem_name}' is not installed. Functionality disabled.) end else case on_failure when :abort raise ::LoadError, %(asciidoctor: FAILED: #{e.message.chomp '.'}. Processing aborted.) when :warn - warn %(asciidoctor: WARNING: #{e.message.chomp '.'}. Functionality disabled.) + logger.warn %(#{e.message.chomp '.'}. Functionality disabled.) end end + nil end # Public: Normalize the data to prepare for parsing # # Delegates to Helpers#normalize_lines_from_string if data is a String. # Delegates to Helpers#normalize_lines_array if data is a String Array. # # returns a String Array of normalized lines def self.normalize_lines data - data.class == ::String ? (normalize_lines_from_string data) : (normalize_lines_array data) + ::String === data ? (normalize_lines_from_string data) : (normalize_lines_array data) end # Public: Normalize the array of lines to prepare them for parsing # # Force encodes the data to UTF-8 and removes trailing whitespace from each line. @@ -193,16 +195,36 @@ else ::File.basename filename 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) + unless (parent_dir = ::File.dirname dir) == '.' + mkdir_p parent_dir end - ::Dir.mkdir(dir) + begin + ::Dir.mkdir dir + rescue ::SystemCallError + raise unless ::File.directory? dir + end end + end + + ROMAN_NUMERALS = { + 'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, + 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1 + } + + # Converts an integer to a Roman numeral. + # + # val - the [Integer] value to convert + # + # Returns the [String] roman numeral for this integer + def self.int_to_roman val + ROMAN_NUMERALS.map {|l, i| + repeat, val = val.divmod i + l * repeat + }.join end end end