formatting mail

Mark McEahern marklists at mceahern.com
Sat Jan 12 09:28:13 PST 2002


Gar Lipow wrote:
> Mark - that may be the single most useful ASP based utility I've seen.
> Wish I'd thought of it.

Thanks!


> Umm how widely can we forward this? Because if you get enough hits on
> this, you may suddenly find your "fixed price" site incurring additional
> charges for use exceeding your gigabyte limit.

I'm not worried about that. Distribute freely. I should probably provide a link for feedback.

Also, the JavaScript suggestion is a good idea. I'll add that to my (neverending) list of things to do.

In the meantime, here's the ASP/VBScript code (I hate VBScript, but my host doesn't provide Python, so there you go.)

<%

' -------------------------------------------------------------------------- --

' format.asp

'

' Copyright C 2002, Mark McEahern.

'

' Feel free to use this code however you wish. Copy it. Sell it. Send it

' to Mars. I make no guarantees.

' -------------------------------------------------------------------------- --

Option Explicit

Response.Buffer = True

Response.Expires = 0

On Error Resume Next

Const requestString = "s"

Const paragraphMarker = "|||"

Dim s, thisScript

thisScript = Request.ServerVariables("script_name")

Main

Sub Main()

If IsPostBack() Then

s = Request.Form(requestString)

s = Format(s)

End If

End Sub

Function Format(text)

text = Replace(text, ">", " ")

text = NormalizeWhitespace(text)

text = Replace(text, vbCrLf & vbCrLf, paragraphMarker)

text = Replace(text, vbCrLf, " ")

text = Replace(text, paragraphMarker, vbCrLf & vbCrLf)

text = NormalizeWhitespace(text)

Format = text

End Function

Function NormalizeWhitespace(text)

Const crLf = "[\r\f\n]+"

Dim re

Set re = New RegExp

re.Global = True

' Remove whitespace from the beginning.

re.Pattern = "^\s+"

text = re.Replace(text, "^")

' Remove whitespace from the end.

re.Pattern = "\s+$"

text = re.Replace(text, "$")

' Replace multiple spaces with a single space.

re.Pattern = " +"

text = re.Replace(text, " ")

' Remove whitespace after crlf.

re.Pattern = crLf & "[ \t\v]"

text = re.Replace(text, vbCrLf)

' Remove whitespace before crlf.

re.Pattern = "[ \t\v]" & crLf

text = re.Replace(text, vbCrLf)

NormalizeWhitespace = text

End Function

Function IsPostBack()

IsPostBack = False

If StrComp(Request.ServerVariables("request_method"), "POST", vbTextCompare) = 0 Then

IsPostBack = True

End If

End Function

%> <html> <head> <title>format</title> <body>

<h1>format</h1>

<p>paste the text here and click format.</p>

<form action="<%=thisScript%>" method="POST">

<textarea name="<%=requestString%>" cols="60" rows="20"><%=s%></textarea>

<br>

<input type="submit" value="format">

</form>

</body> </html>



More information about the lbo-talk mailing list