Refactor to use callbacks
This commit is contained in:
parent
384443692c
commit
6bfb264532
59
source/pandoc-blocks.adb
Normal file
59
source/pandoc-blocks.adb
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
package body Pandoc.Blocks is
|
||||||
|
|
||||||
|
function Create_Block
|
||||||
|
(T : Object_Type;
|
||||||
|
Content : League.JSON.Values.JSON_Value)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
begin
|
||||||
|
return Create_Block (T, Empty_Attribute, Content);
|
||||||
|
end Create_Block;
|
||||||
|
|
||||||
|
function Create_Block
|
||||||
|
(T : Object_Type;
|
||||||
|
Attr : League.JSON.Values.JSON_Value;
|
||||||
|
Content : League.JSON.Values.JSON_Value)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
Block : League.JSON.Objects.JSON_Object;
|
||||||
|
New_Content : League.JSON.Arrays.JSON_Array;
|
||||||
|
begin
|
||||||
|
|
||||||
|
New_Content.Append (Attr);
|
||||||
|
New_Content.Append (Content);
|
||||||
|
|
||||||
|
Block.Insert
|
||||||
|
(Type_String, League.JSON.Values.To_JSON_Value( Type_To_String (T)));
|
||||||
|
|
||||||
|
Block.Insert (Content_String, New_Content);
|
||||||
|
|
||||||
|
return Block.To_JSON_Value;
|
||||||
|
end Create_Block;
|
||||||
|
|
||||||
|
function Raw_Block
|
||||||
|
(T : League.Strings.Universal_String;
|
||||||
|
Content : League.Strings.Universal_String)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
begin
|
||||||
|
return Raw_Block (Empty_Attribute, T, Content);
|
||||||
|
end Raw_Block;
|
||||||
|
|
||||||
|
function Raw_Block
|
||||||
|
(Attr : League.JSON.Values.JSON_Value;
|
||||||
|
T : League.Strings.Universal_String;
|
||||||
|
Content : League.Strings.Universal_String
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
Formatted_Content : League.JSON.Arrays.JSON_Array;
|
||||||
|
begin
|
||||||
|
Formatted_Content.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (T));
|
||||||
|
|
||||||
|
Formatted_Content.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Content));
|
||||||
|
|
||||||
|
return Create_Block (Block_RawBlock, Attr, Formatted_Content);
|
||||||
|
end Raw_Block;
|
||||||
|
|
||||||
|
end Pandoc.Blocks;
|
||||||
28
source/pandoc-blocks.ads
Normal file
28
source/pandoc-blocks.ads
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
with League.JSON.Values;
|
||||||
|
with League.JSON.Arrays.JSON_Array;
|
||||||
|
|
||||||
|
package Pandoc.Blocks is
|
||||||
|
|
||||||
|
function Create_Block
|
||||||
|
(T : Object_Type;
|
||||||
|
Content : League.JSON.Values.JSON_Value)
|
||||||
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
|
function Create_Block
|
||||||
|
(T : Object_Type;
|
||||||
|
Attr : League.JSON.Values.JSON_Value;
|
||||||
|
Content : League.JSON.Values.JSON_Value)
|
||||||
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
|
function Raw_Block
|
||||||
|
(T : League.Strings.Universal_String;
|
||||||
|
Content : League.Strings.Universal_String)
|
||||||
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
|
function Raw_Block
|
||||||
|
(Attr : League.JSON.Values.JSON_Value;
|
||||||
|
T : League.Strings.Universal_String;
|
||||||
|
Content : League.Strings.Universal_String)
|
||||||
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
|
end Pandoc.Blocks;
|
||||||
|
|
@ -1,183 +1,86 @@
|
||||||
pragma Ada_2022;
|
|
||||||
|
|
||||||
package body Pandoc is
|
package body Pandoc is
|
||||||
|
|
||||||
function Attr
|
|
||||||
(Key : League.Strings.Universal_String;
|
|
||||||
Value : League.Strings.Universal_String;
|
|
||||||
Id : League.Strings.Universal_String :=
|
|
||||||
League.Strings.Empty_Universal_String)
|
|
||||||
return League.JSON.Values.JSON_Value is
|
|
||||||
(Attr (Id, [Key], [Value]));
|
|
||||||
|
|
||||||
function Attr
|
|
||||||
(Id : League.Strings.Universal_String;
|
|
||||||
Key : String_Array;
|
|
||||||
Value : String_Array) return League.JSON.Values.JSON_Value
|
|
||||||
is
|
|
||||||
Outer : League.JSON.Arrays.JSON_Array;
|
|
||||||
Other : League.JSON.Arrays.JSON_Array;
|
|
||||||
Inner : League.JSON.Arrays.JSON_Array;
|
|
||||||
begin
|
|
||||||
Outer.Append (League.JSON.Values.To_JSON_Value (Id));
|
|
||||||
Outer.Append (Other.To_JSON_Value);
|
|
||||||
|
|
||||||
for K in Key'Range loop
|
|
||||||
declare
|
|
||||||
Pair : League.JSON.Arrays.JSON_Array;
|
|
||||||
begin
|
|
||||||
Pair.Append (League.JSON.Values.To_JSON_Value (Key (K)));
|
|
||||||
Pair.Append (League.JSON.Values.To_JSON_Value (Value (K)));
|
|
||||||
Inner.Append (Pair.To_JSON_Value);
|
|
||||||
end;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
Outer.Append (Inner.To_JSON_Value);
|
|
||||||
|
|
||||||
return Outer.To_JSON_Value;
|
|
||||||
end Attr;
|
|
||||||
|
|
||||||
function Div (
|
|
||||||
Attr : League.JSON.Values.JSON_Value;
|
|
||||||
Content : Content_Arr) return League.JSON.Values.JSON_Value
|
|
||||||
is
|
|
||||||
Block : League.JSON.Objects.JSON_Object;
|
|
||||||
Out_Content : League.JSON.Arrays.JSON_Array;
|
|
||||||
Content_Block : League.JSON.Arrays.JSON_Array;
|
|
||||||
begin
|
|
||||||
Block.Insert (
|
|
||||||
Type_String,
|
|
||||||
League.JSON.Values.To_JSON_Value (
|
|
||||||
Obj_String_Representation (Block_Div)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
for C in Content'Range loop
|
|
||||||
Content_Block.Append (Content (C));
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
Out_Content.Append (Attr);
|
|
||||||
Out_Content.Append (Content_Block.To_JSON_Value);
|
|
||||||
|
|
||||||
Block.Insert (
|
|
||||||
Content_String,
|
|
||||||
Out_Content.To_JSON_Value
|
|
||||||
);
|
|
||||||
|
|
||||||
return Block.To_JSON_Value;
|
|
||||||
end Div;
|
|
||||||
|
|
||||||
function Div
|
|
||||||
(Attr : League.JSON.Values.JSON_Value;
|
|
||||||
Content : League.JSON.Values.JSON_Value)
|
|
||||||
return League.JSON.Values.JSON_Value
|
|
||||||
is
|
|
||||||
Block : League.JSON.Objects.JSON_Object;
|
|
||||||
Out_Content : League.JSON.Arrays.JSON_Array;
|
|
||||||
begin
|
|
||||||
Block.Insert (
|
|
||||||
Type_String,
|
|
||||||
League.JSON.Values.To_JSON_Value (
|
|
||||||
Obj_String_Representation (Block_Div)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Out_Content.Append (Attr);
|
|
||||||
Out_Content.Append (Content);
|
|
||||||
|
|
||||||
Block.Insert (
|
|
||||||
Content_String,
|
|
||||||
Out_Content.To_JSON_Value
|
|
||||||
);
|
|
||||||
|
|
||||||
return Block.To_JSON_Value;
|
|
||||||
end Div;
|
|
||||||
|
|
||||||
function HTML_Block (Text : League.Strings.Universal_String) return
|
|
||||||
League.JSON.Values.JSON_Value
|
|
||||||
is
|
|
||||||
Output : League.JSON.Objects.JSON_Object;
|
|
||||||
Content_Arr : League.JSON.Arrays.JSON_Array;
|
|
||||||
begin
|
|
||||||
Output.Insert (
|
|
||||||
Type_String,
|
|
||||||
League.JSON.Values.To_JSON_Value (+"RawBlock"));
|
|
||||||
|
|
||||||
Content_Arr.Append (
|
|
||||||
League.JSON.Values.To_JSON_Value (+"html"));
|
|
||||||
|
|
||||||
Content_Arr.Append (
|
|
||||||
League.JSON.Values.To_JSON_Value (Text));
|
|
||||||
|
|
||||||
Output.Insert (
|
|
||||||
Content_String,
|
|
||||||
Content_Arr.To_JSON_Value);
|
|
||||||
|
|
||||||
return Output.To_JSON_Value;
|
|
||||||
end HTML_Block;
|
|
||||||
|
|
||||||
function Plain (Inlines : League.JSON.Values.JSON_Value) return
|
|
||||||
League.JSON.Values.JSON_Value
|
|
||||||
is
|
|
||||||
Output : League.JSON.Objects.JSON_Object;
|
|
||||||
begin
|
|
||||||
Output.Insert (
|
|
||||||
Type_String,
|
|
||||||
League.JSON.Values.To_JSON_Value (+"Plain"));
|
|
||||||
|
|
||||||
Output.Insert (
|
|
||||||
Content_String,
|
|
||||||
Inlines);
|
|
||||||
|
|
||||||
return Output.To_JSON_Value;
|
|
||||||
end Plain;
|
|
||||||
|
|
||||||
function Definition_List (B : League.JSON.Objects.JSON_Object) return
|
|
||||||
League.JSON.Arrays.JSON_Array
|
|
||||||
is
|
|
||||||
Output : League.JSON.Arrays.JSON_Array;
|
|
||||||
Content : constant League.JSON.Arrays.JSON_Array :=
|
|
||||||
B (Content_String).To_Array;
|
|
||||||
begin
|
|
||||||
Output.Append (HTML_Block (+"<dl>"));
|
|
||||||
|
|
||||||
for I in 1 .. Content.Length loop
|
|
||||||
declare
|
|
||||||
Def_Item : constant League.JSON.Arrays.JSON_Array :=
|
|
||||||
Content (I).To_Array;
|
|
||||||
Def_Header : constant League.JSON.Arrays.JSON_Array :=
|
|
||||||
Def_Item (1).To_Array;
|
|
||||||
Def_Content : constant League.JSON.Arrays.JSON_Array :=
|
|
||||||
Def_Item (2).To_Array;
|
|
||||||
begin
|
|
||||||
Output.Append (HTML_Block (+"<dt>"));
|
|
||||||
Output.Append (Plain (Def_Header.To_JSON_Value));
|
|
||||||
Output.Append (HTML_Block (+"</dt><dd>"));
|
|
||||||
|
|
||||||
for J in 1 .. Def_Content.Length loop
|
|
||||||
declare
|
|
||||||
Def_Sub_Content : constant League.JSON.Arrays.JSON_Array :=
|
|
||||||
Def_Content (J).To_Array;
|
|
||||||
begin
|
|
||||||
for K in 1 .. Def_Sub_Content.Length loop
|
|
||||||
Output.Append (Def_Sub_Content (K));
|
|
||||||
end loop;
|
|
||||||
end;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
Output.Append (HTML_Block (+"</dd>"));
|
|
||||||
end;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
Output.Append (HTML_Block (+"</dl>"));
|
|
||||||
|
|
||||||
return Output;
|
|
||||||
|
|
||||||
end Definition_List;
|
|
||||||
|
|
||||||
function Get_Type (B : League.JSON.Objects.JSON_Object)
|
function Get_Type (B : League.JSON.Objects.JSON_Object)
|
||||||
return Object_Type is (Type_Mapping (B (Type_String).To_String));
|
return Object_Type is (Type_Mapping (B (Type_String).To_String));
|
||||||
|
|
||||||
|
function Type_To_String (T : Object_Type)
|
||||||
|
return League.Strings.Universal_String
|
||||||
|
is
|
||||||
|
begin
|
||||||
|
return Obj_String_Representation (T);
|
||||||
|
end Type_To_String;
|
||||||
|
|
||||||
|
function Attr
|
||||||
|
(Id : League.Strings.Universal_String;
|
||||||
|
Key : League.Strings.Universal_String;
|
||||||
|
Value : League.Strings.Universal_String)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
Key_Value_Pair : League.JSON.Arrays.JSON_Array;
|
||||||
|
Attr_Map : League.JSON.Arrays.JSON_Array;
|
||||||
|
Attr : League.JSON.Arrays.JSON_Array;
|
||||||
|
begin
|
||||||
|
Key_Value_Pair.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Key));
|
||||||
|
Key_Value_Pair.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Value));
|
||||||
|
|
||||||
|
Attr_Map.Append (Key_Value_Pair.To_JSON_Value);
|
||||||
|
|
||||||
|
Attr.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Id));
|
||||||
|
|
||||||
|
Attr.Append (Attr_Map.To_JSON_Value);
|
||||||
|
|
||||||
|
return Attr.To_JSON_Value;
|
||||||
|
end Attr;
|
||||||
|
|
||||||
|
function Attr
|
||||||
|
(Id : League.Strings.Universal_String;
|
||||||
|
Key_Value : Key_Value_Pairs)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
Attr_Map : League.JSON.Arrays.JSON_Array;
|
||||||
|
Attr : League.JSON.Arrays.JSON_Array;
|
||||||
|
begin
|
||||||
|
|
||||||
|
for J in Key_Value'Range loop
|
||||||
|
declare
|
||||||
|
Pair : League.JSON.Arrays.JSON_Array;
|
||||||
|
begin
|
||||||
|
Pair.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Key_Value (J).Key));
|
||||||
|
Pair.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Key_Value (J).Value));
|
||||||
|
|
||||||
|
Attr_Map.Append (Pair.To_JSON_Value);
|
||||||
|
end;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
Attr.Append (
|
||||||
|
League.JSON.Values.To_JSON_Value (Id));
|
||||||
|
|
||||||
|
Attr.Append (Attr_Map.To_JSON_Value);
|
||||||
|
|
||||||
|
return Attr.To_JSON_Value;
|
||||||
|
end Attr;
|
||||||
|
|
||||||
|
function Attr
|
||||||
|
(Key : League.Strings.Universal_String;
|
||||||
|
Value : League.Strings.Universal_String)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
begin
|
||||||
|
return Attr (+"", Key, Value);
|
||||||
|
end Attr;
|
||||||
|
|
||||||
|
function Attr (Key_Value : Key_Value_Pairs)
|
||||||
|
return League.JSON.Values.JSON_Value
|
||||||
|
is
|
||||||
|
begin
|
||||||
|
return Attr (+"", Key_Value);
|
||||||
|
end Attr;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
for Key in Object_Type loop
|
for Key in Object_Type loop
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
pragma Ada_2022;
|
||||||
|
|
||||||
with Ada.Containers;
|
with Ada.Containers;
|
||||||
with Ada.Containers.Hashed_Maps;
|
with Ada.Containers.Hashed_Maps;
|
||||||
with League.JSON.Objects;
|
with League.JSON.Objects;
|
||||||
|
|
@ -8,9 +10,6 @@ with League.JSON.Arrays;
|
||||||
|
|
||||||
package Pandoc is
|
package Pandoc is
|
||||||
|
|
||||||
type String_Array is array (Natural range <>) of
|
|
||||||
League.Strings.Universal_String;
|
|
||||||
|
|
||||||
type Object_Type is
|
type Object_Type is
|
||||||
(Block_Plain,
|
(Block_Plain,
|
||||||
Block_Para,
|
Block_Para,
|
||||||
|
|
@ -47,51 +46,55 @@ package Pandoc is
|
||||||
Inline_Note,
|
Inline_Note,
|
||||||
Inline_Span);
|
Inline_Span);
|
||||||
|
|
||||||
type Content_Arr is array (Natural range <>)
|
type Key_Value_Pair is
|
||||||
of League.JSON.Values.JSON_Value;
|
record
|
||||||
|
Key : League.Strings.Universal_String;
|
||||||
|
Value : League.Strings.Universal_String;
|
||||||
|
end record;
|
||||||
|
|
||||||
|
type Key_Value_Pairs is array (Natural range <>) of Key_Value_Pair;
|
||||||
|
|
||||||
function Get_Type (B : League.JSON.Objects.JSON_Object) return Object_Type;
|
function Get_Type (B : League.JSON.Objects.JSON_Object) return Object_Type;
|
||||||
|
function Type_To_String (T : Object_Type)
|
||||||
|
return League.Strings.Universal_String;
|
||||||
|
|
||||||
function "+" (T : Wide_Wide_String) return League.Strings.Universal_String
|
Type_String : constant League.Strings.Universal_String :=
|
||||||
renames League.Strings.To_Universal_String;
|
League.Strings.To_Universal_String ("t");
|
||||||
|
|
||||||
|
Content_String : constant League.Strings.Universal_String :=
|
||||||
|
League.Strings.To_Universal_String ("c");
|
||||||
|
|
||||||
function Attr
|
function Attr
|
||||||
(Key : League.Strings.Universal_String;
|
(Id : League.Strings.Universal_String;
|
||||||
Value : League.Strings.Universal_String;
|
Key : League.Strings.Universal_String;
|
||||||
Id : League.Strings.Universal_String :=
|
Value : League.Strings.Universal_String)
|
||||||
League.Strings.Empty_Universal_String)
|
|
||||||
return League.JSON.Values.JSON_Value;
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
function Attr
|
function Attr
|
||||||
(Id : League.Strings.Universal_String;
|
(Id : League.Strings.Universal_String;
|
||||||
Key : String_Array;
|
Key_Value : Key_Value_Pairs)
|
||||||
Value : String_Array) return League.JSON.Values.JSON_Value;
|
|
||||||
|
|
||||||
function Div
|
|
||||||
(Attr : League.JSON.Values.JSON_Value;
|
|
||||||
Content : Content_Arr) return League.JSON.Values.JSON_Value;
|
|
||||||
|
|
||||||
function Div
|
|
||||||
(Attr : League.JSON.Values.JSON_Value;
|
|
||||||
Content : League.JSON.Values.JSON_Value)
|
|
||||||
return League.JSON.Values.JSON_Value;
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
function HTML_Block (Text : League.Strings.Universal_String)
|
function Attr
|
||||||
|
(Key : League.Strings.Universal_String;
|
||||||
|
Value : League.Strings.Universal_String)
|
||||||
return League.JSON.Values.JSON_Value;
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
function Plain (Inlines : League.JSON.Values.JSON_Value) return
|
function Attr (Key_Value : Key_Value_Pairs)
|
||||||
League.JSON.Values.JSON_Value;
|
return League.JSON.Values.JSON_Value;
|
||||||
|
|
||||||
function Definition_List (B : League.JSON.Objects.JSON_Object) return
|
Empty_Attribute : constant League.JSON.Values.JSON_Value := Attr([]);
|
||||||
League.JSON.Arrays.JSON_Array;
|
|
||||||
|
|
||||||
Type_String : constant League.Strings.Universal_String := +"t";
|
|
||||||
Content_String : constant League.Strings.Universal_String := +"c";
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
type String_Array is array (Natural range <>) of
|
||||||
|
League.Strings.Universal_String;
|
||||||
|
|
||||||
|
function "+" (T : Wide_Wide_String) return League.Strings.Universal_String
|
||||||
|
renames League.Strings.To_Universal_String;
|
||||||
|
|
||||||
Obj_String_Representation :
|
Obj_String_Representation :
|
||||||
constant array (Object_Type) of League.Strings.Universal_String := (
|
constant array (Object_Type) of League.Strings.Universal_String := [
|
||||||
+"Plain",
|
+"Plain",
|
||||||
+"Para",
|
+"Para",
|
||||||
+"LineBlock",
|
+"LineBlock",
|
||||||
|
|
@ -126,7 +129,7 @@ private
|
||||||
+"Image",
|
+"Image",
|
||||||
+"Note",
|
+"Note",
|
||||||
+"Span"
|
+"Span"
|
||||||
);
|
];
|
||||||
|
|
||||||
package Type_Map is new Ada.Containers.Hashed_Maps (
|
package Type_Map is new Ada.Containers.Hashed_Maps (
|
||||||
Key_Type => League.Strings.Universal_String,
|
Key_Type => League.Strings.Universal_String,
|
||||||
|
|
@ -134,6 +137,6 @@ private
|
||||||
Hash => League.Strings.Hash,
|
Hash => League.Strings.Hash,
|
||||||
Equivalent_Keys => League.Strings."=");
|
Equivalent_Keys => League.Strings."=");
|
||||||
|
|
||||||
Type_Mapping : Type_Map.Map := Type_Map.Empty;
|
Type_Mapping : constant Type_Map.Map := Type_Map.Empty_Map;
|
||||||
|
|
||||||
end Pandoc;
|
end Pandoc;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user