Hello. First of all, thank you for Middleman! Is pretty cool. I wanted to have a weblog using flat files since a long time ago, and for the moment I’ve more or less managed to do it like I want.
Specifically, I wanted to have the input files in one single (and simple) format, without the YAML/JSON frontmatter. One example would be valid HTML: the header and the body could be passed verbatim to the layout, which would add the other boilerplate. Another example would be AsciiDoc, since it can have metadata in attributes. The Jekyll plugin supports this.
For passing the raw HTML body I just did a silly Tilt template and works perfectly. For passing the metadata on the header to Middleman so the blog plugin works, it was a bit harder. I’ve read about add_metadata
from tommy, and I tried to use it in an extension on manipulate_resource_list
, but as he also mentions, the blog plugin might not even use it, which is the case. The blog reads only the data field, and I can’t change it since is frozen. I managed to do it by changing raw_data
instead, but according to the source, is private.
class MyMetadata < Middleman::Extension
def initialize(app, options_hash={}, &block)
super
end
def manipulate_resource_list(resources)
resources.each do |resource|
if resource.source_file =~ /\.xhtml$/
doc = Nokogiri::XML(File.read resource.source_file)
title = doc.at_css('title').text
created_at = Time.parse(
doc.at_css('meta[name="DC.Date.Created"]').attributes['content'].value
)
# Unused by the blog plugin.
# resource.add_metadata(options: {
# title: title, date: created_at
# })
resource.raw_data['title'] = title
resource.raw_data['date'] = created_at
end
end
end
end
It seems it worked so far. What do you think? Is it an acceptable approach or is too hackish?
I’ve also thought of using a kind of “precompilation” step, since I’ve seen that the frontmatter feature also looks for foo-bar.html.xxx.frontmatter
files (the same name as the template file with an additional .frontmatter
suffix). I could process the input files to my will, generate the frontmatter files, then let Middleman do its work. But I hope there is a better way.
Thank you!