HTMLUtils.frink

View or download HTMLUtils.frink in plain text format


// Utilities for HTML manipulation.

HTMLEncode[line] :=
{
   if !line
      return ""
   line =~ %s/&/&/g;
   line =~ %s/</&lt;/g;
   line =~ %s/>/&gt;/g;
   return line
}

HTMLEncodeQuoted[line] :=
{
   if !line
      return ""
   line = HTMLEncode[line];
   line =~ %s/"/&quot;/g;
//   line =~ %s/\\u/\\\\u/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
}


// 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
}

// Format an expression using some symbolic rules.
formatExpressionSymbolic[expr] :=
{
   expr = formatExpression[expr]
   return removeApproximations[expr]
}

// Remove approximations
removeApproximations[expr] :=
{
   expr =~ %s/\s*\((approx\.|exactly).*?\)//g
   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 17536 days, 6 hours, 1 minutes ago.