How to resolve the algorithm CSV to HTML translation step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CSV to HTML translation step by step in the Ada 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 Ada programming language

Source code in the ada programming language

with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;

procedure Csv2Html is
   use type Templates_Parser.Vector_Tag;

   Chars : Templates_Parser.Vector_Tag;
   Speeches : Templates_Parser.Vector_Tag;

   CSV_File : Ada.Text_IO.File_Type;
begin
   -- read the csv data
   Ada.Text_IO.Open (File => CSV_File,
                     Mode => Ada.Text_IO.In_File,
                     Name => "data.csv");

   -- fill the tags
   while not Ada.Text_IO.End_Of_File (CSV_File) loop
      declare
         Whole_Line : String := Ada.Text_IO.Get_Line (CSV_File);
         Comma_Pos : Natural := Ada.Strings.Fixed.Index (Whole_Line, ",");
      begin
         Chars := Chars & Whole_Line (Whole_Line'First .. Comma_Pos - 1);
         Speeches := Speeches & Whole_Line (Comma_Pos + 1 .. Whole_Line'Last);
      end;
   end loop;

   Ada.Text_IO.Close (CSV_File);

   -- build translation table and output html
   declare
      Translations : constant Templates_Parser.Translate_Table :=
        (1 => Templates_Parser.Assoc ("CHAR", Chars),
         2 => Templates_Parser.Assoc ("SPEECH", Speeches));
   begin
      Ada.Text_IO.Put_Line
        (Templates_Parser.Parse ("table.tmplt", Translations));
   end;
end Csv2Html;


<table>
@@TABLE@@
   <tr>
      <td>@_WEB_ESCAPE:CHAR_@</td>
      <td>@_WEB_ESCAPE:SPEECH_@</td>
   </tr>
@@END_TABLE@@
</table>

<table>
   <tr>
      <td>Character</td>
      <td>Speech</td>
   </tr>
   <tr>
      <td>The multitude</td>
      <td>The messiah! Show us the messiah!</td>
   </tr>
   <tr>
      <td>Brians mother</td>
      <td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
   </tr>
   <tr>
      <td>The multitude</td>
      <td>Who are you?</td>
   </tr>
   <tr>
      <td>Brians mother</td>
      <td>I'm his mother; that's who!</td>
   </tr>
   <tr>
      <td>The multitude</td>
      <td>Behold his mother! Behold his mother!</td>
   </tr>
</table>

  

You may also check:How to resolve the algorithm Primality by trial division step by step in the PHP programming language
You may also check:How to resolve the algorithm Generate random chess position step by step in the Java programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Ada programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the BQN programming language