# htmlprocessor
[](https://www.npmjs.com/package/htmlprocessor)
[](https://travis-ci.org/dciccale/node-htmlprocessor)
[](https://www.npmjs.com/package/htmlprocessor)
[](https://coveralls.io/github/dciccale/node-htmlprocessor)
`npm install -g htmlprocessor`
### Grunt/Gulp task
This module is the processor behind [grunt-processhtml](https://www.npmjs.com/package/grunt-processhtml), [gulp-processhtml](https://www.npmjs.com/package/gulp-processhtml) tasks.
For plenty of examples visit the [documentation](https://github.com/dciccale/grunt-processhtml#readme).
## CLI
Outputs help
```sh
$ htmlprocessor -h
Usage: htmlprocessor file-to-process.html [options]
-h, --help display this help message
-v, --version display the version number
-l, --list file to output list of replaced files
-o, --output file to output processed HTML to
-d, --data pass a JSON file to processor
-e, --env specify an environment
-r, --recursive recursive processing
-c, --comment-marker change the comment marker
-i, --include-base set the directory to include files from
-s, --strip strip blocks matched by other environments
--custom-block-type specify custom block type
```
Outputs version number
```sh
$ htmlprocessor -v
```
Outputs to `file-to-process.processed.html`.
```sh
$ htmlprocessor file-to-process.html
```
Outputs to `processed/file.html`.
```sh
$ htmlprocessor file-to-process.html -o processed/file.html
```
Pass some data
```sh
$ htmlprocessor file-to-process.html -o processed/file.html -d data.json
```
Specify an environment
```sh
$ htmlprocessor file-to-process.html -o processed/file.html -e dev
```
Allow recursive processing
```sh
$ htmlprocessor file-to-process.html -o processed/file.html -r
```
Change the comment marker to ``
```sh
$ htmlprocessor file-to-process.html -o processed/file.html --comment-marker process
```
## List option
Create a list of files that were replaced and use that list to streamline the build process.
Note: This new option does not affect in any way the previous existing functionality (i.e. it's backward compatible).
```sh
$ htmlprocessor file-to-process.html -o processed/file.html --list wrk/replacement.list
```
Assumning you have this code in an HTML (or JSP)
```sh
.
.
.
.
.
.
.
.
.
```
The file "wrk/replacement.list" will contain something like this:
```sh
file-to-process.html:js/bower_components/bootstrap/dist/css/bootstrap.css
file-to-process.html:content/bootstrap-responsive.min.css
file-to-process.html:content/styles.css
file-to-process.html:content/myApplicationStyles.css
file-to-process.html:js/bower_components/jquery/dist/jquery.js
file-to-process.html:js/bower_components/angular/angular.js
file-to-process.html:js/bower_components/angular-route/angular-route.js
file-to-process.html:app/app.js
file-to-process.html:app/filters/filters.js
file-to-process.html:app/controllers/applications.js
```
And you can use these commands to concatenate and eventually minify without having to update the build to tell
it where it should pickup each files. Also, in this way it orders the global file content in the same manner
as your individual includes originally were.
```sh
sh -c "cat `cat wrk/replacement.list | grep '\.js$' | cut -d: -f2` > dist/js/myApplication.js"
sh -c "cat `cat wrk/replacement.list | grep '\.css$' | cut -d: -f2` > dist/css/myApplication.css"
```
If you processed more than a single "html" file, you can change the grep like this:
```sh
... | grep 'file-to-process.html:.*\.js$' | ... > dist/js/myApplication.js
... | grep 'other-file-to-process.html:.*\.js$' | ... > dist/js/myApplicationOther.js
```
The originating file name is included in the list file for that very purpose.
## Development
Use Node.js 20.19 or later in the Node.js 20 series, or Node.js 22.12 or later.
Mocha and NYC require these versions to run the tests and measure coverage.
```sh
npm ci
npm test
npm run coverage
```
Coverage includes the library, file writer, and command-line tool.
The coverage check requires 95% of statements, lines, and functions, and 90% of branches.
The reports are in `coverage/`, including `lcov.info` for Coveralls.
`npm run coveralls` sends the report with `coveralls-next`.
The uploader uses the native Node.js HTTP APIs instead of `request`.
The package still exports the CommonJS `HTMLProcessor` constructor.
The `process`, `processContent`, `template`, and `registerBlockType` methods remain available.
`grunt-processhtml` uses this constructor and these methods.
The `grunt-processhtml` 0.4.5 release pins `htmlprocessor` to `0.3.4`.
## License
See [LICENSE.txt](https://raw.github.com/dciccale/node-htmlprocessor/master/LICENSE-MIT)