alanshaw / grunt-include-replace

Grunt task to include files and replace variables. Allows for parameterised includes.
MIT License
200 stars 45 forks source link

Messing with indentation in the resulting html #14

Closed rudeayelo closed 11 years ago

rudeayelo commented 11 years ago

Having this structure in the main work html file:

<!-- @@include('head.html') -->

  <body>
    <!-- @@include('test.html') -->
  </body>

</html>

and this in the include:

<div class="indent1">
  <div class="indent2">
    <span class="indent3"></span>
  </div>
</div>

results in this:

<html>
  <head></head>

  <body>
    <div class="indent1">
  <div class="indent2">
    <span class="indent3"></span>
  </div>
</div>
  </body>

</html>

expected result was:

<html>
  <head></head>

  <body>
    <div class="indent1">
      <div class="indent2">
        <span class="indent3"></span>
      </div>
    </div>
  </body>

</html>
alanshaw commented 11 years ago

I'm sorry but I won't be adding this feature request to grunt-include-replace. PHP for one exhibits exactly the same behaviour. If you want pretty HTML you could run the output through something like https://github.com/jonschlinkert/grunt-prettify

bradcavanagh commented 10 years ago

I'd like to throw my support behind this request, because there are some file formats that require consistent indenting of code. The case I've run into are Amazon's Elastic Beanstalk configuration files, which can be either YAML or JSON. In YAML, indentation is required to separate different levels of "stuff", and if I'm trying to include a multi-line file in an EB config file, it all needs to be indented at the same level.

While I could hard-wire this indentation into the included file, a better way would be to have an "indent depth" option for grunt-include-replace that indents the entire block of text by a certain number of spaces.

alanshaw commented 10 years ago

There's an undocumented option processIncludeContents which should be a function that is passed the include file contents and local variables as parameters. You could use that to indent each line by however many spaces you like.

You could customise the number of spaces processIncludeContents inserts by passing in a parameter to the include directive:

@@include('/path/to/include/file.html', {"spaces": 2})

With your grunt options looking like:

options: {
  processIncludeContents: function (contents, locals) {
    var indent = new Array((locals.spaces || 0) + 1).join(" ")
    return contents.replace(/^/gm, indent)
  }
}

Pull requests accepted if you want to add in a specific option for doing this, I imagine it'd just hook into processIncludeContents.

monkeytwins commented 9 years ago

@alanshaw

return contents.replace(/^/gm, indent).replace(indent, "");

except the first line