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