lib/asciidoctor/abstract_node.rb in asciidoctor-1.5.7.1 vs lib/asciidoctor/abstract_node.rb in asciidoctor-1.5.8

- old
+ new

@@ -1,9 +1,9 @@ # encoding: UTF-8 module Asciidoctor # Public: An abstract base class that provides state and methods for managing a -# node of AsciiDoc content. The state and methods on this class are comment to +# node of AsciiDoc content. The state and methods on this class are common to # all content segments in an AsciiDoc document. class AbstractNode include Logging include Substitutors @@ -160,25 +160,36 @@ # return a Boolean indicating whether the option has been specified def option?(name) @attributes.key? %(#{name}-option) end - # TODO document me + # Public: Set the specified option on this node. + # + # This method sets the specified option on this node if not already set. + # It will add the name to the options attribute and set the <name>-option + # attribute. + # + # name - the String name of the option + # + # returns truthy if the option was set or falsey if the option was already set def set_option(name) - if @attributes.key? 'options' - @attributes['options'] = %(#{@attributes['options']},#{name}) + if (attrs = @attributes)['options'] + unless attrs[key = %(#{name}-option)] + attrs['options'] += %(,#{name}) + attrs[key] = '' + end else - @attributes['options'] = name + attrs['options'] = name + attrs[%(#{name}-option)] = '' end - @attributes[%(#{name}-option)] = '' end # Public: Update the attributes of this node with the new values in # the attributes argument. # # If an attribute already exists with the same key, it's value will - # be overridden. + # be overwritten. # # attributes - A Hash of attributes to assign to this node. # # Returns nothing def update_attributes(attributes) @@ -238,11 +249,11 @@ false elsif (val = val.split).delete name if val.empty? @attributes.delete('role') else - @attributes['role'] = val * ' ' + @attributes['role'] = val.join ' ' end true else false end @@ -356,11 +367,11 @@ # # Returns A String data URI containing the content of the target image def generate_data_uri(target_image, asset_dir_key = nil) ext = ::File.extname target_image # QUESTION what if ext is empty? - mimetype = (ext == '.svg' ? 'image/svg+xml' : %(image/#{ext[1..-1]})) + mimetype = (ext == '.svg' ? 'image/svg+xml' : %(image/#{ext.slice 1, ext.length})) if asset_dir_key image_path = normalize_system_path(target_image, @document.attr(asset_dir_key), nil, :target_name => 'image') else image_path = normalize_system_path(target_image) end @@ -398,13 +409,13 @@ ::OpenURI end begin mimetype = nil - bindata = open image_uri, 'rb' do |fd| - mimetype = fd.content_type - fd.read + bindata = open image_uri, 'rb' do |f| + mimetype = f.content_type + f.read end # NOTE base64 is autoloaded by reference to ::Base64 %(data:#{mimetype};base64,#{::Base64.strict_encode64 bindata}) rescue logger.warn %(could not retrieve image data from URI: #{image_uri}) @@ -497,11 +508,12 @@ def read_asset path, opts = {} # remap opts for backwards compatibility opts = { :warn_on_failure => (opts != false) } unless ::Hash === opts if ::File.readable? path if opts[:normalize] - Helpers.normalize_lines_from_string(::IO.read path) * LF + # NOTE Opal does not yet support File#readlines + (Helpers.normalize_lines_array ::File.open(path, 'rb') {|f| f.each_line.to_a }).join LF else # QUESTION should we chomp or rstrip content? ::IO.read path end elsif opts[:warn_on_failure] @@ -531,23 +543,26 @@ if (Helpers.uriish? target) || ((start = opts[:start]) && (Helpers.uriish? start) && (target = doc.path_resolver.web_path target, start)) if doc.attr? 'allow-uri-read' Helpers.require_library 'open-uri/cached', 'open-uri-cached' if doc.attr? 'cache-uri' begin - data = ::OpenURI.open_uri(target) {|fd| fd.read } - data = (Helpers.normalize_lines_from_string data) * LF if opts[:normalize] - return data + if opts[:normalize] + # NOTE Opal does not yet support File#readlines + (Helpers.normalize_lines_array ::OpenURI.open_uri(target) {|f| f.each_line.to_a }).join LF + else + ::OpenURI.open_uri(target) {|f| f.read } + end rescue logger.warn %(could not retrieve contents of #{opts[:label] || 'asset'} at URI: #{target}) if opts.fetch :warn_on_failure, true return end else logger.warn %(cannot retrieve contents of #{opts[:label] || 'asset'} at URI: #{target} (allow-uri-read attribute not enabled)) if opts.fetch :warn_on_failure, true return end else target = normalize_system_path target, opts[:start], nil, :target_name => (opts[:label] || 'asset') - return read_asset target, :normalize => opts[:normalize], :warn_on_failure => (opts.fetch :warn_on_failure, true), :label => opts[:label] + read_asset target, :normalize => opts[:normalize], :warn_on_failure => (opts.fetch :warn_on_failure, true), :label => opts[:label] end end # Internal: URI encode spaces in a String #