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 | --
-- Copyright (C) 2021-2023, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
with Ada.Wide_Wide_Text_IO;
with Ada.Integer_Wide_Wide_Text_IO;
with VSS.Characters;
with VSS.Strings.Conversions;
with VSS.Strings.Cursors.Iterators.Characters;
package body HTML_Writers is
function "+"
(Item : VSS.Strings.Virtual_String'Class) return Wide_Wide_String
renames VSS.Strings.Conversions.To_Wide_Wide_String;
procedure Close_Tag (Self : in out Writer'Class);
function Escape
(Text : VSS.Strings.Virtual_String;
Escape_All : Boolean := False) return VSS.Strings.Virtual_String;
----------------
-- Characters --
----------------
procedure Characters
(Self : in out Writer; Text : VSS.Strings.Virtual_String)
is
Value : constant VSS.Strings.Virtual_String :=
(if Self.CDATA then Text else Escape (Text));
begin
Self.Close_Tag;
Ada.Wide_Wide_Text_IO.Put (+Value);
end Characters;
---------------
-- Close_Tag --
---------------
procedure Close_Tag (Self : in out Writer'Class) is
begin
if not Self.Tag.Is_Empty then
Ada.Wide_Wide_Text_IO.Put (">");
Self.Tag.Clear;
end if;
end Close_Tag;
-----------------
-- End_Element --
-----------------
procedure End_Element
(Self : in out Writer; Local_Name : VSS.Strings.Virtual_String)
is
use type VSS.Strings.Virtual_String;
use type VSS.Strings.Character_Count;
begin
if Self.Tag = Local_Name and then
not (Self.Tag = "code" or else
Self.Tag = "html" or else
Self.Tag = "a" or else
Self.Tag = "li")
and then (Self.Tag.Character_Length = 1 or else
Self.Tag.At_Last_Character.Element not in '1' .. '9')
then
Ada.Wide_Wide_Text_IO.Put ("/>");
Self.Tag.Clear;
else
Self.Close_Tag;
Ada.Wide_Wide_Text_IO.Put ("</");
Ada.Wide_Wide_Text_IO.Put (+Local_Name);
Ada.Wide_Wide_Text_IO.Put (">");
end if;
if Local_Name.Starts_With ("h") or else Local_Name = "p" then
Ada.Wide_Wide_Text_IO.New_Line;
end if;
end End_Element;
------------
-- Escape --
------------
function Escape
(Text : VSS.Strings.Virtual_String;
Escape_All : Boolean := False)
return VSS.Strings.Virtual_String
is
Result : VSS.Strings.Virtual_String;
Cursor : VSS.Strings.Cursors.Iterators.Characters.Character_Iterator :=
Text.At_First_Character;
begin
if Cursor.Has_Element then
loop
case Cursor.Element is
when '&' =>
Result.Append ("&");
when '"' =>
if Escape_All then
Result.Append ("%22");
else
Result.Append (""");
end if;
when '>' =>
Result.Append (">");
when '<' =>
Result.Append ("<");
when 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' |
'-' | '_' | '.' | '~' | '/' | '@' | '+' | ',' |
'(' | ')' | '#' | '?' | '=' | ':' | '*'
=>
Result.Append (Cursor.Element);
when others =>
if Escape_All or
VSS.Characters.Virtual_Character'Pos (Cursor.Element) in
16#1# .. 16#8#
| 16#B# .. 16#C#
| 16#E# .. 16#1F#
| 16#7F#
then
declare
Image : Wide_Wide_String (1 .. 7); -- -#16#xx#
begin
Ada.Integer_Wide_Wide_Text_IO.Put
(To => Image,
Item => VSS.Characters.Virtual_Character'Pos
(Cursor.Element),
Base => 16);
Result.Append ("%");
Result.Append
(VSS.Strings.To_Virtual_String (Image (5 .. 6)));
end;
else
Result.Append (Cursor.Element);
end if;
end case;
exit when not Cursor.Forward;
end loop;
end if;
return Result;
end Escape;
-------------------
-- Start_Element --
-------------------
procedure Start_Element
(Self : in out Writer; Local_Name : VSS.Strings.Virtual_String;
Attributes : HTML_Attributes'Class := No_Attributes)
is
use type VSS.Strings.Virtual_String;
begin
Self.Close_Tag;
Ada.Wide_Wide_Text_IO.Put ("<");
Ada.Wide_Wide_Text_IO.Put (+Local_Name);
if Local_Name = "hr" then
Ada.Wide_Wide_Text_IO.Put (" ");
end if;
for Attribute of Attributes loop
Ada.Wide_Wide_Text_IO.Put (" ");
Ada.Wide_Wide_Text_IO.Put (+Attribute.Name);
Ada.Wide_Wide_Text_IO.Put ("=""");
if Attribute.Name = "href" then
Ada.Wide_Wide_Text_IO.Put (+Escape (Attribute.Value, True));
elsif Attribute.Name = "class" then
Ada.Wide_Wide_Text_IO.Put (+Attribute.Value);
else
Ada.Wide_Wide_Text_IO.Put (+Escape (Attribute.Value, False));
end if;
Ada.Wide_Wide_Text_IO.Put ("""");
end loop;
Self.Tag := Local_Name;
end Start_Element;
end HTML_Writers;
|