Minor reformatting.
[diouzhtu.git] / diouzhtu / src / diouzhtu-code.adb
blobc47e592b57e56f747a7f890ff05281771e4f384d
1 ------------------------------------------------------------------------------
2 -- Diouzhtu --
3 -- --
4 -- Copyright (C) 2007 --
5 -- Olivier Ramonat --
6 -- --
7 -- This library is free software; you can redistribute it and/or modify --
8 -- it under the terms of the GNU General Public License as published by --
9 -- the Free Software Foundation; either version 2 of the License, or (at --
10 -- your option) any later version. --
11 -- --
12 -- This library is distributed in the hope that it will be useful, but --
13 -- WITHOUT ANY WARRANTY; without even the implied warranty of --
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
15 -- General Public License for more details. --
16 -- --
17 -- You should have received a copy of the GNU General Public License --
18 -- along with this library; if not, write to the Free Software Foundation, --
19 -- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --
20 ------------------------------------------------------------------------------
22 with Diouzhtu.Attribute;
23 with GNAT.Regpat;
25 package body Diouzhtu.Code is
27 use GNAT.Regpat;
29 function End_Code (Block : in String) return String;
30 -- Search for a "end code." to close a block code
32 --------------
33 -- End_Code --
34 --------------
36 function End_Code (Block : in String) return String is
37 Extract : constant Pattern_Matcher :=
38 Compile (Expression => ".*?(end code.)",
39 Flags => Case_Insensitive + Single_Line);
40 Matches : Match_Array (0 .. 1);
41 Result : Unbounded_String := Null_Unbounded_String;
42 begin
43 Match (Extract, Block, Matches);
44 if Matches (0) = No_Match then
45 return "";
46 end if;
48 if Block'First < Matches (1).First - 1 then
49 if Block (Matches (1).First - 1) = ASCII.LF then
50 -- Skip the last Lf when end code. at the end of a block
51 Append (Result, Block (Block'First .. Matches (1).First - 2));
52 else
53 Append (Result, Block (Block'First .. Matches (1).First - 1));
54 end if;
55 else
56 -- This is a new block. Adds the missing blank line
57 Append (Result, ASCII.LF & ASCII.LF);
58 end if;
60 return To_String (Result) & "</code></pre></p>" & ASCII.LF;
61 end End_Code;
63 -----------
64 -- Parse --
65 -----------
67 procedure Parse
68 (Wiki : in Wiki_Information;
69 Block : in String;
70 Is_Code_Block : in out Boolean;
71 Result : out Unbounded_String)
73 procedure Begin_Code;
74 -- Search for a begin code block tag "code." else call Diouzhtu.Parse
76 procedure Begin_Code is
77 Extract : constant Pattern_Matcher :=
78 Compile (Expression => "^code(_[a-zA-Z]+?)??" &
79 Attribute.Get_Pattern & "\.\s(.*?)$",
80 Flags => Case_Insensitive + Single_Line);
81 Count : constant Match_Count := Paren_Count (Extract);
82 Matches : Match_Array (0 .. Count);
83 begin
84 Match (Extract, Block, Matches);
85 if Matches (0) = No_Match then
86 Append (Result, Diouzhtu.Parse (Wiki, Block_Level, Block));
87 else
88 -- This is a code block
90 Is_Code_Block := True;
92 Result := To_Unbounded_String ("<p><pre><code");
94 if Matches (2) /= No_Match then
95 Append
96 (Result, Attribute.Extract
97 (Content =>
98 Block (Matches (2).First .. Matches (2).Last),
99 Add_Class =>
100 Block (Matches (1).First + 1 .. Matches (1).Last)));
101 elsif Matches (1) /= No_Match then
102 Append (Result, " class='" &
103 Block (Matches (1).First + 1 .. Matches (1).Last) &
104 "'");
105 end if;
107 Append (Result, ">");
109 if Matches (Count) /= No_Match
110 and then Matches (Count).First < Block'Last then
112 End_Of_Block :
113 declare
114 End_Code_Block : constant String
115 := End_Code (Block (Matches (Count).First .. Block'Last));
116 begin
117 if End_Code_Block /= "" then
118 Is_Code_Block := False;
119 Append (Result, End_Code_Block);
120 else
121 Append (Result,
122 Block (Matches (Count).First .. Block'Last));
123 end if;
124 end End_Of_Block;
125 end if;
126 end if;
127 end Begin_Code;
129 begin
130 Result := Null_Unbounded_String;
132 if not Is_Code_Block then
133 Begin_Code;
134 else
135 End_Of_Block :
136 declare
137 End_Code_Block : constant String := End_Code (Block);
138 begin
139 if End_Code_Block /= "" then
140 Is_Code_Block := False;
141 Append (Result, End_Code_Block);
142 else
143 Append (Result, ASCII.LF & ASCII.LF & Block);
144 end if;
145 end End_Of_Block;
146 end if;
147 end Parse;
149 end Diouzhtu.Code;