Class CorePlugins::ResourceManager
In: lib/webgen/plugins/coreplugins/resourcemanager.rb
Parent: Webgen::Plugin
Webgen::Plugin ResourceManager Configuration lib/webgen/plugins/coreplugins/configuration.rb lib/webgen/plugins/coreplugins/resourcemanager.rb CorePlugins dot/m_40_0.png

Handles resources in webgen.

Methods

Classes and Modules

Class CorePlugins::ResourceManager::FileResource
Class CorePlugins::ResourceManager::MemoryResource
Class CorePlugins::ResourceManager::Resource

Attributes

resources  [R]  Returns all defined resources.

Public Class methods

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 151
151:     def initialize( plugin_manager )
152:       super
153:       @plugin_manager['Core/FileHandler'].add_msg_listener( :after_all_nodes_written, method( :write_resources ) )
154:       @resources = {}
155:       define_webgen_resources unless Webgen.data_dir.empty?
156:       define_user_resources
157:     end

Public Instance methods

Appends given data to the resource name. Data can only be appended to memory resources!

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 187
187:     def append_data( name, data )
188:       if (res = get_resource( name )) && res.type == :memory
189:         res.append_data( data )
190:       else
191:         log(:error) {"Resource #{name} does not exist or data cannot be appended to it!" }
192:       end
193:     end

Adds an exisiting file resource which can be referenced later by using name. The output_path should be an absolute path, like +/images/logo.png+. If not, it will be relative to the output directory.

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 163
163:     def define_file_resource( name, resource_path, output_path )
164:       if File.exists?( resource_path )
165:         define_resource( name, FileResource.new( name, output_path, resource_path ) )
166:       end
167:     end

Adds a new resource which can be referenced later by using name. The output_path should be an absolute path, like +/images/logo.png+. If not, it will be relative to the output directory.

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 172
172:     def define_memory_resource( name, output_path )
173:       define_resource( name, MemoryResource.new( name, output_path ) )
174:     end

Assigns the resource res the name name.

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 177
177:     def define_resource( name, res )
178:       @resources[name] = res unless @resources.has_key?( name )
179:     end

Returns the requested resource.

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 182
182:     def get_resource( name )
183:       @resources[name]
184:     end

Private Instance methods

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 247
247:     def define_user_resources
248:       p = param( 'resources' )
249:       if !p.kind_of?( Array ) || p.find {|h| !h.kind_of?( Array ) || h.length != 3}
250:         log(:error) { "Parameter 'resources' not correctly structured!" }
251:         return
252:       end
253:       p.each {|name, res_path, out_path| define_file_resource( name, res_path, out_path ) }
254:     end

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 222
222:     def define_webgen_emoticons
223:       Dir.glob(File.join( Webgen.data_dir, 'resources', 'emoticons', '*/'), File::FNM_CASEFOLD ).each do |pack_dir|
224:         pack = File.basename( pack_dir )
225:         Dir.glob( File.join( pack_dir, '*' ), File::FNM_CASEFOLD ).each do |smiley_file|
226:           smiley = File.basename( smiley_file, '.*' )
227:           res = define_file_resource( "webgen-emoticons-#{pack}-#{smiley}",
228:                                       smiley_file,
229:                                       "/images/emoticons/#{pack}-#{File.basename(smiley_file)}" )
230:           res.predefined = "Emoticon from pack '#{pack}' for '#{smiley}'"
231:         end
232:       end
233:     end

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 235
235:     def define_webgen_icons
236:       base_dir = File.join( Webgen.data_dir, 'resources', 'icons' )
237:       Dir.glob( File.join( base_dir, '**/*'), File::FNM_CASEFOLD ).each do |icon|
238:         dirs = File.dirname( icon ).sub( /^#{base_dir}/, '' ).split( '/' ).join( '-' )
239:         dirs += '-' if dirs.length > 0
240:         res = define_file_resource( "webgen-icons-#{dirs}#{File.basename( icon, '.*' )}",
241:                                     icon,
242:                                     "/images/icons/#{dirs}#{File.basename(icon)}" )
243:         res.predefined = "Icon named #{File.basename(icon)}"
244:       end
245:     end

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 199
199:     def define_webgen_resources
200:       define_file_resource( 'webgen-logo', File.join( Webgen.data_dir, 'resources', 'images', 'webgen_logo.png' ),
201:                             '/images/webgen-logo.png'
202:                             ).predefined = "The logo of webgen as seen on the homepage."
203:       define_file_resource( 'webgen-generated', File.join( Webgen.data_dir, 'resources', 'images', 'generated_by_webgen.png' ),
204:                             '/images/webgen-generated-by.png'
205:                             ).predefined = "A 88x31 image for use on web sites that were generated by webgen."
206:       define_file_resource( 'w3c-valid-css', File.join( Webgen.data_dir, 'resources', 'images', 'valid-css.gif' ),
207:                             '/images/w3c-valid-css.gif'
208:                             ).predefined = 'The W3C image for valid css.'
209:       define_file_resource( 'w3c-valid-xhtml11', File.join( Webgen.data_dir, 'resources', 'images', 'valid-xhtml11.png' ),
210:                             '/images/w3c-valid-xhtml11.png'
211:                             ).predefined = "The W3C image for valid XHTML1.1"
212: 
213:       define_webgen_emoticons
214:       define_webgen_icons
215: 
216:       define_memory_resource( 'webgen-css', '/css/webgen.css'
217:                               ).predefined = "Plugins use this resource for adding their CSS styles."
218:       define_memory_resource( 'webgen-javascript', '/js/webgen.js'
219:                               ).predefined = "Plugins use this resource for adding their Javascript fragments."
220:     end

[Source]

     # File lib/webgen/plugins/coreplugins/resourcemanager.rb, line 257
257:     def write_resources( root )
258:       outDir = @plugin_manager.param_for_plugin( 'Core/Configuration', 'outDir' )
259: 
260:       @resources.each do |name, res|
261:         if res.write_resource?( outDir, @plugin_manager['Core/FileHandler'] )
262:           begin
263:             FileUtils.makedirs( File.dirname( res.dest_path( outDir ) ) )
264:             res.write_resource( outDir, @plugin_manager['Core/FileHandler'] )
265:             log(:info) { "Resource '#{name}' written to <#{res.dest_path( outDir )}>" }
266:           rescue Exception => e
267:             log(:error) { "Error while writing resource '#{name}': #{e.message}" }
268:           end
269:         end
270:       end
271:     end

[Validate]