Rusty HTML Minifying In R with {rextendr}
This article is originally published at https://rud.is/b
In my M-F newsletter today I mentioned an awesome Rust-based HTML/JS/CSS minifier library that also include batteries for a few other languages.
There was no R port, so I made one using {rextendr}. The {rextendr} package makes is as easy to use Rust code in R packages as {Rcpp} does C/C++ code.
It was as simple as adding some dependencies to the Rust Cargo.toml
file and then adding one Rust function to the main lib.rs
file, and writing a thin wrapper function ({rextendr} can do that, too, but I wanted some default function parameters) for the shim. It took almost no time, and now you, too, can use the utility:
library(minifyhtml)
'
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!-- COMMENT -->
<style>
* { color: black; }
</style>
<title>TITTLE</title>
</head>
<body>
<p>
Some text
</p>
<script>
console.log("This is a console log message.");
</script>
</body>
</html>
' -> src
cat(minify(src))
## <html xmlns=http://www.w3.org/1999/xhtml><meta charset=UTF-8><meta content=width=device-width,initial-scale=1 name=viewport><style>* { color: black; }</style><title>TITTLE</title><body><p>Some text</p><script>console.log("This is a console log message.");</script>
FIN
I have to work out one kink (due to developing on arm64 macOS) and the utility will also be able to minify CSS and JS embedded in HTML.
You can find {minifyhtml} on GitHub.
Thanks for visiting r-craft.org
This article is originally published at https://rud.is/b
Please visit source website for post related comments.