awa_unit_2.4.0_59135a52/ada-wiki/src/wiki-filters-collectors.ads

  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
-----------------------------------------------------------------------
--  wiki-filters-collectors -- Wiki word and link collectors
--  Copyright (C) 2016, 2020, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Containers.Indefinite_Ordered_Maps;

--  === Collector Filters ===
--  The `Wiki.Filters.Collectors` package defines three filters that can be used to
--  collect words, links or images contained in a Wiki document.  The collector filters are
--  inserted in the filter chain and they collect the data as the Wiki text is parsed.
--  After the parsing, the collector filters have collected either the words or the links
--  and they can be queried by using the `Find` or `Iterate` operations.
--  The following collectors are defined:
--
--  * The `Word_Collector_Type` collects words from text, headers, links,
--  * The `Link_Collector_Type` collects links,
--  * The `Image_Collector_Type` collects images,
--
--  The filter is inserted in the filter chain before parsing the Wiki document.
--
--    Words : aliased Wiki.Filters.Collectors.Word_Collector_Type;
--    ...
--    Engine.Add_Filter (Words'Unchecked_Access);
--
--  Once the document is parsed, the collector filters contains the data that was collected.
--  The `Iterate` procedure can be used to have a procedure called for each value
--  collected by the filter.
--
--    Words.Iterate (Print'Access);
--
package Wiki.Filters.Collectors is

   pragma Preelaborate;

   package WString_Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Wiki.Strings.WString,
                                                 Element_Type => Natural,
                                                 "<"          => "<",
                                                 "="          => "=");

   subtype Map is WString_Maps.Map;
   subtype Cursor is WString_Maps.Cursor;

   --  ------------------------------
   --  General purpose collector type
   --  ------------------------------
   type Collector_Type is new Filter_Type with private;
   type Collector_Type_Access is access all Collector_Type'Class;

   function Find (Map  : in Collector_Type;
                  Item : in Wiki.Strings.WString) return Cursor;

   function Contains (Map  : in Collector_Type;
                      Item : in Wiki.Strings.WString) return Boolean;

   procedure Iterate (Map : in Collector_Type;
                      Process : not null access procedure (Pos : in Cursor));

   --  ------------------------------
   --  Word Collector type
   --  ------------------------------
   type Word_Collector_Type is new Collector_Type with private;
   type Word_Collector_Type_Access is access all Word_Collector_Type'Class;

   --  Add a text content with the given format to the document.
   overriding
   procedure Add_Text (Filter    : in out Word_Collector_Type;
                       Document  : in out Wiki.Documents.Document;
                       Text      : in Wiki.Strings.WString;
                       Format    : in Wiki.Format_Map);

   --  Add a link.
   overriding
   procedure Add_Link (Filter     : in out Word_Collector_Type;
                       Document   : in out Wiki.Documents.Document;
                       Name       : in Wiki.Strings.WString;
                       Attributes : in out Wiki.Attributes.Attribute_List);

   --  Add an image.
   overriding
   procedure Add_Image (Filter     : in out Word_Collector_Type;
                        Document   : in out Wiki.Documents.Document;
                        Name       : in Wiki.Strings.WString;
                        Attributes : in out Wiki.Attributes.Attribute_List);

   --  Add a quote.
   overriding
   procedure Add_Quote (Filter     : in out Word_Collector_Type;
                        Document   : in out Wiki.Documents.Document;
                        Name       : in Wiki.Strings.WString;
                        Attributes : in out Wiki.Attributes.Attribute_List);

   --  Add a text block that is pre-formatted.
   overriding
   procedure Add_Preformatted (Filter   : in out Word_Collector_Type;
                               Document : in out Wiki.Documents.Document;
                               Text     : in Wiki.Strings.WString;
                               Format   : in Wiki.Strings.WString);

   --  ------------------------------
   --  Link Collector type
   --  ------------------------------
   type Link_Collector_Type is new Collector_Type with private;
   type Link_Collector_Type_Access is access all Link_Collector_Type'Class;

   --  Add a link.
   overriding
   procedure Add_Link (Filter     : in out Link_Collector_Type;
                       Document   : in out Wiki.Documents.Document;
                       Name       : in Wiki.Strings.WString;
                       Attributes : in out Wiki.Attributes.Attribute_List);

   --  Push a HTML node with the given tag to the document.
   overriding
   procedure Push_Node (Filter     : in out Link_Collector_Type;
                        Document   : in out Wiki.Documents.Document;
                        Tag        : in Wiki.Html_Tag;
                        Attributes : in out Wiki.Attributes.Attribute_List);

   --  ------------------------------
   --  Image Collector type
   --  ------------------------------
   type Image_Collector_Type is new Collector_Type with private;
   type Image_Collector_Type_Access is access all Image_Collector_Type'Class;

   --  Add an image.
   overriding
   procedure Add_Image (Filter     : in out Image_Collector_Type;
                        Document   : in out Wiki.Documents.Document;
                        Name       : in Wiki.Strings.WString;
                        Attributes : in out Wiki.Attributes.Attribute_List);

   --  Push a HTML node with the given tag to the document.
   overriding
   procedure Push_Node (Filter     : in out Image_Collector_Type;
                        Document   : in out Wiki.Documents.Document;
                        Tag        : in Wiki.Html_Tag;
                        Attributes : in out Wiki.Attributes.Attribute_List);

private

   type Collector_Type is new Filter_Type with record
      Items : WString_Maps.Map;
   end record;

   procedure Collect_Attribute (Filter     : in out Collector_Type;
                                Attributes : in Wiki.Attributes.Attribute_List;
                                Name       : in String);

   type Word_Collector_Type is new Collector_Type with null record;

   procedure Collect_Words (Filter  : in out Word_Collector_Type;
                            Content : in Wiki.Strings.WString);

   type Link_Collector_Type is new Collector_Type with null record;

   type Image_Collector_Type is new Collector_Type with null record;

end Wiki.Filters.Collectors;