I had a need for a simple script to do HTML-encoding from the command line, so I wrote this simple Perl script. It relies on Gisle Aas‘ HTML::Entities module, which many Perl installations will already have (it’s a prerequisite for almost every application that does HTML encoding or URI handling).
The script is named html-encode, and it is in intended to work either on filenames from the command line or as a filter:
$ html-encode file.html
Output is dumped to standard output, so it can be redirected as normal. html-encode will also work as a filter, so it can be used in pipelines:
$ cat myfile.html | html-encode
The filter functionality is primarily so that I can use it from within vi. For example, this will encode the entire buffer:
html-encode also sports a -e option, which will return numeric versions on unsage characters instead of their entity equivalences:
$ echo '<hello what="world">' | html-encode <hello what="world"> $ echo '<hello what="world">' | html-encode -e