| 1 |
with Ada.Text_IO, |
| 2 |
Ada.Strings.Fixed, |
| 3 |
Ada.Strings.Unbounded, |
| 4 |
Ada.Characters.Handling, |
| 5 |
Ada.Command_Line; |
| 6 |
procedure Rtf2Form is |
| 7 |
|
| 8 |
-- |
| 9 |
-- Convert an RTF file (as in the Rationale) to a roughly formatted .MSS |
| 10 |
-- file. This tool is part of ARM_Form, the Ada Reference Manual formatter. |
| 11 |
-- |
| 12 |
-- --------------------------------------- |
| 13 |
-- Copyright 2006, 2011 AXE Consultants. |
| 14 |
-- P.O. Box 1512, Madison WI 53701 |
| 15 |
-- E-Mail: [email protected] |
| 16 |
-- |
| 17 |
-- ARM_Form is free software: you can redistribute it and/or modify |
| 18 |
-- it under the terms of the GNU General Public License version 3 |
| 19 |
-- as published by the Free Software Foundation. |
| 20 |
-- |
| 21 |
-- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS" |
| 22 |
-- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY, |
| 23 |
-- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL. |
| 24 |
-- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL, |
| 25 |
-- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES, |
| 26 |
-- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 27 |
-- DAMAGES. |
| 28 |
-- |
| 29 |
-- A copy of the GNU General Public License is available in the file |
| 30 |
-- gpl-3-0.txt in the standard distribution of the ARM_Form tool. |
| 31 |
-- Otherwise, see <http://www.gnu.org/licenses/>. |
| 32 |
-- |
| 33 |
-- If the GPLv3 license is not satisfactory for your needs, a commercial |
| 34 |
-- use license is available for this tool. Contact Randy at AXE Consultants |
| 35 |
-- for more information. |
| 36 |
-- |
| 37 |
-- --------------------------------------- |
| 38 |
-- |
| 39 |
-- Edit History: |
| 40 |
-- |
| 41 |
-- 1/19/06 - RLB - Created program. |
| 42 |
-- 1/20/06 - RLB - Changed to generate tab markers for tabs, as John |
| 43 |
-- wants to preserve tabs where possible. |
| 44 |
-- 10/18/11 - RLB - Changed to GPLv3 license. |
| 45 |
|
| 46 |
use Ada.Strings.Unbounded; |
| 47 |
|
| 48 |
-- Declarations stolen from Trash-Finder: |
| 49 |
|
| 50 |
type Line_Count is range 0 .. 1_000_000; -- 1 million lines ought to be enough. |
| 51 |
type Line_Type; |
| 52 |
type Line_Access is access Line_Type; |
| 53 |
type Line_Type is record |
| 54 |
Number : Line_Count; |
| 55 |
Text : Ada.Strings.Unbounded.Unbounded_String; |
| 56 |
Next : Line_Access; |
| 57 |
end record; |
| 58 |
|
| 59 |
procedure Free is new Ada.Unchecked_Deallocation (Line_Type, Line_Access); |
| 60 |
-- Free an individual line. |
| 61 |
|
| 62 |
procedure Free_All (List : in out Line_Access) is |
| 63 |
-- Free the entire list. |
| 64 |
Temp : Line_Access; |
| 65 |
begin |
| 66 |
while List /= null loop |
| 67 |
Temp := List; |
| 68 |
List := List.Next; |
| 69 |
Free (Temp); |
| 70 |
end loop; |
| 71 |
end Free_All; |
| 72 |
|
| 73 |
procedure Load_File (File_Name : in String; |
| 74 |
Data : in out Line_Access) is |
| 75 |
-- Read the file, breaking at each paragraph end (to keep the lines from |
| 76 |
-- getting too long). The original line breaks are irrelevant. |
| 77 |
File : Ada.Text_IO.File_Type; |
| 78 |
Last, Working : Line_Access := null; |
| 79 |
Count : Line_Count := 0; |
| 80 |
Buffer : String (1..1000); -- Since line endings aren't significant, the |
| 81 |
-- length doesn't matter much, other than we |
| 82 |
-- might miss a "\par" that way. |
| 83 |
BLength : Natural; |
| 84 |
Break_Loc, Start_Loc : Natural; |
| 85 |
begin |
| 86 |
Ada.Text_IO.Open (File, Ada.Text_IO.In_File, File_Name); |
| 87 |
loop |
| 88 |
Ada.Text_IO.Get_Line (File, Buffer, BLength); |
| 89 |
Count := Count + 1; |
| 90 |
--Ada.Text_IO.Put_Line("Line" & Line_Count'Image(Count) & ": read=" & Buffer(1..BLength)); |
| 91 |
Start_Loc := 1; |
| 92 |
Break_Loc := Start_Loc; |
| 93 |
while Break_Loc <= BLength-4 loop |
| 94 |
-- Look for "\par " or "\par\", only (in particular, not "\pard"). |
| 95 |
exit when Buffer(Break_Loc..Break_Loc+3) = "\par" and then |
| 96 |
(Buffer(Break_Loc+4) = ' ' or else |
| 97 |
Buffer(Break_Loc+4) = '\'); |
| 98 |
-- Found it. |
| 99 |
Break_Loc := Break_Loc + 1; |
| 100 |
end loop; |
| 101 |
if Break_Loc > BLength-4 then |
| 102 |
Break_Loc := BLength+1; |
| 103 |
end if; |
| 104 |
if Last = null then |
| 105 |
Working := new Line_Type; |
| 106 |
Working.Number := Count; |
| 107 |
Working.Next := null; |
| 108 |
Working.Text := To_Unbounded_String (Buffer(1..Break_Loc-1)); |
| 109 |
Data := Working; |
| 110 |
Last := Working; |
| 111 |
--Ada.Text_IO.Put_Line("Line" & Line_Count'Image(Count) & ": initial=" & Buffer(1..Break_Loc-1)); |
| 112 |
elsif Length(Last.Text) > 30000 and then Break_Loc > BLength then |
| 113 |
-- Break at the original place, as this is too long. |
| 114 |
-- This is fine so long as the length of Buffer < 2768/2 (1386) |
| 115 |
Last.Text := Last.Text & To_Unbounded_String (Buffer(1..Break_Loc-1)); |
| 116 |
--Ada.Text_IO.Put_Line("Line" & Line_Count'Image(Count) & ": append, then break=" & Buffer(1..Break_Loc-1)); |
| 117 |
Break_Loc := BLength - 3; -- Force a new line here. (This will make |
| 118 |
-- Start_Loc small enough to enter the |
| 119 |
-- loop below and create an empty line.) |
| 120 |
else |
| 121 |
Last.Text := Last.Text & To_Unbounded_String (Buffer(1..Break_Loc-1)); |
| 122 |
--Ada.Text_IO.Put_Line("Line" & Line_Count'Image(Count) & ": append=" & Buffer(1..Break_Loc-1)); |
| 123 |
end if; |
| 124 |
Start_Loc := Break_Loc + 4; -- Skip \par. |
| 125 |
if Start_Loc <= BLength and then Buffer(Start_Loc) = ' ' then |
| 126 |
Start_Loc := Start_Loc + 1; -- Skip trailling space, too. |
| 127 |
end if; |
| 128 |
--Ada.Text_IO.Put_Line("Start_Loc = " & Natural'Image(Start_Loc) & " Blength=" & Natural'Image(BLength)); |
| 129 |
while Start_Loc <= BLength+1 loop -- +1 so we handle end of line "\par" properly. |
| 130 |
-- Create a new line: |
| 131 |
Working := new Line_Type; |
| 132 |
Working.Number := Count; |
| 133 |
Working.Next := null; |
| 134 |
Last.Next := Working; |
| 135 |
Last := Working; |
| 136 |
Break_Loc := Start_Loc; |
| 137 |
while Break_Loc <= BLength-4 loop |
| 138 |
-- Look for "\par " or "\par\", only (in particular, not "\pard"). |
| 139 |
exit when Buffer(Break_Loc..Break_Loc+3) = "\par" and then |
| 140 |
(Buffer(Break_Loc+4) = ' ' or else |
| 141 |
Buffer(Break_Loc+4) = '\'); |
| 142 |
-- Found it. |
| 143 |
Break_Loc := Break_Loc + 1; |
| 144 |
end loop; |
| 145 |
if Break_Loc > BLength-4 then |
| 146 |
Break_Loc := BLength+1; |
| 147 |
end if; |
| 148 |
Working.Text := To_Unbounded_String (Buffer(Start_Loc..Break_Loc-1)); |
| 149 |
--Ada.Text_IO.Put_Line("Line" & Line_Count'Image(Count) & ": startp=" & Buffer(Start_Loc..Break_Loc-1)); |
| 150 |
Start_Loc := Break_Loc + 4; -- Skip \par. |
| 151 |
if Start_Loc <= BLength and then Buffer(Start_Loc) = ' ' then |
| 152 |
Start_Loc := Start_Loc + 1; -- Skip trailling space, too. |
| 153 |
end if; |
| 154 |
end loop; |
| 155 |
-- Done with line when we get here. |
| 156 |
end loop; |
| 157 |
exception |
| 158 |
when Ada.Text_IO.End_Error => -- Done reading. |
| 159 |
Ada.Text_IO.Close (File); |
| 160 |
end Load_File; |
| 161 |
|
| 162 |
|
| 163 |
-- Stack of RTF command information: |
| 164 |
type RTF_Group_Kind is (Unknown, Empty, |
| 165 |
Bold, Italic, Tab, Line_Break, |
| 166 |
Exam, ExamCom, Key); -- Type of RTF command. |
| 167 |
type Brace_Info is record |
| 168 |
Kind : RTF_Group_Kind; |
| 169 |
end record; |
| 170 |
Brace_Info_Stack : array (1..1000) of Brace_Info; |
| 171 |
Current_Brace : Natural := 0; |
| 172 |
|
| 173 |
type Paragraph_Styles is (Unknown, None, Normal, Example, Description); |
| 174 |
Current_Paragraph_Style : Paragraph_Styles := Unknown; |
| 175 |
|
| 176 |
procedure Process_and_Write_File (File_Name : in String; |
| 177 |
Data : in out Line_Access) is |
| 178 |
File : Ada.Text_IO.File_Type; |
| 179 |
Cursor : Line_Access := Data; |
| 180 |
Chars_on_Line : Natural; -- Number of characters output on the current line. |
| 181 |
-- Data: |
| 182 |
Deepest_Brace_Nesting : Natural := 0; |
| 183 |
Paragraphs : Natural := 0; |
| 184 |
Escaped_Chars : Natural := 0; |
| 185 |
EM_Dashes : Natural := 0; |
| 186 |
EN_Dashes : Natural := 0; |
| 187 |
Intraparagraph_Line_Breaks : Natural := 0; |
| 188 |
Intraparagraph_Tabs : Natural := 0; |
| 189 |
Brace_Kinds_Found : array (RTF_Group_Kind) of Natural := (others => 0); |
| 190 |
Styles_Found : array (Paragraph_Styles) of Natural := (others => 0); |
| 191 |
procedure Close_Old_Style is |
| 192 |
-- Close the current paragraph style in preparation for a new one. |
| 193 |
begin |
| 194 |
case Current_Paragraph_Style is |
| 195 |
when Unknown => null; |
| 196 |
when None => null; |
| 197 |
when Normal => |
| 198 |
null; --Ada.Text_IO.Put_Line (File, "@end[Intro]"); -- ** Temp (no style needed for these). |
| 199 |
when Example => |
| 200 |
Ada.Text_IO.Put_Line (File, "@end[Example]"); |
| 201 |
when Description => |
| 202 |
Ada.Text_IO.Put_Line (File, "@end[Description]"); |
| 203 |
end case; |
| 204 |
end Close_Old_Style; |
| 205 |
begin |
| 206 |
Ada.Text_IO.Create (File, Ada.Text_IO.Out_File, File_Name); |
| 207 |
-- Header stuff (ignored, really). |
| 208 |
Ada.Text_IO.Put_Line (File, "@Part(xxx, Root=""rat.msm"")"); |
| 209 |
Ada.Text_IO.Put_Line (File, ""); |
| 210 |
Ada.Text_IO.Put_Line (File, "@comment($Source: e:\\cvsroot/ARM/Progs/rtf2form.ada,v $)"); |
| 211 |
Ada.Text_IO.Put_Line (File, "@comment($Revision: 1.4 $ $Date: 2012/05/19 01:00:26 $)"); |
| 212 |
Ada.Text_IO.Put_Line (File, ""); |
| 213 |
|
| 214 |
while Cursor /= null loop |
| 215 |
declare |
| 216 |
Our_Line : constant String := To_String(Cursor.Text); |
| 217 |
Working : Natural := Our_Line'First; |
| 218 |
Last_Output : Natural := Working - 1; |
| 219 |
procedure Clean_Buffer_and_Close_Old_Style is |
| 220 |
-- Close the current paragraph style in preparation for a new one. |
| 221 |
begin |
| 222 |
Ada.Text_IO.Put_Line (File, Our_Line(Last_Output+1..Working-1)); |
| 223 |
--Ada.Text_IO.Put_Line("Close style on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working-1)); |
| 224 |
Chars_on_Line := 0; -- Start over. |
| 225 |
Last_Output := Working-1; |
| 226 |
Close_Old_Style; |
| 227 |
end Clean_Buffer_and_Close_Old_Style; |
| 228 |
|
| 229 |
function Possible_Style (Head, Tail : Natural) return Boolean is |
| 230 |
-- Check that the stuff between Head and Tail |
| 231 |
-- can *only* be a style, with nothing extra. |
| 232 |
begin |
| 233 |
--Ada.Text_IO.Put_Line ("Possible style=" & Our_Line(Head..Tail)); |
| 234 |
for I in Head .. Tail loop |
| 235 |
if Our_Line(I) = '{' then |
| 236 |
--Ada.Text_IO.Put_Line (" NO - open group"); |
| 237 |
return False; -- Starts some group, not part of a style. |
| 238 |
elsif Our_Line(I) = '{' then |
| 239 |
--Ada.Text_IO.Put_Line (" NO - close group"); |
| 240 |
return False; -- Ends some group, not part of a style. |
| 241 |
elsif Our_Line(I) = ' ' then |
| 242 |
if I = Tail then |
| 243 |
--Ada.Text_IO.Put_Line (" OK"); |
| 244 |
return True; |
| 245 |
elsif Our_Line(I+1) = '\' then |
| 246 |
null; -- OK. |
| 247 |
else -- We have to have another command, not text, immediately following. |
| 248 |
--Ada.Text_IO.Put_Line (" NO - plain text found"); |
| 249 |
return False; |
| 250 |
end if; |
| 251 |
elsif Our_Line(I) in 'a'..'z' or else |
| 252 |
Our_Line(I) in '0'..'9' or else |
| 253 |
Our_Line(I) = '-' or else |
| 254 |
Our_Line(I) = '\' then |
| 255 |
null; -- OK. |
| 256 |
else |
| 257 |
--Ada.Text_IO.Put_Line (" NO - odd character at" & Natural'Image(I)); |
| 258 |
return False; -- Don't know what this is. |
| 259 |
end if; |
| 260 |
end loop; |
| 261 |
--Ada.Text_IO.Put_Line (" OK"); |
| 262 |
return True; -- OK if we get here. |
| 263 |
end Possible_Style; |
| 264 |
|
| 265 |
procedure Start_Style (Style_Code_Loc, End_of_Style : Natural) is |
| 266 |
-- Generate an appropriate style operation. |
| 267 |
-- We've already checked the basic legality of the style. |
| 268 |
-- Set Working and Last_Output as needed. |
| 269 |
begin |
| 270 |
-- We can assume that the style code has enough room. |
| 271 |
if Our_Line(Style_Code_Loc..Style_Code_Loc+4) = "\s15\" then |
| 272 |
-- Normal style (called "TP Text para") |
| 273 |
--Ada.Text_IO.Put_Line (" Normal style"); |
| 274 |
if Current_Paragraph_Style /= Normal then |
| 275 |
Clean_Buffer_and_Close_Old_Style; |
| 276 |
--Ada.Text_IO.Put_Line (File, "@begin[Intro]"); -- ** Temp. |
| 277 |
Current_Paragraph_Style := Normal; |
| 278 |
-- else no change needed. |
| 279 |
end if; |
| 280 |
Styles_Found(Normal) := Styles_Found(Normal) + 1; |
| 281 |
-- We just discard the entire style mess. |
| 282 |
Last_Output := End_of_Style; |
| 283 |
Working := Last_Output + 1; |
| 284 |
|
| 285 |
elsif Our_Line(Style_Code_Loc..Style_Code_Loc+4) = "\s25\" then |
| 286 |
-- Example style (called "PP Program para") |
| 287 |
--Ada.Text_IO.Put_Line (" Example style"); |
| 288 |
if Current_Paragraph_Style /= Example then |
| 289 |
Clean_Buffer_and_Close_Old_Style; |
| 290 |
Ada.Text_IO.Put_Line (File, "@begin[Example]"); |
| 291 |
Current_Paragraph_Style := Example; |
| 292 |
-- else no change needed. |
| 293 |
end if; |
| 294 |
Styles_Found(Example) := Styles_Found(Example) + 1; |
| 295 |
-- We just discard the entire style mess. |
| 296 |
Last_Output := End_of_Style; |
| 297 |
Working := Last_Output + 1; |
| 298 |
|
| 299 |
elsif Our_Line(Style_Code_Loc..Style_Code_Loc+4) = "\s32\" then |
| 300 |
-- Description style (called "BP Bullet", but often |
| 301 |
-- not used for bullets) |
| 302 |
--Ada.Text_IO.Put_Line (" Description style"); |
| 303 |
if Current_Paragraph_Style /= Description then |
| 304 |
Clean_Buffer_and_Close_Old_Style; |
| 305 |
Ada.Text_IO.Put_Line (File, "@begin[Description]"); |
| 306 |
Current_Paragraph_Style := Description; |
| 307 |
-- else no change needed. |
| 308 |
end if; |
| 309 |
Styles_Found(Description) := Styles_Found(Description) + 1; |
| 310 |
-- We just discard the entire style mess. |
| 311 |
Last_Output := End_of_Style; |
| 312 |
Working := Last_Output + 1; |
| 313 |
|
| 314 |
else -- Unknown style. |
| 315 |
--Ada.Text_IO.Put_Line (" Unknown style: code = " & Our_Line(Style_Code_Loc..Style_Code_Loc+4)); |
| 316 |
if Current_Paragraph_Style /= Unknown then |
| 317 |
Clean_Buffer_and_Close_Old_Style; |
| 318 |
Current_Paragraph_Style := Unknown; |
| 319 |
-- else no change needed. |
| 320 |
end if; |
| 321 |
Styles_Found(Unknown) := Styles_Found(Unknown) + 1; |
| 322 |
-- We leave the style mess for analysis. |
| 323 |
Working := Working + 1; |
| 324 |
end if; |
| 325 |
end Start_Style; |
| 326 |
|
| 327 |
begin |
| 328 |
Paragraphs := Paragraphs + 1; |
| 329 |
Chars_on_Line := 0; |
| 330 |
while Working <= Our_Line'Last loop |
| 331 |
if Our_Line(Working) = '{' then |
| 332 |
-- Start an RTF section. |
| 333 |
-- Output everything to here: |
| 334 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 335 |
--Ada.Text_IO.Put_Line("Open Brace on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working-1)); |
| 336 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 337 |
|
| 338 |
-- Stack this opening: |
| 339 |
Current_Brace := Current_Brace + 1; |
| 340 |
if Current_Brace > Deepest_Brace_Nesting then |
| 341 |
Deepest_Brace_Nesting := Current_Brace; |
| 342 |
end if; |
| 343 |
|
| 344 |
if Working+1 > Our_Line'Last or else |
| 345 |
Our_Line(Working+1) /= '\' then |
| 346 |
-- No command here, so it is not interesting (ever) |
| 347 |
-- and we might as well remove it. |
| 348 |
Brace_Info_Stack(Current_Brace).Kind := Empty; |
| 349 |
Last_Output := Working; -- Skip the brace. |
| 350 |
--Ada.Text_IO.Put_Line("Empty Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 351 |
|
| 352 |
elsif Working+3 <= Our_Line'Last and then |
| 353 |
Our_Line(Working..Working+3) = "{\b " then |
| 354 |
Brace_Info_Stack(Current_Brace).Kind := Bold; |
| 355 |
Ada.Text_IO.Put (File, "@b["); |
| 356 |
Chars_on_Line := Chars_on_Line + 3; |
| 357 |
Last_Output := Working+3; |
| 358 |
--Ada.Text_IO.Put_Line("Bold Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 359 |
|
| 360 |
elsif Working+3 <= Our_Line'Last and then |
| 361 |
Our_Line(Working..Working+3) = "{\i " then |
| 362 |
Brace_Info_Stack(Current_Brace).Kind := Italic; |
| 363 |
Ada.Text_IO.Put (File, "@i["); |
| 364 |
Chars_on_Line := Chars_on_Line + 3; |
| 365 |
Last_Output := Working+3; |
| 366 |
--Ada.Text_IO.Put_Line("Italic Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 367 |
|
| 368 |
elsif Working+6 <= Our_Line'Last and then |
| 369 |
Our_Line(Working..Working+6) = "{\line " then |
| 370 |
Brace_Info_Stack(Current_Brace).Kind := Line_Break; |
| 371 |
Ada.Text_IO.New_Line (File); |
| 372 |
Chars_on_Line := 0; -- Start over. |
| 373 |
Last_Output := Working+6; |
| 374 |
--Ada.Text_IO.Put_Line("Line_Break Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 375 |
|
| 376 |
elsif Working+5 <= Our_Line'Last and then |
| 377 |
Our_Line(Working..Working+5) = "{\tab " then |
| 378 |
Brace_Info_Stack(Current_Brace).Kind := Tab; |
| 379 |
Ada.Text_IO.Put (File, "@\"); |
| 380 |
Chars_on_Line := Chars_on_Line + 2; |
| 381 |
Last_Output := Working+5; |
| 382 |
--Ada.Text_IO.Put_Line("Tab Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 383 |
|
| 384 |
elsif Working+14 <= Our_Line'Last and then |
| 385 |
Our_Line(Working..Working+14) = "{\cs16\f1\fs20 " then |
| 386 |
-- \cs16 = "PC Program char" style. Could look for |
| 387 |
-- that early on to find the number. Same with |
| 388 |
-- "\f1" (Arial). But they might be constant, and |
| 389 |
-- that would be wasted work. |
| 390 |
Brace_Info_Stack(Current_Brace).Kind := Exam; |
| 391 |
Ada.Text_IO.Put (File, "@exam["); |
| 392 |
Chars_on_Line := Chars_on_Line + 6; |
| 393 |
Last_Output := Working+14; |
| 394 |
--Ada.Text_IO.Put_Line("Exam Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 395 |
|
| 396 |
elsif Working+8 <= Our_Line'Last and then |
| 397 |
Our_Line(Working..Working+8) = "{\cs20\b " then |
| 398 |
-- \cs20 = "RW Reserved word" style. Could look for |
| 399 |
-- that early on to find the number. |
| 400 |
Brace_Info_Stack(Current_Brace).Kind := Key; |
| 401 |
Ada.Text_IO.Put (File, "@key["); |
| 402 |
Chars_on_Line := Chars_on_Line + 5; |
| 403 |
Last_Output := Working+8; |
| 404 |
--Ada.Text_IO.Put_Line("Key (in example) Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 405 |
|
| 406 |
elsif Working+16 <= Our_Line'Last and then |
| 407 |
Our_Line(Working..Working+16) = "{\cs20\b\f1\fs20 " then |
| 408 |
-- \cs20 = "RW Reserved word" style. \f1 = Arial |
| 409 |
-- font. |
| 410 |
Brace_Info_Stack(Current_Brace).Kind := Key; |
| 411 |
Ada.Text_IO.Put (File, "@key["); |
| 412 |
Chars_on_Line := Chars_on_Line + 5; |
| 413 |
Last_Output := Working+16; |
| 414 |
--Ada.Text_IO.Put_Line("Key (in text) Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 415 |
|
| 416 |
elsif Working+8 <= Our_Line'Last and then |
| 417 |
Our_Line(Working..Working+8) = "{\cs24\i " then |
| 418 |
-- \cs24 = "CT Comment" style. |
| 419 |
Brace_Info_Stack(Current_Brace).Kind := ExamCom; |
| 420 |
Ada.Text_IO.Put (File, "@examcom["); |
| 421 |
Chars_on_Line := Chars_on_Line + 9; |
| 422 |
Last_Output := Working+8; |
| 423 |
--Ada.Text_IO.Put_Line("ExamCom Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 424 |
|
| 425 |
else |
| 426 |
Brace_Info_Stack(Current_Brace).Kind := Unknown; |
| 427 |
Ada.Text_IO.Put (File, '{'); |
| 428 |
Chars_on_Line := Chars_on_Line + 1; |
| 429 |
Last_Output := Working; |
| 430 |
--Ada.Text_IO.Put_Line("Unknown Open Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 431 |
end if; |
| 432 |
Brace_Kinds_Found(Brace_Info_Stack(Current_Brace).Kind) := |
| 433 |
Brace_Kinds_Found(Brace_Info_Stack(Current_Brace).Kind) + 1; |
| 434 |
Working := Last_Output + 1; |
| 435 |
|
| 436 |
elsif Our_Line(Working) = '}' then |
| 437 |
-- End an RTF section. |
| 438 |
-- Output everything to here: |
| 439 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 440 |
--Ada.Text_IO.Put_Line("Close Brace on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working-1)); |
| 441 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 442 |
Last_Output := Working; -- We're including this character. |
| 443 |
case Brace_Info_Stack(Current_Brace).Kind is |
| 444 |
when Unknown => |
| 445 |
Ada.Text_IO.Put (File, '}'); |
| 446 |
--Ada.Text_IO.Put_Line("Unknown Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 447 |
Chars_on_Line := Chars_on_Line + 1; |
| 448 |
when Empty => |
| 449 |
-- Just remove this one. |
| 450 |
--Ada.Text_IO.Put_Line("Empty Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 451 |
null; |
| 452 |
when Bold => |
| 453 |
Ada.Text_IO.Put (File, ']'); |
| 454 |
--Ada.Text_IO.Put_Line("Bold Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 455 |
Chars_on_Line := Chars_on_Line + 1; |
| 456 |
when Italic => |
| 457 |
Ada.Text_IO.Put (File, ']'); |
| 458 |
--Ada.Text_IO.Put_Line("Italic Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 459 |
Chars_on_Line := Chars_on_Line + 1; |
| 460 |
when Line_Break => |
| 461 |
null; |
| 462 |
--Ada.Text_IO.Put_Line("Link_Break Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 463 |
when Tab => |
| 464 |
null; |
| 465 |
--Ada.Text_IO.Put_Line("Tab Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 466 |
when Exam => |
| 467 |
Ada.Text_IO.Put (File, ']'); |
| 468 |
--Ada.Text_IO.Put_Line("Exam Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 469 |
Chars_on_Line := Chars_on_Line + 1; |
| 470 |
when ExamCom => |
| 471 |
Ada.Text_IO.Put (File, ']'); |
| 472 |
--Ada.Text_IO.Put_Line("ExamCom Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 473 |
Chars_on_Line := Chars_on_Line + 1; |
| 474 |
when Key => |
| 475 |
Ada.Text_IO.Put (File, ']'); |
| 476 |
--Ada.Text_IO.Put_Line("Key Close Brace on Line" & Line_Count'Image(Cursor.Number)); |
| 477 |
Chars_on_Line := Chars_on_Line + 1; |
| 478 |
end case; |
| 479 |
Current_Brace := Current_Brace - 1; |
| 480 |
Working := Working + 1; |
| 481 |
|
| 482 |
elsif Our_Line(Working) = ' ' then |
| 483 |
-- Possible break here. |
| 484 |
if Chars_on_Line + (Working - (Last_Output+1) + 1) > 65 then |
| 485 |
-- We need a break here; write it. |
| 486 |
Ada.Text_IO.Put_Line (File, Our_Line(Last_Output+1..Working)); |
| 487 |
--Ada.Text_IO.Put_Line("Soft line break on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 488 |
Chars_on_Line := 0; -- Start over. |
| 489 |
Last_Output := Working; |
| 490 |
Working := Working + 1; |
| 491 |
else -- Don't need a break. |
| 492 |
Working := Working + 1; |
| 493 |
end if; |
| 494 |
|
| 495 |
elsif Our_Line(Working) = '\' then |
| 496 |
-- RTF control word or symbol; see whether it is |
| 497 |
-- interesting. |
| 498 |
if Working+5 <= Our_Line'Last and then |
| 499 |
Our_Line(Working..Working+5) = "\line " then |
| 500 |
-- Line break in a paragraph (without an open |
| 501 |
-- brace). Output a New_line. |
| 502 |
Ada.Text_IO.Put_Line (File, Our_Line(Last_Output+1..Working-1)); |
| 503 |
--Ada.Text_IO.Put_Line("Line break on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 504 |
Intraparagraph_Line_Breaks := Intraparagraph_Line_Breaks + 1; |
| 505 |
Chars_on_Line := 0; -- Start over. |
| 506 |
Last_Output := Working+5; |
| 507 |
Working := Last_Output + 1; |
| 508 |
|
| 509 |
elsif Working+4 <= Our_Line'Last and then |
| 510 |
Our_Line(Working..Working+4) = "\tab " then |
| 511 |
-- Tab inside of a paragraph. Output the previous text and a tab command. |
| 512 |
-- (Note: We don't define any tabstops here; that |
| 513 |
-- will need to be done by hand as needed). |
| 514 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 515 |
--Ada.Text_IO.Put_Line("Tab on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 516 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 517 |
Ada.Text_IO.Put (File, "@\"); |
| 518 |
Chars_on_Line := Chars_on_Line + 2; |
| 519 |
Intraparagraph_Tabs := Intraparagraph_Tabs + 1; |
| 520 |
Last_Output := Working+4; |
| 521 |
Working := Last_Output + 1; |
| 522 |
|
| 523 |
elsif Working+1 <= Our_Line'Last and then |
| 524 |
(Our_Line(Working+1) = '\' or else |
| 525 |
Our_Line(Working+1) = '{' or else |
| 526 |
Our_Line(Working+1) = '}') then |
| 527 |
-- Escaped character. Output the previous text and |
| 528 |
-- the literal character. (We have to do this to |
| 529 |
-- prevent the escaped character from being |
| 530 |
-- acted on as if it is a control or group marker). |
| 531 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 532 |
--Ada.Text_IO.Put_Line("Escaped char on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 533 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 534 |
Escaped_Chars := Escaped_Chars + 1; |
| 535 |
Last_Output := Working; |
| 536 |
Working := Working + 2; |
| 537 |
-- We leave the literal character in the output buffer for later writing. |
| 538 |
|
| 539 |
elsif Working+7 <= Our_Line'Last and then |
| 540 |
Our_Line(Working..Working+7) = "\endash " then |
| 541 |
-- Line break in a paragraph. Output a New_line. |
| 542 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 543 |
--Ada.Text_IO.Put_Line("Endash on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 544 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 545 |
Ada.Text_IO.Put (File, "@en"); |
| 546 |
Chars_on_Line := Chars_on_Line + 3; |
| 547 |
EN_Dashes := EN_Dashes + 1; |
| 548 |
Last_Output := Working+7; |
| 549 |
Working := Last_Output + 1; |
| 550 |
|
| 551 |
elsif Working+7 <= Our_Line'Last and then |
| 552 |
Our_Line(Working..Working+7) = "\emdash " then |
| 553 |
-- Line break in a paragraph. Output a New_line. |
| 554 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Working-1)); |
| 555 |
--Ada.Text_IO.Put_Line("Emdash on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Working)); |
| 556 |
Chars_on_Line := Chars_on_Line + ((Working-1) - (Last_Output+1) + 1); |
| 557 |
Ada.Text_IO.Put (File, "@em"); |
| 558 |
Chars_on_Line := Chars_on_Line + 3; |
| 559 |
EM_Dashes := EM_Dashes + 1; |
| 560 |
Last_Output := Working+7; |
| 561 |
Working := Last_Output + 1; |
| 562 |
|
| 563 |
elsif Working+5 <= Our_Line'Last and then |
| 564 |
Our_Line(Working..Working+5) = "\pard\" then |
| 565 |
-- Starts a (full) paragraph style. |
| 566 |
--Ada.Text_IO.Put_Line("Paragraph style on Line" & Line_Count'Image(Cursor.Number)); |
| 567 |
declare |
| 568 |
End_of_Style : Natural := |
| 569 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 570 |
"\langfenp1033 ") + 13; |
| 571 |
-- For some reason, all of the styles end with this. |
| 572 |
Style_Code : Natural := |
| 573 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 574 |
"\s"); |
| 575 |
-- The style code is generally first. |
| 576 |
begin |
| 577 |
--Ada.Text_IO.Put_Line("Style_Code=" & Natural'Image(Style_Code) & |
| 578 |
-- "; End_of_Style=" & Natural'Image(End_of_Style)); |
| 579 |
if Style_Code = 0 or else End_of_Style = 13 or else |
| 580 |
Style_Code > End_of_Style or else |
| 581 |
(not Possible_Style (Working, End_of_Style)) then |
| 582 |
-- No style code, no end of style, the style |
| 583 |
-- code comes after the end of the style, or |
| 584 |
-- the style has problems. (In which case, we are |
| 585 |
-- probably taking too much as the style). |
| 586 |
-- This is unknown. Do nothing and just output the character. |
| 587 |
Working := Working + 1; |
| 588 |
else |
| 589 |
Start_Style (Style_Code, End_of_Style); |
| 590 |
-- This sets Working and Last_Output appropriately. |
| 591 |
end if; |
| 592 |
end; |
| 593 |
|
| 594 |
elsif Working+5 <= Our_Line'Last and then |
| 595 |
Our_Line(Working..Working+5) = "\pard " then |
| 596 |
-- Starts a (short) paragraph style. |
| 597 |
--Ada.Text_IO.Put_Line("Short paragraph style on Line" & Line_Count'Image(Cursor.Number)); |
| 598 |
declare |
| 599 |
End_of_Style : Natural := |
| 600 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 601 |
"\lin0\itap0 "); |
| 602 |
-- For some reason, all of the styles end with this. |
| 603 |
Style_Code : Natural := |
| 604 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 605 |
"\s"); |
| 606 |
-- The style code is generally first. |
| 607 |
begin |
| 608 |
if End_of_Style = 0 then |
| 609 |
End_of_Style := |
| 610 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 611 |
"\lin480\itap0 "); |
| 612 |
if End_of_Style = 0 then |
| 613 |
End_of_Style := |
| 614 |
Ada.Strings.Fixed.Index (Our_Line(Working..Our_Line'Last), |
| 615 |
"\lin360\itap0 "); |
| 616 |
if End_of_Style /= 0 then |
| 617 |
End_of_Style := End_of_Style + 13; |
| 618 |
end if; |
| 619 |
else |
| 620 |
End_of_Style := End_of_Style + 13; |
| 621 |
end if; |
| 622 |
else |
| 623 |
End_of_Style := End_of_Style + 11; |
| 624 |
end if; |
| 625 |
|
| 626 |
if Style_Code = 0 or else End_of_Style = 0 or else |
| 627 |
Style_Code > End_of_Style or else |
| 628 |
(not Possible_Style (Working, End_of_Style)) then |
| 629 |
-- No style code, no end of style, the style |
| 630 |
-- code comes after the end of the style, or |
| 631 |
-- the style has problems. (In which case, we are |
| 632 |
-- probably taking too much as the style). |
| 633 |
-- This is unknown. Do nothing and just output the character. |
| 634 |
Working := Working + 1; |
| 635 |
else |
| 636 |
Start_Style (Style_Code, End_of_Style); |
| 637 |
-- This sets Working and Last_Output appropriately. |
| 638 |
end if; |
| 639 |
end; |
| 640 |
|
| 641 |
else -- Don't recognize it, just output it (later). |
| 642 |
Working := Working + 1; |
| 643 |
end if; |
| 644 |
else -- Just output it (later). |
| 645 |
Working := Working + 1; |
| 646 |
end if; |
| 647 |
end loop; |
| 648 |
-- Output any remaining text (it can't need to be broken): |
| 649 |
Ada.Text_IO.Put (File, Our_Line(Last_Output+1..Our_Line'Last)); |
| 650 |
--Ada.Text_IO.Put_Line("Remaining text on Line" & Line_Count'Image(Cursor.Number) & ": output=" & Our_Line(Last_Output+1..Our_Line'Last)); |
| 651 |
end; |
| 652 |
Ada.Text_IO.New_Line (File, 2); -- Double space. |
| 653 |
Cursor := Cursor.Next; |
| 654 |
end loop; |
| 655 |
Close_Old_Style; -- Close the last style. |
| 656 |
Ada.Text_IO.Close (File); |
| 657 |
Free_All (Data); |
| 658 |
-- Display statistics: |
| 659 |
Ada.Text_IO.Put_Line ("Conversion statistics:"); |
| 660 |
Ada.Text_IO.Put_Line (" Total paragraphs:" & Natural'Image(Paragraphs)); |
| 661 |
Ada.Text_IO.Put_Line (" Deepest nesting:" & Natural'Image(Deepest_Brace_Nesting)); |
| 662 |
for I in RTF_Group_Kind loop |
| 663 |
Ada.Text_IO.Put_Line (" Kind " & RTF_Group_Kind'Image(I) & |
| 664 |
":" & Natural'Image(Brace_Kinds_Found(I))); |
| 665 |
end loop; |
| 666 |
Ada.Text_IO.Put_Line (" Escaped characters found:" & Natural'Image(Escaped_Chars)); |
| 667 |
Ada.Text_IO.Put_Line (" EM Dashes found:" & Natural'Image(EM_Dashes)); |
| 668 |
Ada.Text_IO.Put_Line (" EN Dashes found:" & Natural'Image(EN_Dashes)); |
| 669 |
Ada.Text_IO.Put_Line (" Line breaks inside of paragraphs:" & Natural'Image(Intraparagraph_Line_Breaks)); |
| 670 |
Ada.Text_IO.Put_Line (" Tabs inside of paragraphs:" & Natural'Image(Intraparagraph_Tabs)); |
| 671 |
for I in Paragraph_Styles loop |
| 672 |
Ada.Text_IO.Put_Line (" Style " & Paragraph_Styles'Image(I) & |
| 673 |
":" & Natural'Image(Styles_Found(I))); |
| 674 |
end loop; |
| 675 |
end Process_and_Write_File; |
| 676 |
|
| 677 |
File_Contents : Line_Access; |
| 678 |
|
| 679 |
begin |
| 680 |
Ada.Text_IO.Put_Line ("Convert " & |
| 681 |
Ada.Command_Line.Argument(1) & ".rtf to " & |
| 682 |
Ada.Command_Line.Argument(1) & ".mss"); |
| 683 |
Load_File (Ada.Command_Line.Argument(1) & ".rtf", File_Contents); |
| 684 |
Process_and_Write_File (Ada.Command_Line.Argument(1) & ".mss", File_Contents); |
| 685 |
Ada.Text_IO.Put_Line ("Conversion completed"); |
| 686 |
end Rtf2Form; |