Module: Webgen::ContentProcessor

Defined in:
lib/webgen/contentprocessor.rb,
lib/webgen/deprecated.rb,
lib/webgen/contentprocessor/erb.rb,
lib/webgen/contentprocessor/haml.rb,
lib/webgen/contentprocessor/head.rb,
lib/webgen/contentprocessor/rdoc.rb,
lib/webgen/contentprocessor/sass.rb,
lib/webgen/contentprocessor/tags.rb,
lib/webgen/contentprocessor/blocks.rb,
lib/webgen/contentprocessor/erubis.rb,
lib/webgen/contentprocessor/maruku.rb,
lib/webgen/contentprocessor/builder.rb,
lib/webgen/contentprocessor/redcloth.rb,
lib/webgen/contentprocessor/fragments.rb,
lib/webgen/contentprocessor/rdiscount.rb

Overview

Namespace for all content processors.

Implementing a content processor

Content processors are used to process the content of files, normally of files in Webgen Page Format. A content processor only needs to respond to one method called call and must not take any parameters in the initialize method. This method is invoked with a Webgen::Context object that provides the whole context (especially the content and the node chain) and the method needs to return this object. During processing a content processor normally changes the content of the context but it does not need to.

A self-written content processor does not need to be in the Webgen::ContentProcessor namespace but all shipped ones do.

After writing the content processor class, one needs to add it to the contentprocessor.map hash so that it is used by webgen. The key for the entry needs to be a short name without special characters or spaces and the value can be:

  • the class name, not as constant but as a string - then this content processor is assumed to work with textual data -, or

  • an array with the class name like before and the type, which needs to be :binary or :text.

Sample Content Processor

The following sample content processor checks for a meta information replace_key and replaces strings of the form replace_key:path/to/node with a link to the specified node if it is found.

Note how the content node, the reference node and the destination node are used sothat the correct meta information is used, the node is correctly resolved and the correct relative link is calculated respectively!

class SampleProcessor

  def call(context)
    if !context.content_node['replace_key'].to_s.empty?
      context.content.gsub!(/#{context.content_node['replace_key']}:([\w\/.]+)/ ) do |match|
        link_node = context.ref_node.resolve($1, context.content_node.lang)
        if link_node
          context.dest_node.link_to(link_node, :lang => context.content_node.lang)
        else
          match
        end
      end
    end
    context
  rescue Exception => e
    raise "Error while replacing special key: #{e.message}"
  end

end

WebsiteAccess.website.config['contentprocessor.map']['replacer'] = 'SampleProcessor'
# Or one could equally write
# WebsiteAccess.website.config['contentprocessor.map']['replacer'] = ['SampleProcessor', :text]

Defined Under Namespace

Modules: Deprecated Classes: AccessHash, Blocks, Builder, Erb, Erubis, Fragments, Haml, Head, Maruku, RDiscount, RDoc, RedCloth, Sass, Tags

Class Method Summary collapse

Class Method Details

.const_missing(const) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/webgen/deprecated.rb', line 50

def self.const_missing(const)
  if const.to_s == 'Context'
    warn("Deprecation warning (~ #{caller.first}): Webgen::ContentProcessor::Context is now named Webgen::Context! This alias will be removed in one of the next releases.")
    Webgen::Context
  else
    super
  end
end

.for_name(name) ⇒ Object

Return the content processor object identified by name.



85
86
87
88
# File 'lib/webgen/contentprocessor.rb', line 85

def self.for_name(name)
  klass, cp_type = WebsiteAccess.website.config['contentprocessor.map'][name]
  klass.nil? ? nil : WebsiteAccess.website.cache.instance(klass)
end

.is_binary?(name) ⇒ Boolean

Return whether the content processor identified by name is processing binary data.

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/webgen/contentprocessor.rb', line 91

def self.is_binary?(name)
  WebsiteAccess.website.config['contentprocessor.map'][name].kind_of?(Array) &&
    WebsiteAccess.website.config['contentprocessor.map'][name].last == :binary
end

.listObject

Return the list of all available content processors.



80
81
82
# File 'lib/webgen/contentprocessor.rb', line 80

def self.list
  WebsiteAccess.website.config['contentprocessor.map'].keys
end