Skip to main content.
Find on this site:

Me as a baby.Mark A. Taff
Location: Home Code Viewer
  1.  Private Function compactHtml(infile)
  2.  'This sub removes whitespace, tabs, and newlines from the source email that is irrelevant to html rendering.
  3.  
  4.  'It preserves whitespace within "<div class="content"></div><!-- End Content -->", as those spaces may be
  5.  'relevant, either via <pre> or <code> tags, or any other tag via the CSS white-space property.
  6.  
  7.  'We can't use regex here because VB Script 5.5 RegExp object can't handle utf-8
  8.  'Copyright (c) 2007 Mark A. Taff
  9.  
  10.   Dim sBeginningPart As String 'part before the content
  11.   Dim sContentPart As String 'the content itself
  12.   Dim sEndingPart As String 'part after the content
  13.  
  14.   Dim iContentStartPosition As Integer
  15.   Dim iContentEndPosition As Integer
  16.   Dim iLenOrig As Integer
  17.   Dim iLen As Integer
  18.  
  19.   Dim fs As FileSystemObject
  20.   Dim a As TextStream
  21.   Dim bDebug As Boolean
  22.  
  23.   bDebug = False
  24.   iLenOrig = Len(infile)
  25.  
  26.   'If we are an html document, and we have a div with a class of content,
  27.   'compact begining and end parts
  28.   If (InStr(1, infile, "<html") <> 0) And (InStr(1, infile, "<div class=""content"">") <> 0) Then
  29.  
  30.   iContentStartPosition = InStr(1, infile, "<div class=""content"">")
  31.   iContentEndPosition = (InStr(1, infile, "</div><!-- End Content -->") + 26)
  32.  
  33.   sBeginningPart = Mid(infile, 1, iContentStartPosition - 1)
  34.   sContentPart = Mid(infile, iContentStartPosition, iContentEndPosition - iContentStartPosition)
  35.   sEndingPart = Mid(infile, iContentEndPosition)
  36.  
  37.   'replace
  38.   Do Until (InStr(1, sBeginningPart, " ") = 0) And (InStr(1, sBeginningPart, vbTab) = 0) And (InStr(1, sBeginningPart, vbCrLf) = 0)
  39.   sBeginningPart = Replace(sBeginningPart, " ", " ")
  40.   sBeginningPart = Replace(sBeginningPart, vbTab, "")
  41.   sBeginningPart = Replace(sBeginningPart, vbCrLf, "")
  42.   Loop
  43.  
  44.   'replace
  45.   Do Until (InStr(1, sEndingPart, " ") = 0) And (InStr(1, sEndingPart, vbTab) = 0) And (InStr(1, sEndingPart, vbCrLf) = 0)
  46.   sEndingPart = Replace(sEndingPart, " ", " ")
  47.   sEndingPart = Replace(sEndingPart, vbTab, "")
  48.   sEndingPart = Replace(sEndingPart, vbCrLf, "")
  49.   Loop
  50.  
  51.   infile = sBeginningPart & sContentPart & sEndingPart
  52.  
  53.   'If not a new valid xhtml version, compact the whole file
  54.   ElseIf (InStr(1, infile, "<html") <> 0) Then
  55.   'replace
  56.   Do Until (InStr(1, infile, " ") = 0) And (InStr(1, infile, vbTab) = 0) And (InStr(1, infile, vbCrLf) = 0)
  57.   infile = Replace(infile, " ", " ")
  58.   infile = Replace(infile, vbTab, "")
  59.   infile = Replace(infile, vbCrLf, "")
  60.   Loop
  61.   End If
  62.  
  63.   'Remove hack, no need to email it
  64.   infile = Replace(infile, "<!-- End Content -->", "")
  65.  
  66.   If (bDebug) Then
  67.   iLen = Len(infile)
  68.   Set fs = CreateObject("Scripting.FileSystemObject")
  69.   Set a = fs.CreateTextFile("C:\Documents and Settings\ben\Desktop\taff-test2.txt", True)
  70.   a.Write (infile)
  71.   a.Close
  72.   End If
  73.  
  74.   compactHtml = infile
  75.  
  76.  End Function
CC-GNU GPL
Photos & software licensed under the CC-GNU GPL
unless otherwise noted in the JPEG comments field or source code.