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 changed? 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 184 def self.lcn(cn, lang) if lang.nil? cn else cn.split('.').insert((cn =~ /^\./ ? 2 : 1), lang.to_s).join('.') end 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 74 def self.make_absolute(base, path) raise(ArgumentError, 'base has to be an absolute path, ie. needs to start with a slash') unless base =~ /^\// Pathname.new(path =~ /^\// ? path : File.join(base, path)).cleanpath.to_s + (path =~ /.\/$/ ? '/' : '') 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 81 def self.match(path, pattern) pattern += '/' if path.to_s =~ /\/$/ && pattern !~ /\/$|^$/ File.fnmatch(pattern, path.to_s, File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME) 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 121 def initialize(path, source_path = path, &ioblock) @meta_info = {} @io = block_given? ? SourceIO.new(&ioblock) : nil @source_path = source_path @passive = false analyse(path) end
Compare the path of this object to other.path
# File lib/webgen/path.rb, line 229 def <=>(other) @path <=> other.path end
Equality -- Return true if other is a Path object with the same path or if other is a String equal to the path. Else return false.
# File lib/webgen/path.rb, line 217 def ==(other) if other.kind_of?(Path) other.path == @path elsif other.kind_of?(String) other == @path else false end end
The absolute canonical name of this path.
# File lib/webgen/path.rb, line 198 def acn if @path =~ /#/ self.class.new(@parent_path).acn + cn else @parent_path + cn end end
The absolute localized canonical name of this path.
# File lib/webgen/path.rb, line 207 def alcn if @path =~ /#/ self.class.new(@parent_path).alcn + lcn else @parent_path + lcn end 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 165 def changed? true end
The canonical name created from the path (namely from the parts basename and extension).
# File lib/webgen/path.rb, line 179 def cn @basename + (@ext.length > 0 ? '.' + @ext : '') + (@basename != '/' && @path =~ /.\/$/ ? '/' : '') end
Duplicate the path object.
# File lib/webgen/path.rb, line 157 def dup temp = super temp.instance_variable_set(:@meta_info, @meta_info.dup) temp end
The SourceIO object associated with the path.
# File lib/webgen/path.rb, line 170 def io if @io @io else raise "No IO object defined for the path #{self}" end end
The localized canonical name created from the path.
# File lib/webgen/path.rb, line 193 def lcn self.class.lcn(cn, @meta_info['lang']) 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 138 def mount_at(mp, prefix = nil) raise(ArgumentError, "The mount point (#{mp}) must be a valid directory path") if mp =~ /^[^\/]|#|[^\/]$/ raise(ArgumentError, "The strip prefix (#{prefix}) must be a valid directory path") if !prefix.nil? && prefix =~ /^[^\/]|#|[^\/]$/ temp = dup strip_re = /^#{Regexp.escape(prefix.to_s)}/ temp.instance_variable_set(:@path, temp.path.sub(strip_re, '')) reanalyse = (@path == '/' || temp.path == '') temp.instance_variable_set(:@path, File.join(mp, temp.path)) temp.instance_variable_set(:@source_path, temp.path) if @path == @source_path if reanalyse temp.send(:analyse, temp.path) else temp.instance_variable_set(:@parent_path, File.join(mp, temp.parent_path.sub(strip_re, ''))) end temp end
Analyse the path and fill the object with the extracted information.
# File lib/webgen/path.rb, line 251 def analyse(path) @path = path if @path =~ /#/ analyse_fragment elsif @path =~ /\/$/ analyse_directory else analyse_file end @meta_info['title'] = @basename.tr('_-', ' ').capitalize @ext ||= '' raise "The basename of a path may not be empty: #{@path}" if @basename.empty? || @basename == '#' raise "The parent path must start with a slash: #{@path}" if @path !~ /^\// && @path != '/' end
Analyse the path assuming it is a directory.
# File lib/webgen/path.rb, line 267 def analyse_directory @parent_path = (@path == '/' ? '' : File.join(File.dirname(@path), '/')) @basename = File.basename(@path) end
Analyse the path assuming it is a file.
# File lib/webgen/path.rb, line 275 def analyse_file @parent_path = File.join(File.dirname(@path), '/') match_data = FILENAME_RE.match(File.basename(@path)) if !match_data[1].nil? && match_data[3].nil? && match_data[4].nil? # handle special case of sort_info.basename as basename.ext @basename = match_data[1] @ext = match_data[2] else @meta_info['sort_info'] = (match_data[1].nil? ? nil : match_data[1].to_i) @basename = match_data[2] @meta_info['lang'] = Webgen::LanguageManager.language_for_code(match_data[3]) @ext = (@meta_info['lang'].nil? && !match_data[3].nil? ? match_data[3].to_s + '.' : '') + match_data[4].to_s end end
Analyse the path assuming it is a fragment.
# File lib/webgen/path.rb, line 291 def analyse_fragment @parent_path, @basename = @path.scan(/^(.*?)(#.*?)$/).first raise "The parent path of a fragment path must be a file path and not a directory path: #{@path}" if @parent_path =~ /\/$/ raise "A fragment path must only contain one hash character: #{path}" if @path.count("#") > 1 end
Generated with the Darkfish Rdoc Generator 2.