diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2021-11-04 18:15:08 +1300 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2021-11-04 18:15:08 +1300 |
commit | 340e2c4c51d4b933269cff7d085c09ceb145631e (patch) | |
tree | ac5f264c5cafacd6cdcef3bb84d2af1805230c59 | |
parent | 6fbf97420e457570428b3ffbbd8b8de272252e39 (diff) |
Fixed HTML character encoding, question/answer format field parsing
-rw-r--r-- | src/deck_io.adb | 4 | ||||
-rw-r--r-- | src/fmd.adb | 64 | ||||
-rw-r--r-- | src/fmd.ads | 13 |
3 files changed, 59 insertions, 22 deletions
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 * ' ') & "<fields>"); 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 * ' ') & "<field>" & (-Text) & "</field>"); end loop; Put_Line (File_Handle, (4 * ' ') & "</fields>"); @@ -73,18 +72,15 @@ package body FMD is Text : SU.Unbounded_String; begin Put_Line (File_Handle, (8 * ' ') & "<pack>"); - 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 * ' ') & "<qst>" & (-Text) & "</qst>"); 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 * ' ') & "<!--" & "<qst>" & (-Text) & "</qst>" & "-->"); 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 * ' ') & "<ans>" & (-Text) & "</ans>"); end loop; Put_Line (File_Handle, (8 * ' ') & "</pack>"); @@ -119,8 +115,7 @@ package body FMD is Put_Line (File_Handle, (8 * ' ') & "<e>"); 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 * ' ') & "<f>" & (-Text) & "</f>"); else Put_Line (File_Handle, (12 * ' ') & "<f></f>"); @@ -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, "<br>"); + -- Replace_All (Result, Latin.LF, "<br>"); + + -- 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; |