Module: Pandocomatic::Path
- Defined in:
- lib/pandocomatic/path.rb
Overview
Pandocomatic related path functions. Paths in pandocomatic templates have various forms and behaviors. This behavior is defined by methods in this module.
Constant Summary collapse
- ROOT_PATH_INDICATOR =
Indicator for paths that should be treated as “relative to the root path”. These paths start with this ROOT_PATH_INDICATOR.
'$ROOT$'
Class Method Summary collapse
-
.absolute_path?(path) ⇒ Boolean
Is this path an absolute path in pandocomatic?.
-
.determine_root_path(options) ⇒ String
Determine the root path given a set of command-line options.
-
.local_path?(path) ⇒ Boolean
Is this path a local path in pandocomatic?.
-
.make_path_root_relative(path, dst, root) ⇒ String
Make a path root relative given the destination path and the root path.
-
.root_relative_path?(path) ⇒ Boolean
Is this path a root relative path in pandocomatic?.
-
.update_path(config, path, dst = '', check_executable: false) ⇒ String
Update the path to an executable processor or executor given this Configuration.
-
.which(cmd) ⇒ Object
Cross-platform way of finding an executable in the $PATH.
Class Method Details
.absolute_path?(path) ⇒ Boolean
Is this path an absolute path in pandocomatic?
100 101 102 103 104 105 106 |
# File 'lib/pandocomatic/path.rb', line 100 def self.absolute_path?(path) if Gem.win_platform? path.match('^[a-zA-Z]:\\\\.*$') else path.start_with? '/' end end |
.determine_root_path(options) ⇒ String
Determine the root path given a set of command-line options
120 121 122 123 124 125 126 127 128 |
# File 'lib/pandocomatic/path.rb', line 120 def self.determine_root_path() if [:root_path_given] [:root_path] elsif [:output_given] File.absolute_path(File.dirname([:output])) else File.absolute_path '.' end end |
.local_path?(path) ⇒ Boolean
Is this path a local path in pandocomatic?
88 89 90 91 92 93 94 |
# File 'lib/pandocomatic/path.rb', line 88 def self.local_path?(path) if Gem.win_platform? path.match('^\\.\\\\.*$') else path.start_with? './' end end |
.make_path_root_relative(path, dst, root) ⇒ String
Make a path root relative given the destination path and the root path
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/pandocomatic/path.rb', line 138 def self.make_path_root_relative(path, dst, root) # Find how to get to the root directopry from dst directory. # Assumption is that dst is a subdirectory of root. dst_dir = File.dirname(File.absolute_path(dst)) path.delete_prefix! ROOT_PATH_INDICATOR if root_relative_path? path if File.exist?(root) && File.realpath(dst_dir.to_s).start_with?(File.realpath(root)) rel_start = '' until File.identical?(File.realpath("#{dst_dir}/#{rel_start}"), File.realpath(root)) # invariant dst_dir/rel_start <= root rel_start += '../' end if rel_start.end_with?('/') && path.start_with?('/') "#{rel_start}#{path.delete_prefix('/')}" else "#{rel_start}#{path}" end else # Because the destination is not in a subdirectory of root, a # relative path to that root cannot be created. Instead, # the path is assumed to be absolute relative to root root = root.delete_suffix '/' if root.end_with? '/' path = path.delete_prefix '/' if path.start_with? '/' "#{root}/#{path}" end end |
.root_relative_path?(path) ⇒ Boolean
Is this path a root relative path in pandocomatic?
112 113 114 |
# File 'lib/pandocomatic/path.rb', line 112 def self.root_relative_path?(path) path.start_with? ROOT_PATH_INDICATOR end |
.update_path(config, path, dst = '', check_executable: false) ⇒ String
Update the path to an executable processor or executor given this Configuration
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pandocomatic/path.rb', line 43 def self.update_path(config, path, dst = '', check_executable: false) updated_path = path if local_path? path # refers to a local dir; strip the './' before appending it to # the source directory as to prevent /some/path/./to/path updated_path = path[2..] elsif absolute_path? path updated_path = path elsif root_relative_path? path updated_path = make_path_root_relative path, dst, config.root_path else updated_path = which path if check_executable if updated_path.nil? || !check_executable # refers to data-dir updated_path = File.join config.data_dir, path end end updated_path end |
.which(cmd) ⇒ Object
Cross-platform way of finding an executable in the $PATH.
which(‘ruby’) #=> /usr/bin/ruby
Taken from: stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby#5471032
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pandocomatic/path.rb', line 72 def self.which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end |