   beautifyHTML = require('js-beautify').html
   Entities = require('html-entities').AllHtmlEntities
   {decode} = new Entities()
-  path = require 'path'
+  fspath = require 'path'
   mkdirp = require 'mkdirp'
 
 

        tag: tag
        isBlock: isBlockTag(tag)
        name: if isBlockTag(tag) then getBlockName(data) else null

      if _options.decode
        # Make sure we decode the HTML entities.

    res.push _.rest(pathToRoot)


  # Creates multiple HTML files from `data`, each HTML file will be based on
  # template `tpl`, wil be stored in `dest` directory (plus the path determined
  # by the ids on the path to the section in the markdown document) and will
  # have `ext` extension.
  splitIntoMultipleFiles = (data, tpl, dest, ext) ->
    # Leaves is an array of arrays (paths from leaf to root in the markdown
    # document).
    leaves = []

    getDataLeaves data, [], leaves

    # We have e.g. `clients/overview` and `clients/foo/bar` now but we want
    # `clients` and `clients/foo` too.
    leavesIncludingBlocks = _.union leaves, (_.initial(p) for p in leaves)

    # There's a lot of duplicates now, let's remove them to speed things up.
    leavesIncludingBlocks = _.uniq leavesIncludingBlocks, (leafPath) ->
      _.pluck(leafPath, 'id').join ' '

    # Blocks are supposed to be invisible. So lets remove them (otherwise we would
    # get paths with ids like `-- front --` and `-- back --`).
    leaves = _.map leavesIncludingBlocks, (path) ->
      return _.reject path, (node) -> node.isBlock

    # We might have gotten some empty paths, let's get rid of those.
    nonEmptyLeaves = _.filter leaves, (path) -> path.length > 0

    nodesPaths = _.map nonEmptyLeaves, (leafPath) ->
      # `parts` will be the path to the new HTML page produced from the
      # markdown section.
      parts = _.union([dest], _.pluck(leafPath, 'id'))
      leafDest = fspath.join.apply @, parts
      leafDest = "#{leafDest}#{ext}"
      return [leafPath, leafDest]

    for [nodes, path] in nodesPaths
      mkdirp.sync fspath.dirname(path)

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