*** empty log message ***
[m17n/m17n-docs.git] / utils / imdoc.c
index 3226417..346da2f 100644 (file)
@@ -1,8 +1,22 @@
 #include <stdio.h>
 #include <string.h>
 #include <locale.h>
+#include <libgen.h>
 #include <m17n.h>
 
+struct CommentList
+{
+  char line[256];
+  struct CommentList *next;
+};
+
+void
+invalid_file (char *filename)
+{
+  fprintf (stderr, "Invalid input method file: %s\n", filename);
+  exit (1);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -10,20 +24,55 @@ main (int argc, char **argv)
   MPlist *plist;
   MSymbol language, name;
   MText *mt;
+  FILE *fp;
+  char buf[256];
+  struct CommentList *head_comment = NULL, *cur_comment = NULL;
+  MSymbol Minput_method, Mutf8;
 
   setlocale (LC_ALL, "");
   M17N_INIT ();
+  Minput_method = msymbol ("input-method");
+  Mutf8 = msymbol ("utf-8");
 
-  if (argc < 3)
+  if (argc < 2)
+    {
+      fprintf (stderr, "Usage: imdoc FILE\n");
+      exit (1);
+    }
+  fp = fopen (argv[1], "r");
+  if (! fp)
     {
-      fprintf (stderr, "Usage: imdoc FILE (input-method LANG NAME [EXTRA])\n");
+      fprintf (stderr, "Usage: imdoc FILE\n");
       exit (1);
     }
-  file = argv[1];
-  mt = mtext_from_data (argv[2], strlen (argv[2]), MTEXT_FORMAT_US_ASCII);
+  while (fgets (buf, 256, fp)
+        && buf[0] != '(')
+    {
+      if (buf[0] == ';' && buf[1] == ';' && buf[2] == ';')
+       {
+         if (! head_comment)
+           {
+             cur_comment = head_comment = alloca (sizeof (struct CommentList));
+           }
+         else
+           {
+             cur_comment->next = alloca (sizeof (struct CommentList));
+             cur_comment = cur_comment->next;
+           }
+         strcpy (cur_comment->line, buf + 4);
+         cur_comment->next = NULL;
+       }
+    }
+  if (! buf[0])
+    invalid_file (argv[1]);
+  mt = mtext_from_data (buf, strlen (buf), MTEXT_FORMAT_US_ASCII);
   plist = mplist_deserialize (mt);
   m17n_object_unref (mt);
+  if (! plist)
+    invalid_file (argv[1]);
   plist = mplist_value (plist);
+  if (mplist_value (plist) != Minput_method)
+    invalid_file (argv[1]);
   plist = mplist_next (plist);
   language = mplist_value (plist);
   plist = mplist_next (plist);
@@ -32,26 +81,92 @@ main (int argc, char **argv)
   if (mplist_key (plist) != Mnil)
     language = mplist_value (plist), name = Mnil;
   m17n_object_unref (plist);
+
+  file = basename (strdup (argv[1]));
+
+  if (name != Mnil)
+    {
+      MPlist *plist;
+
+      printf ("*/\n/***ja\n<li> %s (language:", file);
+      if (language == Mt)
+       printf ("generic name:%s", msymbol_name (name));
+      else
+       printf ("%s name:%s", msymbol_name (language), msymbol_name (name));
+      printf (")*/\n/***en\n<li> %s (language:", file);
+      if (language == Mt)
+       printf ("generic name:%s", msymbol_name (name));
+      else
+       printf ("%s name:%s", msymbol_name (language), msymbol_name (name));
+      plist = minput_get_title_icon (language, name);
+      if (plist)
+       {
+         MText *title = mplist_value (plist);
+         
+         printf ("@htmlonly\n title:\"");
+         plist = mplist_next (plist);
+         mconv_encode_stream (Mutf8, title, stdout);
+         printf ("\"");
+         if (mplist_key (plist) != Mnil)
+           {
+             MText *icon = mplist_value (plist);
+             char *iconfile = mtext_data (icon, MTEXT_FORMAT_US_ASCII,
+                                          NULL, NULL, NULL);
+             char *base;
+             char cmd[1024];
+
+             base = basename (strdup (iconfile));
+             sprintf (cmd, "convert -resize 50%% %s images/icon-%s",
+                      iconfile, base);
+             system (cmd);
+             printf (" icon:<img src=\"icon-%s\" border=\"1\" style=\"vertical-align:middle;\">\n", base);
+           }
+         else
+           printf (" icon:none\n");
+         printf ("@endhtmlonly\n");
+       }
+      printf (")*/\n/***\n");
+    }
+  else
+    printf ("<li> %s (extra-name:%s, only for inclusion)\n", file,
+           msymbol_name (name));
+
   mt = minput_get_description (language, name);
   if (! mt)
     {
-      fprintf (stderr, "Invalid IM name: %s %s\n", argv[1] ,argv[2]);
-      exit (1);
+      printf ("Not yet officially released.\n");
+      exit (0);
     }
-#if 0
-  if (argc < 5)
+  else if (head_comment)
     {
-      if (language == Mt)
-       printf ("<li> %s (language:generic name:%s)\n", file, argv[3]);
-      else
-       printf ("<li> %s (language:%s name:%s)\n", file, argv[2], argv[3]);
+      for (; head_comment; head_comment = head_comment->next)
+       {
+         char *p = head_comment->line;
+
+         if (p[0] == '|' && p[1] == '|')
+           printf ("<tr><td align=\"center\">"), p += 2;
+         while (*p)
+           {
+             if (p[0] == '|')
+               {
+                 if (p[1] == '|')
+                   printf ("</td></tr>"), p += 2;
+                 else
+                   printf ("</td><td align=\"center\">"), p++;
+               }
+             else
+               putchar (*p), p++;
+           }
+       }
     }
   else
-    printf ("<li> %s (extra-name:%s, only for inclusion)\n", file, argv[4]);
-#endif
-  printf ("@verbatim\n");
-  mconv_encode_stream (msymbol ("utf-8"), mt, stdout);
-  printf ("\n@endverbatim\n");
+    {
+      printf ("@verbatim\n");
+      mconv_encode_stream (msymbol ("utf-8"), mt, stdout);
+      if (mtext_ref_char (mt, mtext_len (mt) - 1) != '\n')
+       printf ("\n");
+      printf ("@endverbatim\n");
+    }
 
   M17N_FINI ();
   exit (0);