Hi all,
sometimes we use a WebView to load a local (or non local) HTML file.
To decrease the page overhead and speed up the page loading is good practice to minimize the size,
so remove all informations really not needed for code interpretation, but useful on development
process about user interation, like unused white spaces, comments, end of lines, tabulators etc.
See https://en.wikipedia.org/wiki/Minification_(programming)
This can be different but similar to when we compile eg. Java to bytecode, C++ to a binary file etc....
On this direction I've searched on the forum if someone already wrote some code to just pass
the HTML string and return a minified working string decreased on size.
I've searched it but seem no one have already wrote this, or not posted to the forum, so I tried to write it myself.
I wrote this, it works and return the full HTML as just one full long line decreasing by 15-20% factor the size.
It is a bit spartan and probably only works in my use case but not with all HTML + CSS + JS strings.
My question is.... There are better ways to do this ?
Thanks
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
I use it that way:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
			
			sometimes we use a WebView to load a local (or non local) HTML file.
To decrease the page overhead and speed up the page loading is good practice to minimize the size,
so remove all informations really not needed for code interpretation, but useful on development
process about user interation, like unused white spaces, comments, end of lines, tabulators etc.
See https://en.wikipedia.org/wiki/Minification_(programming)
This can be different but similar to when we compile eg. Java to bytecode, C++ to a binary file etc....
On this direction I've searched on the forum if someone already wrote some code to just pass
the HTML string and return a minified working string decreased on size.
I've searched it but seem no one have already wrote this, or not posted to the forum, so I tried to write it myself.
I wrote this, it works and return the full HTML as just one full long line decreasing by 15-20% factor the size.
It is a bit spartan and probably only works in my use case but not with all HTML + CSS + JS strings.
My question is.... There are better ways to do this ?
Thanks
			
				B4X:
			
		
		
		Private Sub MinifyHTMLFile(Original As StringBuilder) As ResumableSub
 
    Dim isStyle As Boolean = False
    Dim isScript As Boolean = False
    Dim isMultilineComment As Boolean = False
 
    Dim Minified, FinalMinified, Styles, Scripts As StringBuilder
 
    Minified.Initialize
    FinalMinified.Initialize
    Styles.Initialize
    Scripts.Initialize
 
    Dim lines() As String = Regex.Split(CRLF, Original.ToString)
    Log("NUMBER OF LINES: " & lines.Length)
 
    For i = 0 To lines.Length-1
        Dim line As String = lines(i).Trim
 
        If line.Length = 0 Then
            Log("Found void string on line " & (i+1))
        Else
            If line.StartsWith("<style>") Then
                isStyle = True
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [START STYLE]")
            Else If line.StartsWith("</style>") Then
                isStyle = False
                Minified.Append(Styles.ToString).Append(CRLF)
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [END STYLE]")
                Log("FULL STYLE: [" & Styles.ToString & "]")
            Else If line.StartsWith("<script>") Then
                isScript = True
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [START SCRIPT]")
            Else If line.StartsWith("</script>") Then
                isScript = False
                Minified.Append(Scripts.ToString).Append(CRLF)
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [END SCRIPT]")
                Log("FULL Script: [" & Scripts.ToString & "]")
            Else If line.StartsWith("<") And line.EndsWith(">") Then
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [HTML TAG]")
            Else
                If line.StartsWith("/*") Then isMultilineComment = True  'MULTILINE COMMENT
 
                If isStyle Then              
                    If line.StartsWith("//") = True Then
                        Log("Found Full Line [STYLE] comment on line " & (i+1) & " -> [" & line & "]")
                    Else
                        If line.Contains("//") = False Then
                            If isMultilineComment = False Then
                                Styles.Append(line)
                                Log("LINE [ " & (i+1) & "]: " & line & " [STYLE]")
                            Else
                                Log("LINE [ " & (i+1) & "]: " & line & " [STYLE MULTILINE COMMENT]")
                            End If
                        Else
                            Dim n As Int = line.IndexOf("//")
                            Dim comment As String = line.SubString(n).Trim
                            Dim tmp As String = line.SubString2(0, n).Trim
                            Styles.Append(tmp)
                            Log("Found Partial Line [STYLE] comment on line " & (i+1) & " -> [" & comment & "]    LINE WITHOUT COMMENTS -> [" & tmp & "]")
                        End If
                    End If
                Else If isScript Then
                    If line.StartsWith("//") = True Then
                        Log("Found Full Line [SCRIPT] comment on line " & (i+1) & " -> [" & line & "]")
                    Else
                        If line.Contains("//") = False Or line.Contains("\/") = True Then  ' We add Escape
                            If isMultilineComment = False Then
                                Scripts.Append(line)
                                Log("LINE [ " & (i+1) & "]: " & line & " [SCRIPT]")
                            Else
                                Log("LINE [ " & (i+1) & "]: " & line & " [SCRIPT MULTILINE COMMENT]")
                            End If
                        Else
                            Dim n As Int = line.IndexOf("//")
                            Dim comment As String = line.SubString(n).Trim
                            Dim tmp As String = line.SubString2(0, n).Trim
                            Scripts.Append(tmp)
                            Log("Found Partial Line [SCRIPT] comment on line " & (i+1) & " -> [" & comment & "]    LINE WITHOUT COMMENTS -> [" & tmp & "]")
                        End If
                    End If
                End If
 
                If line.EndsWith("*/") Then isMultilineComment = False
            End If
'            Sleep(0)
        End If
    Next
 
    LogColor("MINIFIED MULTILINE:", xui.Color_Green)
 
    '''''' Log it line by line
    Dim lines() As String = Regex.Split(CRLF, Minified.ToString)
    For Each line As String In lines
        LogColor(line, xui.Color_Yellow)
    Next
 
    FinalMinified.append(Minified.ToString.Replace(CRLF, ""))  ' Make it one line
 
    LogColor("NO MINIFIED Size: " & Original.Length & " Bytes", xui.Color_Red)
    LogColor("MINIFIED Size: " & FinalMinified.Length & " Bytes", xui.Color_Red)
 
    LogColor("Size decreased by a factor of " & NumberFormat(100 - MapFloat(FinalMinified.Length, 0, Original.Length, 0, 100), 0, 2) & " percent", xui.Color_Green)
    Return FinalMinified
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 SubI use it that way:
			
				B4X:
			
		
		
		    CreateHTMLString
    Wait For (MinifyHTMLFile(mFullHTML)) Complete (Minified As StringBuilder)
    SaveHTMLFile(Minified.ToString)
			
				Last edited: 
			
		
	
								
								
									
	
		
			
		
	
								
							
							 
				 
 
		 
 
		 
			 
 
		 
 
		 
 
		