summaryrefslogtreecommitdiff
path: root/src/deckdata-fmd.adb
blob: 10040a0f6ab399ef8be0969f2870c00c6a279f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


--  This source is licensed under the Sunset License v1.0


with

    Ada.Characters.Latin_1,
    Ada.Strings.Fixed,
    Ada.Strings.Maps,
    Ada.Text_IO;

use

    Ada.Text_IO;


package body Deckdata.FMD is


    package Latin renames Ada.Characters.Latin_1;
    package Strfix renames Ada.Strings.Fixed;
    package Strmap renames Ada.Strings.Maps;

    function "*"
           (Left  : in Natural;
            Right : in Character)
        return String
        renames Ada.Strings.Fixed."*";




    procedure Put_Header
           (File_Handle : in Ada.Text_IO.File_Type;
            Version     : in String := "1.4") is
    begin
        Put_Line (File_Handle, "<?xml version=""1.0"" encoding=""UTF-8""?>");
        Put_Line (File_Handle, "<!DOCTYPE freshmemory-dict>");
        Put_Line (File_Handle, "<dict version=""" & Version & """>");
    end Put_Header;




    procedure Put_Fields
           (File_Handle : in Ada.Text_IO.File_Type;
            Field_IDs   : in Field_ID_Vector)
    is
        Text : SU.Unbounded_String;
    begin
        Put_Line (File_Handle, (4 * ' ') & "<fields>");
        for FID of Field_IDs loop
            Text := Prep (SU.Unbounded_String (FID));
            Put_Line (File_Handle, (8 * ' ') & "<field>" & (-Text) & "</field>");
        end loop;
        Put_Line (File_Handle, (4 * ' ') & "</fields>");
    end Put_Fields;




    procedure Start_Pack_Section
           (File_Handle : in Ada.Text_IO.File_Type) is
    begin
        Put_Line (File_Handle, (4 * ' ') & "<packs>");
    end Start_Pack_Section;


    procedure Put_Pack
           (File_Handle : in Ada.Text_IO.File_Type;
            Q_Data      : in Field_ID_Vector;
            A_Data      : in Field_ID_Vector)
    is
        Text : SU.Unbounded_String;
    begin
        Put_Line (File_Handle, (8 * ' ') & "<pack>");
        Text := Prep (SU.Unbounded_String (Q_Data.First_Element));
        Put_Line (File_Handle, (12 * ' ') & "<qst>" & (-Text) & "</qst>");
        for I in Field_Ordinal
            range Field_Ordinal'Succ (Q_Data.First_Index) .. Q_Data.Last_Index
        loop
            Text := Prep (SU.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 := Prep (SU.Unbounded_String (FID));
            Put_Line (File_Handle, (12 * ' ') & "<ans>" & (-Text) & "</ans>");
        end loop;
        Put_Line (File_Handle, (8 * ' ') & "</pack>");
    end Put_Pack;


    procedure End_Pack_Section
           (File_Handle : in Ada.Text_IO.File_Type) is
    begin
        Put_Line (File_Handle, (4 * ' ') & "</packs>");
    end End_Pack_Section;




    procedure Start_Entry_Section
           (File_Handle : in Ada.Text_IO.File_Type) is
    begin
        Put_Line (File_Handle, (4 * ' ') & "<entries>");
    end Start_Entry_Section;


    procedure Put_Entry
           (File_Handle : in Ada.Text_IO.File_Type;
            Data        : in Field_Vector;
            Quantity    : in Positive)
    is
        Counter : Positive := 1;
        Position : Field_Ordinal := Data.First_Index;
        Text : SU.Unbounded_String;
    begin
        Put_Line (File_Handle, (8 * ' ') & "<e>");
        while Counter <= Quantity loop
            if Position <= Data.Last_Index then
                Text := Prep (SU.Unbounded_String (Data.Element (Position)));
                Put_Line (File_Handle, (12 * ' ') & "<f>" & (-Text) & "</f>");
            else
                Put_Line (File_Handle, (12 * ' ') & "<f></f>");
            end if;
            Position := Field_Ordinal'Succ (Position);
            Counter := Counter + 1;
        end loop;
        Put_Line (File_Handle, (8 * ' ') & "</e>");
    end Put_Entry;


    procedure End_Entry_Section
           (File_Handle : in Ada.Text_IO.File_Type) is
    begin
        Put_Line (File_Handle, (4 * ' ') & "</entries>");
    end End_Entry_Section;




    procedure Put_Footer
           (File_Handle : in Ada.Text_IO.File_Type) is
    begin
        Put_Line (File_Handle, "</dict>");
    end Put_Footer;




    procedure Replace_All
           (Text : in out SU.Unbounded_String;
            Char : in     Character;
            Sub  : in     String)
    is
        Position : Natural;
    begin
        loop
            Position := SU.Index (Text, Strmap.To_Set (Char));
            exit when Position = 0;
            SU.Replace_Slice (Text, Position, Position, Sub);
        end loop;
    end Replace_All;


    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 + Item'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
        --  Fresh Memory needs these character codes
        Replace_All (Result, '<', "&lt;");
        Replace_All (Result, '>', "&gt;");
        Replace_All (Result, '"', "&quot;");

        --  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, "&ndash;", "–");
        Replace_All (Result, "&rsquo;", "’");
        Replace_All (Result, "&lsquo;", "‘");
        Replace_All (Result, "&rdquo;", "”");
        Replace_All (Result, "&ldquo;", "“");

        return Result;
    end Prep;


end Deckdata.FMD;