     return root
 
 
-  # Returns header id for a region (accessible in the template).
-  # Either you can specify the id in the Markdown file or it will take the
-  # heading text and slugify it.
-  getHeaderId = (data) ->
+  # Returns metadata for a region (accessible in the template).
+  getMetadata = (data) ->
+    grunt.log.debug 'data', data
+
     text = _.last data[0]
-    {id_pattern} = _options
-    match = text.match id_pattern
+    {metadata_pattern} = _options
+    match = text.match metadata_pattern
+
+    metadata = {}
+
+    grunt.log.debug 'match', text, match
 
     if match
-      # Strip the ID pattern.
-      data[0][data[0].length - 1] = _.trim text.replace(id_pattern, '')
-      headerId = match[1]
-
-    # Slugify the header text.
-    headerId or= _(data[0]).chain().last().slugify().value().toLowerCase()
-
-    return headerId
-
-
-  grunt.registerMultiTask 'md_to_html', 'Transmogrify your MD into HTML.', ->
+      # Strip the metadata pattern.
+      data[0][data[0].length - 1] = _.trim text.replace(metadata_pattern, '')
+      metadata = JSON.parse match[1]
+
+    metadata.id or= _(data[0]).chain().last().slugify().value().toLowerCase()
+
+    grunt.log.debug 'meatadata', metadata
+
+    return metadata
+
+
+  grunt.registerMultiTask 'markdown_jade', 'Turn markdown & template into HTML.', ->
 
     # Default options.
     _options = @options
-      id_pattern: /{(.+)}/
+      metadata_pattern: /{(.+)}/
       tags: ['h1', 'h2', 'h3']
 
-    _.each @files, ({src, template, dest}) =>
+    _.each @files, ({src, template, dest, ext}) =>
 
       files = _.filter src, (filepath) ->
         if not grunt.file.exists filepath

      # changing data object.
      getHTML = (data) -> _.union(['html'], JSON.parse JSON.stringify data)

      root.children.push
        id: headerId
        content: markdown.renderJsonML(getHTML data)
        header: markdown.renderJsonML(getHTML [data[0]])
        body: markdown.renderJsonML(getHTML _.rest(data))

      _.each parsedFilesTuples, ([filepath, data]) ->
        tpl = template or _options.template

        if not _options.multiple_files
          grunt.file.copy tpl, dest,
            process: (contents, path) ->
              html = grunt.template.process contents, data: _.extend(data, _options.data)
              # Pretty print the html.
              return if _options.pretty then beautifyHTML(html) else html
        else
          splitIntoMultipleFiles(data, tpl, dest, ext)


  getDataLeaves = (data, pathToRoot=[], res) ->
    for child in data.children
      #parent = if data.id then [data.id] else []
      getDataLeaves(child, _.union(pathToRoot, [data]), res)

    # Don't forget the leave node.
    pathToRoot.push(data)

    # We're only interested in the leaves.
    return unless _.isEmpty(data.children)

    # Forget the first part (that's the "header").
    res.push _.rest(pathToRoot)


  splitIntoMultipleFiles = (data, tpl, dest, ext) ->
    leaves = []
    getDataLeaves data, [], leaves

    tmp = []

    # Add the non-leaf paths to the mix as well (so that we have links to
    # e.g. foo/bar, i.e., the "section overview page").
    leaves = _.union leaves, (_.initial(p) for p in leaves)

    for leavePath in leaves when leavePath.length > 0
      parts = _.union([dest], _.pluck(leavePath, 'id'))
      leaveDest = path.join.apply @, parts
      leaveDest = "#{leaveDest}#{ext}"

      grunt.log.debug 'mkdirp', path.dirname(leaveDest)
      grunt.log.debug 'section',_.pluck(leavePath, 'id'), _.last(leavePath)

      if not _.last(leavePath).body
        grunt.log.debug "skipping ", _.pluck(leavePath, 'id'), ": no body"
        continue

      mkdirp.sync path.dirname(leaveDest)

      grunt.file.copy tpl, leaveDest,
        process: (contents, path) ->
          html = grunt.template.process contents, data: _.extend({
              section: _.last(leavePath)
              ids: _.pluck(leavePath, 'id')
              all: data
              root: leavePath[0]
            }, _options.data)
          # Pretty print the html.
          return if _options.pretty then beautifyHTML(html) else html
