How to resolve the algorithm CSV to HTML translation step by step in the Oberon-2 programming language
How to resolve the algorithm CSV to HTML translation step by step in the Oberon-2 programming language
Table of Contents
Problem Statement
Consider a simplified CSV format where all rows are separated by a newline and all columns are separated by commas. No commas are allowed as field data, but the data may contain other characters and character sequences that would normally be escaped when converted to HTML
Create a function that takes a string representation of the CSV data and returns a text string of an HTML table representing the CSV data. Use the following data as the CSV text to convert, and show your output.
Optionally allow special formatting for the first row of the table as if it is the tables header row
(via preferably; CSS if you must).
Let's start with the solution:Step by Step solution about How to resolve the algorithm CSV to HTML translation step by step in the Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE CSV2HTML;
IMPORT
Object,
IO,
IO:FileChannel,
IO:TextRider,
SB := ADT:StringBuffer,
NPCT:Tools,
NPCT:CGI:Utils,
Ex := Exception,
Out;
VAR
fileChannel: FileChannel.Channel;
rd: TextRider.Reader;
line: ARRAY 1024 OF CHAR;
table: SB.StringBuffer;
parts: ARRAY 2 OF STRING;
PROCEDURE DoTableHeader(sb: SB.StringBuffer;parts: ARRAY OF STRING);
BEGIN
sb.Append("
");
sb.AppendLn
END DoTableHeader;
PROCEDURE DoTableRow(sb: SB.StringBuffer;parts: ARRAY OF STRING);
BEGIN
sb.Append(""+Utils.EscapeHTML(parts[0])+" "+Utils.EscapeHTML(parts[1])+" ");
sb.AppendLn
END DoTableRow;
PROCEDURE DoTable(sb: SB.StringBuffer): STRING;
VAR
aux: SB.StringBuffer;
BEGIN
aux := SB.New(""+Utils.EscapeHTML(parts[0])+" "+Utils.EscapeHTML(parts[1])+" ");aux.AppendLn;
RETURN aux.ToString() + sb.ToString() + "
";
END DoTable;
BEGIN
TRY
fileChannel := FileChannel.OpenUnbuffered("script.csv",{FileChannel.read});
CATCH Ex.Exception(ex):
Out.Object(ex.GetMessage());Out.Ln;
HALT(1)
END;
rd := TextRider.ConnectReader(fileChannel);
(* Extract headers *)
TRY
rd.ReadLine(line);
table := NEW(SB.StringBuffer,2048);
Tools.Split(Object.NewLatin1(line),",",parts);
DoTableHeader(table,parts);
CATCH IO.Error(ex):
Out.Object(ex.Name() + ": " + ex.GetMessage());Out.Ln;
HALT(2)
END;
(* Extract data *)
LOOP
TRY
rd.ReadLine(line);
IF (line[0] # 0X)THEN (* skip empty lines *)
Tools.Split(Object.NewLatin1(line),",",parts);
DoTableRow(table,parts)
END
CATCH IO.Error(ex):
EXIT
END
END;
Out.Object(DoTable(table));Out.Ln;
fileChannel.Close()
END CSV2HTML.
You may also check:How to resolve the algorithm Repeat a string step by step in the Tcl programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Objeck programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Ruby programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the Julia programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Odin programming language