A Path object provides information about a path that is used to create one or more nodes as well as methods for accessing the path’s content. So a Path object always refers to a source path. In contrast, output paths are always strings and just specify the location where a specific node should be written to.
Note the path and source_path attributes of a Path object:
The source_path specifies a path string that was directly created by a Source object. Each Path object must have such a valid source path sothat webgen can infer the Path the lead to the creation of a Node object later.
In contrast, the path attribute specifies the path that is used to create the canonical name (and by default the output path) of a Node object. Normally it is the same as the source_path but can differ (e.g. when fragment nodes are created for page file nodes).
A Path object can represent one of three different things: a directory, a file or a fragment. If the path ends with a slash character, then the path object represents a directory, if the path contains a hash character anywhere, then the path object represents a fragment and else it represents a file. Have a look at the webgen manual to see the exact format of a path!
A webgen source class needs to derive a specialized path class from this class and implement an approriate # method that returns true if the path’s content has changed since the last webgen run.
Utility method for creating the lcn from the cn and the language lang.
# File lib/webgen/path.rb, line 185 185: def self.lcn(cn, lang) 186: if lang.nil? 187: cn 188: else 189: cn.split('.').insert((cn =~ /^\./ ? 2 : 1), lang.to_s).join('.') 190: end 191: end
Make the given path absolute by prepending the absolute directory path base if necessary. Also resolves all ’..’ and ’.’ references in path.
# File lib/webgen/path.rb, line 75 75: def self.make_absolute(base, path) 76: raise(ArgumentError, 'base has to be an absolute path, ie. needs to start with a slash') unless base =~ /^\// 77: Pathname.new(path =~ /^\// ? path : File.join(base, path)).cleanpath.to_s + (path =~ /.\/$/ ? '/' : '') 78: end
Return true if the given path matches the given pattern. For information on which patterns are supported, have a look at the documentation of File.fnmatch.
# File lib/webgen/path.rb, line 82 82: def self.match(path, pattern) 83: pattern += '/' if path.to_s =~ /\/$/ && pattern !~ /\/$|^$/ 84: File.fnmatch(pattern, path.to_s, File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME) 85: end
Create a new Path object for path. The optional source_path parameter specifies the path string that lead to the creation of this path. The optional block needs to return an IO object for getting the content of the path.
The path needs to be in a well defined format which can be looked up in the webgen manual.
# File lib/webgen/path.rb, line 122 122: def initialize(path, source_path = path, &ioblock) 123: @meta_info = {} 124: @io = block_given? ? SourceIO.new(&ioblock) : nil 125: @source_path = source_path 126: @passive = false 127: analyse(path) 128: end
Compare the # of this object to other.path
# File lib/webgen/path.rb, line 230 230: def <=>(other) 231: @path <=> other.path 232: end
Equality — Return true if other is a Path object with the same # or if other is a String equal to the #. Else return false.
# File lib/webgen/path.rb, line 218 218: def ==(other) 219: if other.kind_of?(Path) 220: other.path == @path 221: elsif other.kind_of?(String) 222: other == @path 223: else 224: false 225: end 226: end
The absolute canonical name of this path.
# File lib/webgen/path.rb, line 199 199: def acn 200: if @path =~ /#/ 201: self.class.new(@parent_path).acn + cn 202: else 203: @parent_path + cn 204: end 205: end
The absolute localized canonical name of this path.
# File lib/webgen/path.rb, line 208 208: def alcn 209: if @path =~ /#/ 210: self.class.new(@parent_path).alcn + lcn 211: else 212: @parent_path + lcn 213: end 214: end
Has the content of this path changed since the last webgen run? This default implementation always returns true, a specialized sub class needs to override this behaviour!
# File lib/webgen/path.rb, line 166 166: def changed? 167: true 168: end
The canonical name created from the path (namely from the parts basename and extension).
# File lib/webgen/path.rb, line 180 180: def cn 181: @basename + (@ext.length > 0 ? '.' + @ext : '') + (@basename != '/' && @path =~ /.\/$/ ? '/' : '') 182: end
Duplicate the path object.
# File lib/webgen/path.rb, line 158 158: def dup 159: temp = super 160: temp.instance_variable_set(:@meta_info, @meta_info.dup) 161: temp 162: end
The SourceIO object associated with the path.
# File lib/webgen/path.rb, line 171 171: def io 172: if @io 173: @io 174: else 175: raise "No IO object defined for the path #{self}" 176: end 177: end
The localized canonical name created from the path.
# File lib/webgen/path.rb, line 194 194: def lcn 195: self.class.lcn(cn, @meta_info['lang']) 196: end
Mount this path at the mount point mp, optionally stripping prefix from the parent path, and return the new path object.
The parameters mp and prefix have to be absolute directory paths, ie. they have to start and end with a slash and must not contain any hash characters!
# File lib/webgen/path.rb, line 139 139: def mount_at(mp, prefix = nil) 140: raise(ArgumentError, "The mount point (#{mp}) must be a valid directory path") if mp =~ /^[^\/]|#|[^\/]$/ 141: raise(ArgumentError, "The strip prefix (#{prefix}) must be a valid directory path") if !prefix.nil? && prefix =~ /^[^\/]|#|[^\/]$/ 142: 143: temp = dup 144: strip_re = /^#{Regexp.escape(prefix.to_s)}/ 145: temp.instance_variable_set(:@path, temp.path.sub(strip_re, '')) 146: reanalyse = (@path == '/' || temp.path == '') 147: temp.instance_variable_set(:@path, File.join(mp, temp.path)) 148: temp.instance_variable_set(:@source_path, temp.path) if @path == @source_path 149: if reanalyse 150: temp.send(:analyse, temp.path) 151: else 152: temp.instance_variable_set(:@parent_path, File.join(mp, temp.parent_path.sub(strip_re, ''))) 153: end 154: temp 155: end
Analyse the path and fill the object with the extracted information.
# File lib/webgen/path.rb, line 252 252: def analyse(path) 253: @path = path 254: if @path =~ /#/ 255: analyse_fragment 256: elsif @path =~ /\/$/ 257: analyse_directory 258: else 259: analyse_file 260: end 261: @meta_info['title'] = @basename.tr('_-', ' ').capitalize 262: @ext ||= '' 263: raise "The basename of a path may not be empty: #{@path}" if @basename.empty? || @basename == '#' 264: raise "The parent path must start with a slash: #{@path}" if @path !~ /^\// && @path != '/' 265: end
Analyse the path assuming it is a directory.
# File lib/webgen/path.rb, line 268 268: def analyse_directory 269: @parent_path = (@path == '/' ? '' : File.join(File.dirname(@path), '/')) 270: @basename = File.basename(@path) 271: end
Analyse the path assuming it is a file.
# File lib/webgen/path.rb, line 276 276: def analyse_file 277: @parent_path = File.join(File.dirname(@path), '/') 278: match_data = FILENAME_RE.match(File.basename(@path)) 279: 280: if !match_data[1].nil? && match_data[3].nil? && match_data[4].nil? # handle special case of sort_info.basename as basename.ext 281: @basename = match_data[1] 282: @ext = match_data[2] 283: else 284: @meta_info['sort_info'] = (match_data[1].nil? ? nil : match_data[1].to_i) 285: @basename = match_data[2] 286: @meta_info['lang'] = Webgen::LanguageManager.language_for_code(match_data[3]) 287: @ext = (@meta_info['lang'].nil? && !match_data[3].nil? ? match_data[3].to_s + '.' : '') + match_data[4].to_s 288: end 289: end
Analyse the path assuming it is a fragment.
# File lib/webgen/path.rb, line 292 292: def analyse_fragment 293: @parent_path, @basename = @path.scan(/^(.*?)(#.*?)$/).first 294: raise "The parent path of a fragment path must be a file path and not a directory path: #{@path}" if @parent_path =~ /\/$/ 295: raise "A fragment path must only contain one hash character: #{path}" if @path.count("#") > 1 296: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.