Closed rudeayelo closed 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
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.
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
.
@alanshaw
return contents.replace(/^/gm, indent).replace(indent, "");
except the first line
Having this structure in the main work html file:
and this in the include:
results in this:
expected result was: