Sub MinifyHTML(html As String) As String
' Remove HTML comments
Dim noHTMLComments As String = Regex.Replace("<!--.*?-->", "", html)
' Minify <script> blocks
Dim scriptMatches As List
scriptMatches = Regex.Split("<script[^>]*>[\s\S]*?</script>", noHTMLComments)
Dim minifiedScripts As String = ""
For Each match As String In scriptMatches
minifiedScripts = minifiedScripts & MinifyScript(match)
Next
' Minify <style> blocks
Dim styleMatches As List
styleMatches = Regex.Split("<style[^>]*>[\s\S]*?</style>", minifiedScripts)
Dim minifiedStyles As String = ""
For Each match As String In styleMatches
minifiedStyles = minifiedStyles & MinifyStyle(match)
Next
' Further remove spaces and line breaks
Dim noExtraSpaces As String = Regex.Replace(minifiedStyles, "\s{2,}", " ")
Dim noSpacesAroundTags As String = Regex.Replace(noExtraSpaces, "\s*(<[^>]+>)\s*", "$1")
' Remove line breaks and make it one long line
'''''''' Return Regex.Replace(noSpacesAroundTags, "\s+", " ")
Dim finalhtml As String = Regex.Replace(noSpacesAroundTags, "\s+", " ")
LogColor("NO MINIFIED Size: " & html.Length & " Bytes", xui.Color_Red)
LogColor("MINIFIED Size: " & finalhtml.Length & " Bytes", xui.Color_Red)
LogColor("Size decreased by a factor of " & NumberFormat(100 - MapFloat(finalhtml.Length, 0, html.Length, 0, 100), 0, 2) & " percent", xui.Color_Green)
LogColor("FINAL MINIFIED ONE LINE: ", xui.Color_Green)
' myLog.PrintColor(Minified.ToString, xui.Color_Magenta)
LogColor(finalhtml, xui.Color_Yellow)
Return finalhtml
End Sub
Sub MinifyScript(scriptContent As String) As String
' Remove multiline comments
scriptContent = Regex.Replace(scriptContent, "/\*[\s\S]*?\*/", "")
' Remove single-line comments
Return Regex.Replace(scriptContent, "//.*", "")
End Sub
Sub MinifyStyle(styleContent As String) As String
' Similar to MinifyScript, remove comments
styleContent = Regex.Replace(styleContent, "/\*[\s\S]*?\*/", "")
Return Regex.Replace(styleContent, "//.*", "")
End Sub
'Re-maps a Float number from one range to another.
Sub MapFloat(Value As Float, fromLow As Float, fromHigh As Float, toLow As Float, toHigh As Float) As Float
Return ( (Value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow )
End Sub