lib/asciidoctor/abstract_node.rb in asciidoctor-1.5.1 vs lib/asciidoctor/abstract_node.rb in asciidoctor-1.5.2

- old
+ new

@@ -1,5 +1,6 @@ +# 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 # all content segments in an AsciiDoc document. class AbstractNode @@ -30,10 +31,13 @@ @document = parent else if parent @parent = parent @document = parent.document + else + @parent = nil + @document = nil end end @context = context @node_name = context.to_s # QUESTION are we correct in duplicating the attributes (seems to be just as fast) @@ -54,18 +58,22 @@ # Public: Returns whether this {AbstractNode} is an instance of {Inline} # # Returns [Boolean] def inline? + # :nocov: raise ::NotImplementedError + # :nocov: end # Public: Returns whether this {AbstractNode} is an instance of {Block} # # Returns [Boolean] def block? + # :nocov: raise ::NotImplementedError + # :nocov: end # Public: Get the value of the specified attribute # # Get the value for the specified attribute. First look in the attributes on @@ -267,15 +275,11 @@ # asset_dir_key - The String attribute key used to lookup the directory where # the media is located (default: 'imagesdir') # # Returns A String reference for the target media def media_uri(target, asset_dir_key = 'imagesdir') - if is_uri? target - target - else - normalize_web_path target, (asset_dir_key ? @document.attr(asset_dir_key) : nil) - end + normalize_web_path target, (asset_dir_key ? @document.attr(asset_dir_key) : nil) end # Public: Construct a URI reference or data URI to the target image. # # If the target image is a URI reference, then leave it untouched. @@ -296,22 +300,20 @@ # # Returns A String reference or data URI for the target image def image_uri(target_image, asset_dir_key = 'imagesdir') if (doc = @document).safe < SafeMode::SECURE && doc.attr?('data-uri') if is_uri?(target_image) || - (asset_dir_key && (images_base = doc.attr(asset_dir_key)) && - is_uri?(images_base) && (target_image = normalize_web_path target_image, images_base)) - if doc.attr? 'allow-uri-read' + (asset_dir_key && (images_base = doc.attr(asset_dir_key)) && is_uri?(images_base) && + (target_image = normalize_web_path(target_image, images_base, false))) + if doc.attr?('allow-uri-read') generate_data_uri_from_uri target_image, doc.attr?('cache-uri') else target_image end else generate_data_uri target_image, asset_dir_key end - elsif is_uri? target_image - target_image else normalize_web_path target_image, (asset_dir_key ? doc.attr(asset_dir_key) : nil) end end @@ -395,36 +397,50 @@ # Public: Read the contents of the file at the specified path. # This method assumes that the path is safe to read. It checks # that the file is readable before attempting to read it. # - # path - the String path from which to read the contents - # warn_on_failure - a Boolean that controls whether a warning is issued if - # the file cannot be read + # path - the String path from which to read the contents + # opts - a Hash of options to control processing (default: {}) + # * :warn_on_failure a Boolean that controls whether a warning + # is issued if the file cannot be read (default: false) + # * :normalize a Boolean that controls whether the lines + # are normalized and coerced to UTF-8 (default: false) # # Returns the [String] content of the file at the specified path, or nil # if the file does not exist. - def read_asset(path, warn_on_failure = false) + def read_asset(path, opts = {}) + # remap opts for backwards compatibility + opts = { :warn_on_failure => (opts != false) } unless ::Hash === opts if ::File.readable? path - # QUESTION should we use strip or rstrip instead of chomp here? - ::File.read(path).chomp + if opts[:normalize] + # QUESTION should we strip content? + Helpers.normalize_lines_from_string(::IO.read(path)) * EOL + else + ::IO.read(path) + end else - warn "asciidoctor: WARNING: file does not exist or cannot be read: #{path}" if warn_on_failure + warn %(asciidoctor: WARNING: file does not exist or cannot be read: #{path}) if opts[:warn_on_failure] nil end end # Public: Normalize the web page using the PathResolver. # # See {PathResolver#web_path} for details. # - # target - the String target path - # start - the String start (i.e, parent) path (optional, default: nil) + # target - the String target path + # start - the String start (i.e, parent) path (optional, default: nil) + # preserve_uri_target - a Boolean indicating whether target should be preserved if contains a URI (default: true) # # Returns the resolved [String] path - def normalize_web_path(target, start = nil) - (@path_resolver ||= PathResolver.new).web_path(target, start) + def normalize_web_path(target, start = nil, preserve_uri_target = true) + if preserve_uri_target && is_uri?(target) + target + else + (@path_resolver ||= PathResolver.new).web_path target, start + end end # Public: Resolve and normalize a secure path from the target and start paths # using the PathResolver. # @@ -449,20 +465,21 @@ # # Returns the [String] path resolved from the start and target paths, with any # parent references resolved and self references removed. If a jail is provided, # this path will be guaranteed to be contained within the jail. def normalize_system_path target, start = nil, jail = nil, opts = {} + path_resolver = (@path_resolver ||= PathResolver.new) if (doc = @document).safe < SafeMode::SAFE if start - start = ::File.join doc.base_dir, start unless (@path_resolver ||= PathResolver.new).is_root? start + start = ::File.join doc.base_dir, start unless path_resolver.is_root? start else start = doc.base_dir end else start = doc.base_dir unless start jail = doc.base_dir unless jail end - (@path_resolver ||= PathResolver.new).system_path target, start, jail, opts + path_resolver.system_path target, start, jail, opts end # Public: Normalize the asset file or directory to a concrete and rinsed path # # Delegates to normalize_system_path, with the start path set to the value of