summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2018-04-04 00:55:38 -0600
committerDan Allen <dan.j.allen@gmail.com>2018-04-11 23:12:07 -0600
commit061cc583b5b264a5de994f5bb2b436f9a6291840 (patch)
tree81a1e31ce50b863b3a2bc765ef70324cd416196b
parent6f294271fcdecc68e51b15d9279734748a9d903f (diff)
apply a few cleanups to PathResolver
- change partition_path to return 2 elements instead of three - consolidate path split in partition_path - fold partition_path call into join_path in expand_path
-rw-r--r--lib/asciidoctor/path_resolver.rb40
1 files changed, 11 insertions, 29 deletions
diff --git a/lib/asciidoctor/path_resolver.rb b/lib/asciidoctor/path_resolver.rb
index 523ff064..553dea2e 100644
--- a/lib/asciidoctor/path_resolver.rb
+++ b/lib/asciidoctor/path_resolver.rb
@@ -225,8 +225,7 @@ class PathResolver
#
# returns a String path with any parent or self references resolved.
def expand_path path
- path_segments, path_root, _ = partition_path path
- join_path path_segments, path_root
+ join_path *partition_path(path)
end
# Public: Partition the path into path segments and remove any empty segments
@@ -237,11 +236,8 @@ class PathResolver
# web - a Boolean indicating whether the path should be handled
# as a web path (optional, default: false)
#
- # Returns a 3-item Array containing the Array of String path segments, the
- # path root (e.g., '/', './', 'c:/') if the path is absolute and the posix
- # version of the path.
- #--
- # QUESTION is it worth it to normalize slashes? it doubles the time elapsed
+ # Returns a 2-item Array containing the Array of String path segments and the
+ # path root (e.g., '/', './', 'c:/', or '//'), which is nil unless the path is absolute.
def partition_path path, web = nil
if (result = (cache = web ? @_partition_path_web : @_partition_path_sys)[path])
return result
@@ -275,24 +271,10 @@ class PathResolver
# else ex. sample/path
end
- path_segments = posix_path.split SLASH
- # shift twice for a UNC path
- if root == DOUBLE_SLASH
- path_segments = path_segments[2..-1]
- # shift twice for a file:/// path and adjust root
- # NOTE technically file:/// paths work without this adjustment
- #elsif ::RUBY_ENGINE_OPAL && ::JAVASCRIPT_IO_MODULE == 'xmlhttprequest' && root == 'file:/'
- # root = 'file://'
- # path_segments = path_segments[2..-1]
- # shift once for any other root
- elsif root
- path_segments.shift
- end
+ path_segments = (root ? (posix_path.slice root.length, posix_path.length) : posix_path).split SLASH
# strip out all dot entries
path_segments.delete DOT
- # QUESTION should we chop trailing /? (we pay a small fraction)
- #posix_path = posix_path.chop if posix_path.end_with? SLASH
- cache[path] = [path_segments, root, posix_path]
+ cache[path] = [path_segments, root]
end
# Public: Join the segments using the posix file separator (since Ruby knows
@@ -338,7 +320,7 @@ class PathResolver
if target.nil_or_empty?
target_segments = []
else
- target_segments, target_root, _ = partition_path target
+ target_segments, target_root = partition_path target
end
if target_segments.empty?
@@ -372,22 +354,22 @@ class PathResolver
# both jail and start have been posixfied at this point
if jail == start
- jail_segments, jail_root, _ = partition_path jail
+ jail_segments, jail_root = partition_path jail
start_segments = jail_segments.dup
elsif jail
unless start.start_with? jail
raise ::SecurityError, %(#{opts[:target_name] || 'Start path'} #{start} is outside of jail: #{jail} (disallowed in safe mode))
end
- start_segments, start_root, _ = partition_path start
- jail_segments, jail_root, _ = partition_path jail
+ start_segments, start_root = partition_path start
+ jail_segments, jail_root = partition_path jail
# Already checked for this condition
#if start_root != jail_root
# raise ::SecurityError, %(Jail root #{jail_root} does not match root of #{opts[:target_name] || 'start path'}: #{start_root})
#end
else
- start_segments, start_root, _ = partition_path start
+ start_segments, start_root = partition_path start
jail_root = start_root
end
@@ -452,7 +434,7 @@ class PathResolver
# end
#end
- target_segments, target_root, _ = partition_path target, true
+ target_segments, target_root = partition_path target, true
resolved_segments = []
target_segments.each do |segment|
if segment == DOT_DOT