I just finished building two websites in hobix (coswellproductions.org 
and howtomodel.com) and I really like its simple approach to web content 
management.  However, one thing that I really needed for my web 
development was the ability to create the static HTML pages based on 
much more complicated content.  For instance, there's a lot of 
information on the howtomodel.com site that's very structured, with lots 
of areas on a page that need to be hidden or shown based on whether or 
not some property in a configuration file exists.  For my own website, I 
needed to be able to auto-generate a lot of JavaScript and HTML for 
displaying images and videos in a JS gallery frame, and I wasn't going 
to hand-code all of those, especially if the gallery API changed in the 
future.

This is what I came up with.  It allows me to run ERB code right inside 
of a Hobix entry, and I get access to all of the yaml parameters that 
are defined before the text processor field is processed, since (as far 
as I can tell) yaml files are parsed from top-to-bottom.  I still have 
the RedCloth markup available to me, and I can even pre-load certain 
variables before any processing even occurs.  This is loaded through an 
included Ruby file defined in the requires section of the hobix.yaml 
file for the site.  If I don't want RedCloth, I can use the ErbProcessor 
instead.

John

--

require 'erb'

# ErbProcessor is a text processor that accepts ERB code
# that's been used as page content.

class ErbProcessor
  @@shared_data = {}
 
  # When a new ErbProcessor is initialized via Hobix::Entry#maker,
  # have the ErbProcessor get a copy of the current Entry's fields.
 
  def initialize(string, fields)
    @code = ERB.new(string)
    @fields = @@shared_data
    @fields.update(fields)
  end
 
  # Values global to all instances of ErbProcessor get added here.
 
  def self.append_shared_data(data)
    @@shared_data.update(data)
  end
 
  def to_html
    @code.result(binding)
  end 
 
  def to_str
    to_html
  end
 
  # Access your available fields by using self[field_name]...
 
  def [](field)
    @fields[field]
  end
 
  # ...or just use the field name
 
  def method_missing(symbol)
    @fields[symbol.to_s]
  end
end

# ErbRedClothProcessor runs the provided string through
# both ERB and then RedCloth before returning the result.

class ErbRedClothProcessor < ErbProcessor
  def to_html
    a = @code.result(binding)
    RedCloth.new(a).to_html
  end
end

module Hobix
  class ErbRedClothEntry < Entry

    yaml_type "tag:erbredcloth.cool,2006:normal"

    def self.text_processor; ErbRedClothProcessor; end

    def self.maker( val )
      self::text_processor_fields.each do |f|
          if val[f].respond_to? :value
              str = val[f].value
              def str.to_html
                  self
              end
              val[f] = str
          elsif val[f].respond_to? :to_str
              val[f] = self::text_processor.new( val[f].to_str, val )
          end
      end

      YAML::object_maker( self, val )
    end
  end
end

_______________________________________________
Hobix-is-the-way mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/hobix-is-the-way

Reply via email to