lib/dokkit/resource/document.rb in dokkit-0.4.0 vs lib/dokkit/resource/document.rb in dokkit-0.4.1

- old
+ new

@@ -23,15 +23,19 @@ # Includes the builtin extensions to be used by Document objects. include Extension::Builtin # Set the default filters chain for commonly used output format. DEFAULT_FILTERS_CHAIN = { - 'html' => ['deplate-html'], - 'latex' => ['deplate-latex'], - 'text' => ['deplate-text'] + 'html' => ['erb', 'deplate-html'], + 'latex' => ['erb', 'deplate-latex'], + 'text' => ['erb', 'deplate-text'] } - + DEFAULT_POST_FILTERS_CHAIN = { + 'html' => ['erb'], + 'latex' => ['erb'], + 'text' => ['erb'] + } attr_reader :configuration attr_reader :source_fn attr_reader :basename, :name_noext, :dirname, :relativename attr_reader :config_fns, :layout_fns, :target_fns attr_reader :targets, :layouts, :deps @@ -119,11 +123,26 @@ DEFAULT_FILTERS_CHAIN[format] else @logger.error("No defined filters chain for format '#{format}'!") end end - + + # Return the post filters chain associated with the given format. + def post_filters_for(format) + if @targets.has_key?(format) + if @targets[format].has_key?('postfilter') + @targets[format]['postfilter'] + elsif DEFAULT_POST_FILTERS_CHAIN.has_key?(format) + DEFAULT_POST_FILTERS_CHAIN[format] + end + elsif DEFAULT_POST_FILTERS_CHAIN.has_key?(format) + DEFAULT_POST_FILTERS_CHAIN[format] + else + @logger.error("No defined post filters chain for format '#{format}'!") + end + end + def target_for(format) @targets[format][:target_fn] end def deps_for(format) @@ -213,30 +232,22 @@ @content_for_layout end # Produce output from source. def render_source!(format) - unless @source.nil? - erb = ERB.new(@source) - erb.filename = @source_fn - @content_for_layout = apply_filters(erb.result(binding), format) - end + @content_for_layout = apply_filters(@source, filters_for(format), format) unless @source.nil? end + def render_layout!(layout_fn, format) + @content_for_layout = apply_filters(File.read(layout_fn), post_filters_for(format), format) unless File.read(layout_fn).nil? + end + # Injects rendered content from +source_fn+ in the layout chain. def render_all_layouts!(format) - unless !@layouts[format] || @layouts[format].empty? - @layouts[format].each { |layout_fn| render_layout!(layout_fn) } - end + @layouts[format].each { |layout_fn| render_layout!(layout_fn, format) } unless !@layouts[format] || @layouts[format].empty? end - def render_layout!(layout_fn) - erb = ERB.new(File.read(layout_fn)) - erb.filename = layout_fn - @content_for_layout = erb.result(binding) - end - # Collect all dependencies. def collect_all collect_formats collect_layouts collect_deps @@ -257,11 +268,11 @@ # Collect the layout files traversing +targets+ hash. def collect_layouts @targets.each_key do |format| @layouts[format] = [] - + process_layout_configuration_key(format) @layouts[format].uniq! @layouts[format].compact! end @@ -290,11 +301,12 @@ format_key = format.keys.first opts = format.values.first if opts ext = (opts['ext'] if opts.has_key?('ext')) || format_key filters = (opts['filter'] if opts.has_key?('filter')) || DEFAULT_FILTERS_CHAIN['html'] - { :target_fn => target_fn(ext.to_sym), 'filter' => filters } + post_filters = (opts['postfilter'] if opts.has_key?('postfilter')) || DEFAULT_POST_FILTERS_CHAIN['html'] + { :target_fn => target_fn(ext.to_sym), 'filter' => filters, 'postfilter' => post_filters } else @logger.error("You must define format '#{format}'!") end end @@ -364,20 +376,11 @@ def target_fn(format) filename_helper(@name_noext, @configuration[:document_dir], @configuration[:output_dir], ".#{format.to_s}") end # Apply filters on text to produce the given format. - def apply_filters(text, format) - filters_chain = @targets[format]['filter'] || DEFAULT_FILTERS_CHAIN[format] - filters = [] - if filters_chain - filters_chain.each do |filter| - filters << @filter_factory.get(filter) - end - filters.inject(text) { |s, f| f.filter(s) } - else - @logger.error("Don't know how to render '#{source_fn}': cannot find filters for format '#{format}'!") - end + def apply_filters(text, filters_chain, format) + filters_chain.collect { |filter| @filter_factory.get(filter, binding) }.inject(text) { |s, f| f.filter(s) } end end end end