Parent

Namespace

Included Modules

Class/Module Index [+]

Quicksearch

Webgen::Path

General Information

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:

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!

Relation to Source classes

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.

Constants

FILENAME_RE

Attributes

basename[RW]

The canonical name of the path without the extension.

ext[RW]

The extension of the path.

meta_info[RW]

Extracted meta information for the path.

parent_path[R]

The string specifying the parent path

passive[W]

Specifies whether this path should be used during the "tree update" phase of a webgen run or only later during node resolution.

path[R]

The full path for which this Path object was created.

source_path[R]

A string specifying the path that lead to the creation of this path.

Public Class Methods

lcn(cn, lang) click to toggle source

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_absolute(base, path) click to toggle source

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
match(path, pattern) click to toggle source

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
new(path, source_path = path, &ioblock) click to toggle source

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

Public Instance Methods

<=>(other) click to toggle source

Compare the path of this object to other.path

# File lib/webgen/path.rb, line 229
def <=>(other)
  @path <=> other.path
end
==(other) click to toggle source

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
Also aliased as: eql?
acn() click to toggle source

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
alcn() click to toggle source

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
changed?() click to toggle source

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
cn() click to toggle source

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
dup() click to toggle source

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
eql?(other) click to toggle source
Alias for: ==
io() click to toggle source

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
lcn() click to toggle source

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_at(mp, prefix = nil) click to toggle source

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
passive?() click to toggle source

Is this path only used later during node resolution? Defaults to false, i.e. used during the "tree update" phase.

# File lib/webgen/path.rb, line 113
def passive?; @passive; end

Private Instance Methods

analyse(path) click to toggle source

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_directory() click to toggle source

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_file() click to toggle source

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_fragment() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.