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

Published on 12 May 2024 09:40 PM

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

Source code in the delphi programming language

program csv2html;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes;

const
  // Carriage Return/Line Feed
  CRLF    = #13#10;

  // The CSV data
  csvData =
  'Character,Speech'+CRLF+
  'The multitude,The messiah! Show us the messiah!'+CRLF+
  'Brians mother,Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!'+CRLF+
  'The multitude,Who are you?'+CRLF+
  'Brians mother,I''m his mother; that''s who!'+CRLF+
  'The multitude,Behold his mother! Behold his mother!';

  // HTML header
  htmlHead =
  '
  'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'+CRLF+
  '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+CRLF+
  ''+CRLF+
  ''+CRLF+
  ''+CRLF+
  'CSV-to-HTML Conversion'+CRLF+
  ''+CRLF+
  ''+CRLF+
  ''+CRLF;

  // HTML footer
  htmlFoot =
  ''+CRLF+
  '';

{ Function to split a string into a list using a given delimiter }
procedure SplitString(S, Delim: string; Rslt: TStrings);
var
  i: integer;
  fld: string;
begin
  fld := '';

  for i := Length(S) downto 1 do
    begin
      if S[i] = Delim then
        begin
          Rslt.Insert(0,fld);
          fld := '';
        end
        else
         fld := S[i]+fld;
    end;

  if (fld <> '') then
      Rslt.Insert(0,fld);
end;

{ Simple CSV parser with option to specify that the first row is a header row }
procedure ParseCSV(const csvIn: string; htmlOut: TStrings; FirstRowIsHeader: Boolean = True);
const
  rowstart      = '';
  rowend        = '';
  cellendstart  = '';
  hcellendstart = '';
  hrowstart     = '';
  hrowend       = '';
var
  tmp,pieces: TStrings;
  i: Integer;
begin
  // HTML header
  htmlOut.Text := htmlHead + CRLF + CRLF;

  // Start the HTML table
  htmlOut.Text := htmlOut.Text + ''  + CRLF;  // Create stringlist  tmp := TStringList.Create;  try    // Assign CSV data to stringlist and fix occurences of '<' and '>'    tmp.Text := StringReplace(csvIn,'<','<',[rfReplaceAll]);    tmp.Text := StringReplace(tmp.Text,'>','>',[rfReplaceAll]);    // Create stringlist to hold the parts of the split data    pieces := TStringList.Create;    try      // Loop through the CSV rows      for i := 0 to Pred(tmp.Count) do        begin          // Split the current row          SplitString(tmp[i],',',pieces);          // Check if first row and FirstRowIsHeader flag set          if (i = 0) and FirstRowIsHeader then                      // Render HTML            htmlOut.Text := htmlOut.Text + hrowstart + pieces[0] + hcellendstart + pieces[1] + hrowend + CRLF            else            htmlOut.Text := htmlOut.Text + rowstart + pieces[0] + cellendstart + pieces[1] + rowend + CRLF;        end;      // Finish the HTML table and end the HTML page      htmlOut.Text := htmlOut.Text + '
' + CRLF + htmlFoot;
finally pieces.Free; end; finally tmp.Free; end; end; var HTML: TStrings; begin // Create stringlist to hold HTML output HTML := TStringList.Create; try Writeln('Basic:'); Writeln(''); // Load and parse the CSV data ParseCSV(csvData,HTML,False); // Output the HTML to the console Writeln(HTML.Text); // Save the HTML to a file (in application's folder) HTML.SaveToFile('csv2html_basic.html'); Writeln(''); Writeln('====================================='); Writeln(''); HTML.Clear; Writeln('Extra Credit:'); Writeln(''); // Load and parse the CSV data ParseCSV(csvData,HTML,True); // Output the HTML to the console Writeln(HTML.Text); // Save the HTML to a file (in application's folder) HTML.SaveToFile('csv2html_extra.html'); Writeln(''); Writeln('====================================='); finally HTML.Free; end; // Keep console window open Readln; end. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> CSV-to-HTML Conversion
CharacterSpeech
The multitudeThe messiah! Show us the messiah!
Brians mother<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitudeWho are you?
Brians motherI'm his mother; that's who!
The multitudeBehold his mother! Behold his mother!
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> CSV-to-HTML Conversion
CharacterSpeech
The multitudeThe messiah! Show us the messiah!
Brians mother<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitudeWho are you?
Brians motherI'm his mother; that's who!
The multitudeBehold his mother! Behold his mother!

You may also check:How to resolve the algorithm Mutual recursion step by step in the Elixir programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the langur programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Delphi programming language
You may also check:How to resolve the algorithm Permutations step by step in the EDSAC order code programming language