lib/webgen/plugins/filehandlers/directory.rb in webgen-0.3.8 vs lib/webgen/plugins/filehandlers/directory.rb in webgen-0.4.0
- old
+ new
@@ -1,9 +1,9 @@
#
#--
#
-# $Id: directory.rb 369 2005-12-24 11:37:40Z thomas $
+# $Id: directory.rb 548 2006-12-23 14:45:53Z thomas $
#
# webgen: template based static website generator
# Copyright (C) 2004 Thomas Leitner
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
@@ -18,106 +18,108 @@
# write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++
#
-require 'webgen/plugins/filehandlers/filehandler'
+load_plugin 'webgen/plugins/filehandlers/filehandler.rb'
+require 'webgen/node'
module FileHandlers
# Handles directories.
- class DirectoryHandler < DefaultFileHandler
+ class DirectoryHandler < DefaultHandler
- # Specialized node describing a directory.
+ # Specialized node for a directory.
class DirNode < Node
- def initialize( parent, name )
- super( parent )
- self['title'] = name
- self['src'] = self['dest'] = name + '/'
- self['processor'] = Webgen::Plugin['DirectoryHandler']
- self['int:directory?'] = true
+ def initialize( parent, path, meta_info = {} )
+ super( parent, path )
+ self.meta_info = meta_info
+ self['title'] = File.basename( path ).capitalize
end
def []( name )
- process_dir_index if !self.metainfo.has_key?( 'indexFile' ) && name == 'indexFile'
+ process_dir_index if name == 'indexFile' &&
+ (!self.meta_info.has_key?( 'indexFile' ) ||
+ (!self.meta_info['indexFile'].nil? && !self.meta_info['indexFile'].kind_of?( Node ) ) )
super
end
+ #######
+ private
+ #######
+
def process_dir_index
- indexFile = Webgen::Plugin['DirectoryHandler']['indexFile']
+ indexFile = self.meta_info['indexFile']
if indexFile.nil?
self['indexFile'] = nil
else
- node = node_for_string( indexFile )
+ node = resolve_node( indexFile )
if node
- self.logger.info { "Directory index file for <#{self.recursive_value( 'src' )}> => <#{node.recursive_value( 'src', false )}>" }
+ node_info[:processor].log(:info) { "Directory index file for <#{self.full_path}> => <#{node.full_path}>" }
self['indexFile'] = node
else
- self.logger.warn { "No directory index file found for directory <#{self.recursive_value( 'src' )}>" }
+ node_info[:processor].log(:warn) { "No directory index file found for directory <#{self.full_path}>" }
self['indexFile'] = nil
end
end
end
end
- summary "Handles directories"
- handle_path '**/'
- add_param 'indexFile', 'index.page', 'The default file name for the directory index page file.'
- depends_on 'FileHandler'
- used_meta_info 'indexFile', 'directoryName'
+ infos( :name => 'File/DirectoryHandler',
+ :author => Webgen::AUTHOR,
+ :summary => "Handles directories"
+ )
- # Return a new DirNode.
- def create_node( path, parent )
- unless parent && node = parent.find {|child| /^#{File.basename( path )}\/$/ =~ child['src'] }
- node = DirNode.new( parent, File.basename( path ) )
+ default_meta_info( { 'indexFile' => 'index.page' } )
+
+ register_path_pattern '**/', 0
+
+
+ # Returns a new DirNode.
+ def create_node( path, parent, meta_info )
+ filename = File.basename( path ) + '/'
+ if parent.nil? || (node = parent.find {|child| child =~ filename }).nil?
+ node = DirNode.new( parent, filename, meta_info )
+ node.node_info[:processor] = self
+ node.node_info[:src] = path
end
node
end
- # Create the directory (and all its parent directories if necessary).
+ # Creates the directory (and all its parent directories if necessary).
def write_node( node )
- name = node.recursive_value( 'dest' )
- FileUtils.makedirs( name ) unless File.exists?( name )
+ FileUtils.makedirs( node.full_path ) unless File.exists?( node.full_path )
end
# Return the page node for the directory +node+ using the specified language +lang+. If an
# index file is specified, then the its correct language node is returned, else +node+ is
# returned.
- def get_node_for_lang( node, lang )
- if node['indexFile']
- node['indexFile']['processor'].get_node_for_lang( node['indexFile'], lang )
- else
- node
- end
+ def node_for_lang( node, lang )
+ langnode = node['indexFile'].node_for_lang( lang ) if node['indexFile']
+ langnode || node
end
- # Get the HTML link for the directory +node+.
- def get_html_link( node, refNode, title = nil )
- lang_node = get_node_for_lang( node, refNode['lang'] )
- title ||= lang_node['directoryName'] || node['title']
- super( lang_node, refNode, title )
+ # See DefaultFileHandler#link_from and PageHandler#link_from.
+ def link_from( node, ref_node, attr = {} )
+ lang_node = (attr[:resolve_lang_node] == false ? node : node.node_for_lang( ref_node['lang'] ) )
+ attr[:link_text] ||= lang_node['directoryName'] || node['title']
+ super( lang_node, ref_node, attr )
end
- # Recursively create a given path
+ # Recursively creates a given directory path starting from the path of +parent+ and returns the
+ # bottom most directory node.
def recursive_create_path( path, parent )
- p = parent
- node = nil
path.split( File::SEPARATOR ).each do |pathname|
case pathname
- when '.' then
- # do nothing
- when '..' then
- p = p.parent
- else
- node = create_node( pathname, p )
- p.add_child( node ) unless p.nil? || node.nil?
- p = node
+ when '.' then #do nothing
+ when '..' then parent = parent.parent
+ else parent = @plugin_manager['Core/FileHandler'].create_node( pathname, parent, self )
end
end
- node
+ parent
end
end
end