// Utilities for HTML manipulation.
HTMLEncode[line] :=
{
line =~ %s/&/&/g;
line =~ %s/</</g;
line =~ %s/>/>/g;
return line
}
// Decodes the hex-encoded parts of an argument.
URLDecode[arg] :=
{
return arg =~ %s/%([0-9a-fA-F]{2})/char[parseInt[$1, 16]]/ge
}
// Helper function to return a zero-padded hexadecimal number.
hexpad[c] :=
{
cc = hex[char[c]]
if (length[cc] == 1)
cc = "0$cc"
return "%$cc"
}
// URL encode a part of a string to be used in a URL. The specification for
// URLs (RFC 1738, Dec. '94) states "...Only alphanumerics [0-9a-zA-Z], the
// special characters "$-_.+!*'()," and reserved characters used for
// their reserved purposes may be used unencoded within a URL."
URLEncode[str] :=
{
return str =~ %s/([^0-9a-zA-Z\$\-\_\.\+\!\*\'\(\)\,])/hexpad[$1]/ge
}
// Make the <OPTIONS> tags for an HTML <SELECT> list, with the specified
// item selected.
// makeselect[list, selected]
makeSelect[list, selected] :=
{
for [name, val] list
{
if (val != undef)  // Has value
{
vs = "$val"  // Coerce to string
valStr = (val != undef) ? " VALUE=\"" + HTMLEncode[vs] + "\"" : ""
sel = (vs == selected ? " SELECTED" : "")
} else
sel = (name == selected ? " SELECTED" : "")
println[" <OPTION$valStr$sel>" + HTMLEncode["$name"]]
}
}
// Format a Frink expression into a prettier HTML equivalent.
formatExpression[expr] :=
{
expr =~ %s/\^([\/\d\w\.\-]+)/<SUP>$1<\/SUP>/g  // Draw superscripts
expr =~ %s/\+(\s*)-\s*1\s+/-$1/g;  // "+ -1" goes to "-"
expr =~ %s/\+(\s*)-/-$1/g;  // "+ -" goes to "-"
return expr
}
View or download HTMLUtils.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 14705 days, 20 hours, 1 minutes ago.