awa_2.4.0_59135a52/ada-wiki/samples/convert.adb

  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
-----------------------------------------------------------------------
--  convert -- Convert a wiki format into another
--  Copyright (C) 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Wide_Wide_Text_IO;
with Ada.IO_Exceptions;
with Ada.Strings.Unbounded;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;

with GNAT.Command_Line;

with Util.Files;
with Util.Strings.Transforms;

with Wiki.Strings;
with Wiki.Filters.Html;
with Wiki.Filters.TOC;
with Wiki.Streams.Builders;
with Wiki.Render.Wiki;
with Wiki.Documents;
with Wiki.Parsers;

procedure Convert is

   use GNAT.Command_Line;
   use Ada.Strings.Unbounded;
   use Ada.Strings.UTF_Encoding;

   procedure Usage;
   procedure Parse (Content : in String);
   procedure Print (Item : in Wiki.Strings.WString);
   function To_Syntax (Name : in String) return Wiki.Wiki_Syntax;

   Html_Filter : aliased Wiki.Filters.Html.Html_Filter_Type;
   TOC         : aliased Wiki.Filters.TOC.TOC_Filter;
   Count       : Natural := 0;
   Src_Syntax  : Wiki.Wiki_Syntax := Wiki.SYNTAX_MARKDOWN;
   Dst_Syntax  : Wiki.Wiki_Syntax := Wiki.SYNTAX_MARKDOWN;

   procedure Usage is
   begin
      Ada.Text_IO.Put_Line ("Convert a wiki file from one format to another");
      Ada.Text_IO.Put_Line ("Usage: convert [-s format] [-d format] file...");
      Ada.Text_IO.Put_Line ("  -s format  Define the source file format");
      Ada.Text_IO.Put_Line ("  -d format  Define the destination file format");
   end Usage;

   procedure Print (Item : in Wiki.Strings.WString) is
   begin
      Ada.Wide_Wide_Text_IO.Put (Item);
   end Print;

   procedure Parse (Content : in String) is
      Doc      : Wiki.Documents.Document;
      Engine   : Wiki.Parsers.Parser;
      Stream   : aliased Wiki.Streams.Builders.Output_Builder_Stream;
      Renderer : aliased Wiki.Render.Wiki.Wiki_Renderer;
   begin
      Engine.Add_Filter (TOC'Unchecked_Access);
      Engine.Add_Filter (Html_Filter'Unchecked_Access);
      Engine.Set_Syntax (Src_Syntax);
      Engine.Parse (Wide_Wide_Strings.Decode (Content), Doc);

      Renderer.Set_Output_Stream (Stream'Unchecked_Access, Dst_Syntax);
      Renderer.Render (Doc);
      Stream.Iterate (Print'Access);
      Ada.Wide_Wide_Text_IO.New_Line;
   end Parse;

   function To_Syntax (Name : in String) return Wiki.Wiki_Syntax is
   begin
      if Name = "markdown" then
         return Wiki.SYNTAX_MARKDOWN;
      end if;

      if Name = "dotclear" then
         return Wiki.SYNTAX_DOTCLEAR;
      end if;

      if Name = "creole" then
         return Wiki.SYNTAX_CREOLE;
      end if;

      if Name = "textile" then
         return Wiki.SYNTAX_TEXTILE;
      end if;

      if Name = "mediawiki" then
         return Wiki.SYNTAX_MEDIA_WIKI;
      end if;

      if Name = "html" then
         return Wiki.SYNTAX_HTML;
      end if;

      return Wiki.SYNTAX_MARKDOWN;
   end To_Syntax;

begin
   loop
      case Getopt ("s: d:") is

         when 's' =>
            declare
               Value : constant String := Util.Strings.Transforms.To_Lower_Case (Parameter);
            begin
               Src_Syntax := To_Syntax (Value);

            exception
               when Constraint_Error =>
                  Ada.Text_IO.Put_Line ("Invalid source format " & Value);
            end;

         when 'd' =>
            declare
               Value : constant String := Util.Strings.Transforms.To_Lower_Case (Parameter);
            begin
               Dst_Syntax := To_Syntax (Value);

            exception
               when Constraint_Error =>
                  Ada.Text_IO.Put_Line ("Invalid source format " & Value);
            end;

         when others =>
            exit;
      end case;
   end loop;

   loop
      declare
         Name : constant String := GNAT.Command_Line.Get_Argument;
         Data : Unbounded_String;
      begin
         if Name = "" then
            if Count = 0 then
               Usage;
            end if;
            return;
         end if;
         Count := Count + 1;
         Util.Files.Read_File (Name, Data);
         Parse (To_String (Data));

      exception
         when Ada.IO_Exceptions.Name_Error =>
            Ada.Text_IO.Put_Line ("Cannot read file '" & Name & "'");

      end;
   end loop;

exception
   when Invalid_Switch =>
      Ada.Text_IO.Put_Line ("Invalid option.");
      Usage;

end Convert;