Is there any way to override the file extension when choosing Tilt renderer for the file?

I have source files that contains markdown text, but due to a quirk in the application that produces them, they don’t have any extension (not even .html).

I have an extension that fixes the destination path, so if the extensionless file for example is named something I can access it as something.html.

Problem is, how to instruct Middleman to render it as markdown? I’ve peaked around in the code, and it seams that Tilt is using the extension of the resource.source_file, to determine what renderer to apply to the file. I have not been able to find any place where it’s possible to instruct it to use another renderer. There is however a lot of options and blocks passed around, and maybe one of those could do the trick?

Hi @tommysundstrom,

I’m not sure how to resolve that problem. My inclination would be to address the cause, not the symptom, and fix the application that produces them, but if that’s not possible, you have a couple of options.

First would be a Rake task that you could run by hand to automatically rename the files to what you need. Create (or edit) a file called Rakefile in your project root:

desc 'Rename files without extensions to .md'
task :rename_markdown do
  require 'fileutils'

  files_without_extensions = Dir.glob("source/**/*").select do |file|
    File.extname(file).empty? && !File.directory?(file)
  end

  files_without_extensions.each do |source|
    destination = "#{source}.md"
    print "Moving #{source} to #{destination}... "
    # This will overwrite any file that might already exist at the destination
    FileUtils.mv(file, "#{file}.md")
    puts "OK"
  end
end

You can run this task with rake rename_markdown, whenever you need to.

If running the task by hand isn’t ideal, you could use something like Guard to watch the filesystem, and run that Rake task when any new files are given.

Handling things at the Middleman level will make your code quite likely coupled with Middleman’s implementation, which is a bad idea for the long run.