Rewrite destination path

Hi,

I’m new to middleman (and Ruby) and I’d like to implement the following scenario: Given a directory parent/ and subdirectories subi/ with multiple files in it:

parent/
    sub0/
        sub00/
            file0.adoc
        sub01/
            file1.adoc
    sub1/
        sub10/
            file2.adoc
        sub11/
            file3.adoc

How do I change the paths to get:

parent/
    sub00/
        file0.html
    sub01/
        file1.html
    sub10/
        file2.html
    sub11/
        file3.html

Do I have to rewrite the destination_path in the Sitemap::Resource via an extension or how do I achieve this?

[Edit] Simplified the scenario, was: “Rewrite path based on frontmatter”.

You can do this most straightforwardly with proxy:

# config.rb

proxy '/parent/sub00/file0.html', '/parent/sub0/sub00/file0.html', ignore: true
proxy '/parent/sub01/file1.html', '/parent/sub0/sub01/file1.html', ignore: true
proxy '/parent/sub10/file2.html', '/parent/sub1/sub10/file2.html', ignore: true
proxy '/parent/sub11/file3.html', '/parent/sub1/sub11/file3.html', ignore: true
$ tree source build
source
└── parent
    ├── sub0
    │   ├── sub00
    │   │   └── file0.adoc
    │   └── sub01
    │       └── file1.adoc
    └── sub1
        ├── sub10
        │   └── file2.adoc
        └── sub11
            └── file3.adoc
build
└── parent
    ├── sub00
    │   └── file0.html
    ├── sub01
    │   └── file1.html
    ├── sub10
    │   └── file2.html
    └── sub11
        └── file3.html

The sitemap and frontmatter aren’t available during configuration, though—

# config.rb

puts "#{ sitemap.resources.length } during configuration"
after_configuration { puts "#{ sitemap.resources.length } after configuration" }
ready { puts "#{ sitemap.resources.length } when ready" }
$ middleman server
== The Middleman is loading
0 during configuration
0 after configuration
4 when ready

—so you’ll probably find a custom extension most useful:

# config.rb

require 'lib/rewrites'

activate :rewrites
# lib/rewrites.rb

class Rewrites < Middleman::Extension
  def manipulate_resource_list(resources)
    resources.each do |resource|
      basename = File.basename(resource.path)
      category = resource.data.category

      resource.destination_path = File.join('parent', category, basename)
    end
  end
end

::Middleman::Extensions.register :rewrites, Rewrites
# source/parent/sub0/sub00/file0.adoc etc. frontmatter

category: 'sub00'

Thank you very much, that helps a lot. Unfortunately I have hundreds of subi/ directories - I guess I have to iterate and call proxy for every file (couldn’t find globbing support).