1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2007 --
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. --
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. --
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
;
25 package body Diouzhtu
.Code
is
29 function End_Code
(Block
: in String) return String;
30 -- Search for a "end code." to close a block code
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
;
43 Match
(Extract
, Block
, Matches
);
44 if Matches
(0) = No_Match
then
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));
53 Append
(Result
, Block
(Block
'First .. Matches
(1).First
- 1));
56 -- This is a new block. Adds the missing blank line
57 Append
(Result
, ASCII
.LF
& ASCII
.LF
);
60 return To_String
(Result
) & "</code></pre></p>" & ASCII
.LF
;
68 (Wiki
: in Wiki_Information
;
70 Is_Code_Block
: in out Boolean;
71 Result
: out Unbounded_String
)
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
);
84 Match
(Extract
, Block
, Matches
);
85 if Matches
(0) = No_Match
then
86 Append
(Result
, Diouzhtu
.Parse
(Wiki
, Block_Level
, Block
));
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
96 (Result
, Attribute
.Extract
98 Block
(Matches
(2).First
.. Matches
(2).Last
),
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
) &
107 Append
(Result
, ">");
109 if Matches
(Count
) /= No_Match
110 and then Matches
(Count
).First
< Block
'Last then
114 End_Code_Block
: constant String
115 := End_Code
(Block
(Matches
(Count
).First
.. Block
'Last));
117 if End_Code_Block
/= "" then
118 Is_Code_Block
:= False;
119 Append
(Result
, End_Code_Block
);
122 Block
(Matches
(Count
).First
.. Block
'Last));
130 Result
:= Null_Unbounded_String
;
132 if not Is_Code_Block
then
137 End_Code_Block
: constant String := End_Code
(Block
);
139 if End_Code_Block
/= "" then
140 Is_Code_Block
:= False;
141 Append
(Result
, End_Code_Block
);
143 Append
(Result
, ASCII
.LF
& ASCII
.LF
& Block
);