The following program will replace characters in text files that may not display correctly
in HTML files. I wrote it to make including code snippets in my website easier. In vim, you
can read in the output of this command to the current line with the command
r!obfuscate.exe <input>. The text file <input> will
be printed (escaped) under the cursor location.
Program obfuscate.cpp
/* Escape characters in text files for HTML dislpay.
* INTEL icx obfuscate.cpp /Qstd:c++23
* GNU g++ obfuscate.cpp -std=c++23 */
#include <fstream>
#include <iostream>
#include <print>
#include <regex>
#include <string>
int main(int argc, char **argv) {
if (argc != 2) {
std::print(std::cerr, "ERROR: Incorrect number of parameters.\n");
std::print(std::cerr, "USAGE: {} <input_file>\n", argv[0]);
return 1;
}
/* The first argument is a path to a text file that we will convert to html format. */
std::ifstream in(argv[1], std::ios::binary);
if (!in) {
std::print(std::cerr, "ERROR: Failed to open file: {}\n", argv[1]);
return 1;
}
/* Read the entire file into a string. */
std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
if (in.bad()) {
std::print(std::cerr, "ERROR: Opened {} but failed to read content.\n", argv[1]);
return 1;
}
/* Format text such that it will display nicely in HTML.
* Note that order of operations below matters. If & replacement
* happend after, say, < replacement, then the & in < will be
* replaced. Similar story for \r or <br>. Beware! */
s = std::regex_replace(s, std::regex("&"), "&");
s = std::regex_replace(s, std::regex("<"), "<");
s = std::regex_replace(s, std::regex(">"), ">");
s = std::regex_replace(s, std::regex("\r"), ""); /* Just cause Windows... */
s = std::regex_replace(s, std::regex("\n"), "<br>\n");
s = std::regex_replace(s, std::regex("\t"), " ");
s = std::regex_replace(s, std::regex(" "), " ");
std::print("<code>\n{}</code>", s);
return 0;
}
Output of obfuscate.exe obfuscate.cpp
/* Escape characters in text files for HTML dislpay.<br>
* INTEL icx obfuscate.cpp /Qstd:c++23 <br>
* GNU g++ obfuscate.cpp -std=c++23 */<br>
#include <fstream><br>
#include <iostream><br>
#include <print><br>
#include <regex><br>
#include <string><br>
<br>
int main(int argc, char **argv) {<br>
if (argc != 2) {<br>
std::print(std::cerr, "ERROR: Incorrect number of parameters.\n");<br>
std::print(std::cerr, "USAGE: {} <input_file>\n", argv[0]);<br>
return 1;<br>
}<br>
<br>
/* The first argument is a path to a text file that we will convert to html format. */<br>
std::ifstream in(argv[1], std::ios::binary);<br>
if (!in) {<br>
std::print(std::cerr, "ERROR: Failed to open file: {}\n", argv[1]);<br>
return 1;<br>
}<br>
<br>
/* Read the entire file into a string. */<br>
std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());<br>
if (in.bad()) {<br>
std::print(std::cerr, "ERROR: Opened {} but failed to read content.\n", argv[1]);<br>
return 1;<br>
}<br>
<br>
/* Format text such that it will display nicely in HTML. <br>
* Note that order of operations below matters. If &amp; replacement<br>
* happend after, say, &lt; replacement, then the & in &lt; will be<br>
* replaced. Similar story for \r or <br>. Beware! */<br>
s = std::regex_replace(s, std::regex("&"), "&amp;");<br>
s = std::regex_replace(s, std::regex("<"), "&lt;");<br>
s = std::regex_replace(s, std::regex(">"), "&gt;");<br>
s = std::regex_replace(s, std::regex("\r"), ""); /* Just cause Windows... */<br>
s = std::regex_replace(s, std::regex("\n"), "<br>\n");<br>
s = std::regex_replace(s, std::regex("\t"), "&nbsp;&nbsp;&nbsp;&nbsp;");<br>
s = std::regex_replace(s, std::regex(" "), "&nbsp;");<br>
<br>
std::print("<code>\n{}</code>", s);<br>
return 0;<br>
}<br>