From 340e2c4c51d4b933269cff7d085c09ceb145631e Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Thu, 4 Nov 2021 18:15:08 +1300 Subject: Fixed HTML character encoding, question/answer format field parsing --- src/deck_io.adb | 4 +++- src/fmd.adb | 64 +++++++++++++++++++++++++++++++++++++++++---------------- src/fmd.ads | 13 +++++++++--- 3 files changed, 59 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/deck_io.adb b/src/deck_io.adb index 8a3637e..5207fe7 100644 --- a/src/deck_io.adb +++ b/src/deck_io.adb @@ -203,7 +203,9 @@ package body Deck_IO is is use type Pat.Match_Location; Matches : Pat.Match_Array (0 .. 1); - Regexp : Pat.Pattern_Matcher := Pat.Compile ("{{(?:\w+:)?(\w+)}}", Pat.Single_Line); + Regexp : Pat.Pattern_Matcher := + -- My god I hate regular expressions, they're such unreadable nonsense + Pat.Compile ("{{(?:(?:\w|\s)+:)?((?:\w|\s)+)}}", Pat.Single_Line); Marker : Positive := Raw_Data'First; begin loop diff --git a/src/fmd.adb b/src/fmd.adb index ae11660..8d8fcde 100644 --- a/src/fmd.adb +++ b/src/fmd.adb @@ -48,8 +48,7 @@ package body FMD is begin Put_Line (File_Handle, (4 * ' ') & ""); for FID of Field_IDs loop - Text := To_Unbounded_String (FID); - Standard_Escapes (Text); + Text := Prep (To_Unbounded_String (FID)); Put_Line (File_Handle, (8 * ' ') & "" & (-Text) & ""); end loop; Put_Line (File_Handle, (4 * ' ') & ""); @@ -73,18 +72,15 @@ package body FMD is Text : SU.Unbounded_String; begin Put_Line (File_Handle, (8 * ' ') & ""); - Text := To_Unbounded_String (Q_Data.First_Element); - Standard_Escapes (Text); + Text := Prep (To_Unbounded_String (Q_Data.First_Element)); Put_Line (File_Handle, (12 * ' ') & "" & (-Text) & ""); for I in Vector_Index range Vector_Index'Succ (Q_Data.First_Index) .. Q_Data.Last_Index loop - Text := To_Unbounded_String (Q_Data.Element (I)); - Standard_Escapes (Text); + Text := Prep (To_Unbounded_String (Q_Data.Element (I))); -- Fresh Memory unfortunately cannot cope with multiple question fields Put_Line (File_Handle, (12 * ' ') & ""); end loop; for FID of A_Data loop - Text := To_Unbounded_String (FID); - Standard_Escapes (Text); + Text := Prep (To_Unbounded_String (FID)); Put_Line (File_Handle, (12 * ' ') & "" & (-Text) & ""); end loop; Put_Line (File_Handle, (8 * ' ') & ""); @@ -119,8 +115,7 @@ package body FMD is Put_Line (File_Handle, (8 * ' ') & ""); while Counter <= Quantity loop if Position <= Data.Last_Index then - Text := To_Unbounded_String (Data.Element (Position)); - Standard_Escapes (Text); + Text := Prep (To_Unbounded_String (Data.Element (Position))); Put_Line (File_Handle, (12 * ' ') & "" & (-Text) & ""); else Put_Line (File_Handle, (12 * ' ') & ""); @@ -150,7 +145,7 @@ package body FMD is - procedure Escape + procedure Replace_All (Text : in out SU.Unbounded_String; Char : in Character; Sub : in String) @@ -162,16 +157,49 @@ package body FMD is exit when Position = 0; SU.Replace_Slice (Text, Position, Position, Sub); end loop; - end Escape; + end Replace_All; - procedure Standard_Escapes - (Text : in out SU.Unbounded_String) is + procedure Replace_All + (Text : in out SU.Unbounded_String; + Item : in String; + Sub : in String) + is + Position : Natural; + begin + loop + Position := SU.Index (Text, Item); + exit when Position = 0; + SU.Replace_Slice (Text, Position, Position + Sub'Length - 1, Sub); + end loop; + end Replace_All; + + + function Prep + (Text : in SU.Unbounded_String) + return SU.Unbounded_String + is + Result : SU.Unbounded_String := Text; begin - Escape (Text, '<', "<"); - Escape (Text, '>', ">"); - Escape (Text, '"', """); - end Standard_Escapes; + -- Fresh Memory needs these character codes + Replace_All (Result, '<', "<"); + Replace_All (Result, '>', ">"); + Replace_All (Result, '"', """); + + -- Not sure why these cause Fresh Memory to not load the dict + -- Replace_All (Result, Latin.CR & Latin.LF, "
"); + -- Replace_All (Result, Latin.LF, "
"); + + -- Fresh memory doesn't recognise these character codes so get rid of them + -- Note that this list may not be complete + Replace_All (Result, "–", "–"); + Replace_All (Result, "’", "’"); + Replace_All (Result, "‘", "‘"); + Replace_All (Result, "”", "”"); + Replace_All (Result, "“", "“"); + + return Result; + end Prep; end FMD; diff --git a/src/fmd.ads b/src/fmd.ads index fe720d5..1c64deb 100644 --- a/src/fmd.ads +++ b/src/fmd.ads @@ -118,15 +118,22 @@ private - procedure Escape + procedure Replace_All (Text : in out SU.Unbounded_String; Char : in Character; Sub : in String) with Pre => Ada.Strings.Fixed.Count (Sub, Ada.Strings.Maps.To_Set (Char)) = 0; - procedure Standard_Escapes - (Text : in out SU.Unbounded_String); + procedure Replace_All + (Text : in out SU.Unbounded_String; + Item : in String; + Sub : in String); + + + function Prep + (Text : in SU.Unbounded_String) + return SU.Unbounded_String; end FMD; -- cgit