RosettaCodeData/Task/Create-an-HTML-table/Agena/create-an-html-table.agena

56 lines
2.2 KiB
Plaintext

notNumbered := 0; # possible values for html table row numbering
numberedLeft := 1; # " " " " " " "
numberedRight := 2; # " " " " " " "
alignCentre := 0; # possible values for html table column alignment
alignLeft := 1; # " " " " " " "
alignRight := 2; # " " " " " " "
# write an html table to a file
writeHtmlTable := proc( fh, t :: table ) is
local align := "align='";
case t.columnAlignment
of alignLeft then align := align & "left'"
of alignRight then align := align & "right'"
else align := align & "center'"
esac;
local put := proc( text :: string ) is io.write( fh, text & "\n" ) end;
local thElement := proc( content :: string ) is put( "<th " & align & ">" & content & "</th>" ) end;
local tdElement := proc( content ) is put( "<td " & align & ">" & content & "</td>" ) end;
# table element
put( "<table"
& " cellspacing='" & t.cellSpacing & "'"
& " colspacing='" & t.colSpacing & "'"
& " border='" & t.border & "'"
& ">"
);
# table headings
put( "<tr>" );
if t.rowNumbering = numberedLeft then thElement( "" ) fi;
for col to size t.headings do thElement( t.headings[ col ] ) od;
if t.rowNumbering = numberedRight then thElement( "" ) fi;
put( "</tr>" );
# table rows
for row to size t.data do
put( "<tr>" );
if t.rowNumbering = numberedLeft then thElement( row & "" ) fi;
for col to size t.data[ row ] do tdElement( t.data[ row, col ] ) od;
if t.rowNumbering = numberedRight then thElement( row & "" ) fi;
put( "</tr>" )
od;
# end of table
put( "</table>" )
end ;
# create an html table and print it to standard output
scope
local t := [];
t.cellSpacing, t.colSpacing := 0, 0;
t.border := 1;
t.columnAlignment := alignRight;
t.rowNumbering := numberedLeft;
t.headings := [ "A", "B", "C" ];
t.data := [ [ 1001, 1002, 1003 ], [ 21, 22, 23 ], [ 201, 202, 203 ] ];
writeHtmlTable( io.stdout, t )
epocs