*** 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 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       printf ("@verbatim\n");
165       mconv_encode_stream (msymbol ("utf-8"), mt, stdout);
166       if (mtext_ref_char (mt, mtext_len (mt) - 1) != '\n')
167         printf ("\n");
168       printf ("@endverbatim\n");
169     }
170
171   M17N_FINI ();
172   exit (0);
173 }