tsv2html.rc

#!/usr/bin/env rc
echo '<table>'
nl='
'
tab='	'
bel=`{printf '\a'}
lines = ``($nl){tac | sed -e 's/\t/\a\t/g'}
@{
  for (line in $lines) {
    x=1
    printf '<tr>'
    tokens = ``($tab){echo -n $line}
    for (token in $tokens) {
      token=`{echo $token | sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')}
      rowspan=``($nl){eval echo '$'rowspan$x}
      if (~ $#rowspan 0) {
        rowspan=1
      }
      if (~ $token $bel) {
        eval ``(){eval echo rowspan$x '''=''' '`{' expr $rowspan + 1 '}' }
      } else {
        if (~ $rowspan 1) {
          printf '<td>%s' $token
        } else {
          printf '<td rowspan="%s">%s' $rowspan $token
        }
        eval ``(){eval echo rowspan$x '''=''' 1}
      }
      x=`{expr $x + 1}
    }
    printf '\n'
  }
} | tac | tr -d $bel
echo '</table>'