Meta-Dynamic Link Generation

I have started a much-needed refactoring of Lump. Configuration-driven automatic markup generation is being put into a lump.rb library, which is then loaded by the index.rhtml template for the main page. In the process, I have also been cleaning up a little of the configuration file's format.

It has been going well so far, but I am quickly catching up with part of the task that will be interesting to try to figure out how to accomplish.

First, code:

class Lump
  def initialize(conf)
    @config = YAML.load_file(conf)

  def page_title

  def link_list
    @config['head']['link'].collect do |k,v|
      '<link rel="' + v['rel'].to_s + '" type="' + v['type'].to_s +
      '" title="' + v['title'].to_s + '" href="' + v['href'].to_s + '" />'

  def anchor_list(a_type)
    @config['body']['anchor'][a_type].collect do |v|
      '<li><a href="' + v['url'].to_s + '">' + v['name'].to_s + '</a></li>'

That is my new Lump class. So far, that's where all the magic happens. As you should be able to tell, if you know much Ruby, there is nothing magical about it yet.

I have so far managed to avoid thinking about it too much, but I suspect I will need to start adding some magic to this when it comes time to deal with lists of anchor tags that have to be constructed at least in part dynamically. An example would be something like this:

print  '<p><a href="'
print  ''
print  "#{cgi['page'].to_s}&t=", 'r') do |c|
  print c.readline.sub(/^#+\s+/, '').chomp
print  '" style="text-decoration: none">'
print  '<img src=""'
print  ' style="border: none"/> Discuss At Hacker News</a></p>'

While I could (relatively) easily write a new method to do this for me, I'd like to keep the anchor_list general enough to handle these links as well. The problem is that I have not yet figured out how -- and, if I am going to do so, I am beginning to suspect I will need to sprinkle a little metaprogramming magic dust in there to keep it from turning into a single method bigger than the entire rest of the class (so far) combined. After all, it is going to call for dynamically generating the pattern by which anchor tags are dynamically generated. Stuff like that is not as much a walk in the park as the straightforward coding I have done so far for Lump, but if I learn something I suppose the time and effort will prove to have been worth it.

. . . even if I end up getting rid of it later because I decide the code is too "magical" to really be a good idea. I suppose time will tell. For now, though, I have other things to do, so it will have to wait.