*** empty log message ***
[m17n/m17n-docs.git] / utils / imdoc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <locale.h>
4 #include <libgen.h>
5 #include <m17n.h>
6
7 struct CommentList
8 {
9   char line[256];
10   struct CommentList *next;
11 };
12
13 void
14 invalid_file (char *filename)
15 {
16   fprintf (stderr, "Invalid input method file: %s\n", filename);
17   exit (1);
18 }
19
20 #define INHIBIT_TITLE
21
22 int
23 main (int argc, char **argv)
24 {
25   char *file;
26   MPlist *plist;
27   MSymbol language, name;
28   MText *mt;
29   FILE *fp;
30   char buf[256];
31   struct CommentList *head_comment = NULL, *cur_comment = NULL;
32   MSymbol Minput_method, Mutf8;
33
34   setlocale (LC_ALL, "");
35   M17N_INIT ();
36   Minput_method = msymbol ("input-method");
37   Mutf8 = msymbol ("utf-8");
38
39   if (argc < 2)
40     {
41       fprintf (stderr, "Usage: imdoc FILE\n");
42       exit (1);
43     }
44   fp = fopen (argv[1], "r");
45   if (! fp)
46     {
47       fprintf (stderr, "Usage: imdoc FILE\n");
48       exit (1);
49     }
50   while (fgets (buf, 256, fp)
51          && buf[0] != '(')
52     {
53       if (buf[0] == ';' && buf[1] == ';' && buf[2] == ';')
54         {
55           if (! head_comment)
56             {
57               cur_comment = head_comment = alloca (sizeof (struct CommentList));
58             }
59           else
60             {
61               cur_comment->next = alloca (sizeof (struct CommentList));
62               cur_comment = cur_comment->next;
63             }
64           strcpy (cur_comment->line, buf + 4);
65           cur_comment->next = NULL;
66         }
67     }
68   if (! buf[0])
69     invalid_file (argv[1]);
70   mt = mtext_from_data (buf, strlen (buf), MTEXT_FORMAT_US_ASCII);
71   plist = mplist_deserialize (mt);
72   m17n_object_unref (mt);
73   if (! plist)
74     invalid_file (argv[1]);
75   plist = mplist_value (plist);
76   if (mplist_value (plist) != Minput_method)
77     invalid_file (argv[1]);
78   plist = mplist_next (plist);
79   language = mplist_value (plist);
80   plist = mplist_next (plist);
81   name = mplist_value (plist);
82   plist = mplist_next (plist);
83   if (mplist_key (plist) != Mnil)
84     language = mplist_value (plist), name = Mnil;
85   m17n_object_unref (plist);
86
87   file = basename (strdup (argv[1]));
88
89   if (name != Mnil)
90     {
91       MPlist *plist;
92
93       printf ("*/\n/***ja\n<li> %s (language:", file);
94       if (language == Mt)
95         printf ("generic name:%s", msymbol_name (name));
96       else
97         printf ("%s name:%s", msymbol_name (language), msymbol_name (name));
98       printf (")*/\n/***en\n<li> %s (language:", file);
99       if (language == Mt)
100         printf ("generic name:%s", msymbol_name (name));
101       else
102         printf ("%s name:%s", msymbol_name (language), msymbol_name (name));
103       plist = minput_get_title_icon (language, name);
104       if (plist)
105         {
106           MText *title = mplist_value (plist);
107           
108 #ifndef INHIBIT_TITLE
109           printf ("@htmlonly\n title:\"");
110 #else
111           printf ("@htmlonly\n");
112 #endif
113           plist = mplist_next (plist);
114 #ifndef INHIBIT_TITLE
115           mconv_encode_stream (Mutf8, title, stdout);
116           printf ("\"");
117 #endif
118           if (mplist_key (plist) != Mnil)
119             {
120               MText *icon = mplist_value (plist);
121               char *iconfile = mtext_data (icon, MTEXT_FORMAT_US_ASCII,
122                                            NULL, NULL, NULL);
123               char *base;
124               char cmd[1024];
125               int result;
126
127               base = basename (strdup (iconfile));
128               sprintf (cmd, "convert -resize 50%% %s images/icon-%s",
129                        iconfile, base);
130               result = system (cmd);
131               if (result != 0)
132                 {
133                   fprintf (stderr, "Command fail: %s", cmd);
134                   exit (1);
135                 }
136               printf (" icon:<img src=\"icon-%s\" border=\"1\" style=\"vertical-align:middle;\">\n", base);
137             }
138           else
139             printf (" icon:none\n");
140           printf ("@endhtmlonly\n");
141         }
142       printf (")*/\n/***\n");
143     }
144   else
145     printf ("<li> %s (extra-name:%s, only for inclusion)\n", file,
146             msymbol_name (name));
147
148   mt = minput_get_description (language, name);
149   if (! mt)
150     {
151       printf ("Not yet officially released.\n");
152       exit (0);
153     }
154   else if (head_comment)
155     {
156       for (; head_comment; head_comment = head_comment->next)
157         {
158           char *p = head_comment->line;
159
160           if (p[0] == '|' && p[1] == '|')
161             printf ("<tr><td align=\"center\">"), p += 2;
162           while (*p)
163             {
164               if (p[0] == '|')
165                 {
166                   if (p[1] == '|')
167                     printf ("</td></tr>"), p += 2;
168                   else
169                     printf ("</td><td align=\"center\">"), p++;
170                 }
171               else
172                 putchar (*p), p++;
173             }
174         }
175     }
176   else
177     {
178       MConverter *converter = mconv_stream_converter (Mutf8, stdout);
179       int len = mtext_len (mt), i, c;
180       int has_non_ascii = 0;
181       
182       if (mtext_ref_char (mt, len - 1) != '\n')
183         {
184           mtext_ins_char (mt, len, '\n', 1);
185           len ++;
186         }
187
188       for (i = 0; i < len; i++)
189         {
190           c = mtext_ref_char (mt, i);
191           if (c >= 0x100)
192             {
193               has_non_ascii = 1;
194               break;
195             }
196         }
197       if (has_non_ascii)
198         printf ("@if FOR_HTML\n");
199       printf ("@verbatim\n");
200       mconv_encode_stream (Mutf8, mt, stdout);
201       printf ("@endverbatim\n");
202       if (has_non_ascii)
203         {
204           printf ("@endif\n@if FOR_LATEX\n");
205           printf ("@verbatim\n");
206           for (i = 0; i < len; i++)
207             {
208               c = mtext_ref_char (mt, i);
209               if (c >= 0x100)
210                 printf ("<U+%04X>", c);
211               else
212                 putchar (c);
213             }
214           printf ("@endverbatim\n");
215           printf ("@endif\n");
216         }
217       mconv_free_converter (converter);
218     }
219
220   M17N_FINI ();
221   exit (0);
222 }