lib/dokkit/environment/basic.rb in dokkit-0.4.4 vs lib/dokkit/environment/basic.rb in dokkit-0.5.0

- old
+ new

@@ -1,179 +1,205 @@ # # File 'basic.rb' created on 01 mag 2008 at 15:37:33. # -# See 'dokkit.rb' or +LICENSE+ for licence information. +# See 'dokkit.rb' or +LICENSE+ for license information. # # (C)2006-2008 Andrea Fazzi <andrea.fazzi@alca.le.it> (and contributors). # require 'ostruct' require 'dokkit' require 'dokkit/environment' +require 'dokkit/environment/helpers' require 'dokkit/logging' require 'dokkit/factory' require 'dokkit/resource' require 'dokkit/tasklib' require 'dokkit/filters' require 'dokkit/cache' module Dokkit module Environment - module Basic - # When included in a container, initialize it. - def initialize + def environment(&blk) Basic.new(&blk); end + + class Basic < Container + + include Dokkit::Environment::Helper + + def initialize(&blk) + super add_lib_to_load_path - yield self if block_given? + + init_extmap + init_services + + yield self if block_given? + define_tasks end - - # Add lib folder to the load path. - def add_lib_to_load_path - $: << 'lib' - end - - # Define rake tasks for the environment. + def define_tasks render clean end - - # Setup a console logger. - def logger - @logger ||= Logging::Observer::Console.logger + + def init_services + methods.select { |meth| meth =~ /register_/ }.each { |meth| send(meth) } end - - # Setup cache. - def cache - @cache ||= Cache.new + + def register_document_fs + register :document_fs do + Environment::Helper::FileSelection.new(configuration.document_dir) do |fs| + fs.include('**/*') + fs.exclude('**/*.yaml') + end + end end + + def register_data_fs + register :data_fs do + Environment::Helper::FileSelection.new(configuration.data_dir) do |fs| + fs.include('**/*') + end + end + end + + def register_extmap + register :extmap do + Helper::ExtMap.new(configuration.document_dir) + end + end + + def register_filter_factory + register :filter_factory do + Dokkit::Factory.new do |factory| + factory.add('nil') { Dokkit::Filter::Nil.new } + factory.add('erb') { |document| Dokkit::Filter::ERB.new(document) } + factory.add('yaml') { |document| Dokkit::Filter::YAML.new(document) } + factory.add('tidy') { Dokkit::Filter::Tidy.new } + factory.add('maruku-html') { Dokkit::Filter::MarukuHTML.new } + factory.add('deplate-latex') { Dokkit::Filter::DeplateLatex.new } + factory.add('deplate-html') { Dokkit::Filter::DeplateHTML.new } + factory.add('deplate-text') { Dokkit::Filter::DeplateText.new } + factory.add('haml') { |document| Dokkit::Filter::Haml.new(document) } + end + + end + end + + def register_resource_factory + register :resource_factory do + Factory.new do |factory| + factory.add(:document, &document_factory_block) + factory.add(:data, &data_factory_block) + end + end + end + + def register_cache + register :cache do + Cache.new + end + end + + def register_render + register :render do + TaskLib::Render.new do |task| + task.logger = logger + task.resource_factory = resource_factory + task.document_fns = document_fs.files + task.data_fns = data_fs.files + end + end + end + + def register_clean + register :clean do + TaskLib::Clean.new do |task| + task.logger = logger + task.output_dir = configuration.output_dir + task.cache_dir = configuration.cache_dir + end + end + end + + def register_logger + register :logger do + Logging::Observer::Console.logger + end + end + + def register_configuration + register :configuration do + OpenStruct.new(default_configuration) + end + end + + # Add lib folder to the load path. + def add_lib_to_load_path + $: << 'lib' + end + def init_extmap + @extmap = { } + end + def default_filter_chain { 'deplate' => { 'html' => ['erb', 'deplate-html'], 'latex' => ['erb', 'deplate-latex'], 'text' => ['erb', 'deplate-text']}, - 'maruku' => { 'html' => ['erb', 'maruku-html'], 'latex' => ['erb', 'maruku-latex'], 'text' => ['erb', 'maruku-text']}, + 'maruku' => { 'html' => ['erb', 'maruku-html'], 'latex' => ['erb', 'maruku-latex'] }, 'haml' => { 'html' => ['haml'] }, } end def default_postfilter_chain { 'html' => ['erb'], 'latex' => ['erb'], 'text' => ['erb'] } end - - # Return an OpenStruct object to setup documentation - # directories. Instantiate the object only once. - # - # configuration.document_dir :: set/get documents folder. - # configuration.config_dir :: set/get configs folder. - # configuration.layout_dir :: set/get layouts folder. - # configuration.data_dir :: set/get data folder. - # configuration.output_dir :: set/get output folder. - # configuration.cache_dir :: set/get cache folder. - # - def configuration - @configuration ||= OpenStruct.new({ :document_dir => 'doc/pages', - :config_dir => 'doc/configs', - :layout_dir => 'doc/layouts', - :data_dir => 'doc/data', - :output_dir => 'output', - :cache_dir => '.cache', - :default_filter_chain => default_filter_chain, - :default_postfilter_chain => default_postfilter_chain }) - end - - # Return a Dokkit::Environment::Helper::FileSelection object - # with the list of documents in configuration.document_dir - # folder and its subdir. The object is instantiated only - # once. By default all files in configuration.document_dir and - # subdirs are included. - def documents - @documents ||= Environment::Helper::FileSelection.new(configuration.document_dir) do |fs| - fs.include('**/*') - fs.exclude('**/*.yaml') - end + def default_configuration + { + :document_dir => 'doc/pages', + :config_dir => 'doc/configs', + :layout_dir => 'doc/layouts', + :data_dir => 'doc/data', + :output_dir => 'output', + :cache_dir => '.cache', + :default_filter_chain => default_filter_chain, + :default_postfilter_chain => default_postfilter_chain + } end - # Return a Dokkit::Environment::Helper::FileSelection object - # with the list of data files in configuration.data_dir folder - # and its subdir. The object is instantiated only once. - def data - @data ||= Environment::Helper::FileSelection.new(configuration.data_dir).include('**/*') - end - - # Return a Dokkit::Environment::Helper::ExtMap object that map - # an extension code block to document files using glob patterns. - def extmap - @extmap ||= Helper::ExtMap.new(configuration.document_dir) - end - - # Construct a filter factory and register filters. - def filter_factory - @filter_factory ||= Dokkit::Factory.new do |factory| - factory.add('nil' => lambda { Dokkit::Filter::Nil.new } ) - factory.add('erb' => lambda { |binding| Dokkit::Filter::ERB.new(binding) } ) - factory.add('tidy' => lambda { Dokkit::Filter::Tidy.new } ) - factory.add('maruku-html' => lambda { Dokkit::Filter::MarukuHTML.new } ) - factory.add('deplate-latex' => lambda { Dokkit::Filter::DeplateLatex.new } ) - factory.add('deplate-html' => lambda { Dokkit::Filter::DeplateHTML.new } ) - factory.add('deplate-text' => lambda { Dokkit::Filter::DeplateText.new } ) - factory.add('haml' => lambda { |document| Dokkit::Filter::Haml.new(document) } ) + def document + register :document do + Resource::Document.new(source_fn, configuration.marshal_dump) do |document| + document.logger = logger + document.cache = cache + document.resource_factory = resource_factory + document.filter_factory = filter_factory + end end end - - # Construct a resource factory to get resource instances - # (i.e. document and data). The object is instantiated only - # once. - def resource_factory - @resource_factory ||= Factory.new do |factory| - factory.add(:document => document_factory_block) - factory.add(:data => data_factory_block) - end - end - + # Return a block that is able to construct a Document instance. def document_factory_block lambda do |source_fn| - Resource::Document.new(source_fn, - configuration.marshal_dump, - logger, - cache, - resource_factory, - filter_factory, - &extmap[source_fn]) + Resource::Document.new(source_fn, configuration.marshal_dump) do |document| + document.logger = logger + document.cache = cache + document.resource_factory = resource_factory + document.filter_factory = filter_factory + document.extend @extmap[source_fn] if @extmap[source_fn] + end end end - - # Return a block that is able to construct a Data instance. + def data_factory_block lambda do |source_fn| Resource::Data.new(source_fn, configuration.marshal_dump) end end - # Define render tasklib. - def render - TaskLib::Render.new(logger, resource_factory, documents.files, data.files) - end - - # Define clean tasklib. - def clean - TaskLib::Clean.new(logger, configuration.marshal_dump) - end - end - end -end - -module Dokkit - module Environment - module Basic - - # Define a setup container. - class Container - include Dokkit::Environment::Basic - end - - end + end end