parse_text.c
changeset 24 026a2ba0ce16
parent 18 64ed4238657f
child 25 8d524bb8dcea
equal deleted inserted replaced
23:97feccfc5215 24:026a2ba0ce16
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <stdlib.h>
     2 #include <stdlib.h>
     3 #include "dictre.h"
     3 #include "dictre.h"
     4 
     4 
     5 static int is_http = 0;
     5 static int is_http = 0;
       
     6 static int give_html = 1;
     6 static int content_length = -1;
     7 static int content_length = -1;
     7 static struct Dict dakcentiga;
     8 static struct Dict dakcentiga;
       
     9 static struct Dict dsignifoj;
       
    10 
       
    11 static void print_html_header()
       
    12 {
       
    13     printf(
       
    14 "<html>\n"
       
    15 "<head>\n"
       
    16 "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n"
       
    17 "    <title>Akcentigita teksto</title>\n"
       
    18 "</head>\n"
       
    19 "<style type=\"text/css\" media=\"screen\">\n"
       
    20 "div.difino { display: none ;\n"
       
    21 "    position: absolute;\n"
       
    22 "    left: 0;\n"
       
    23 "    background: moccasin;\n"
       
    24 "    border: thin;\n"
       
    25 "    border-color: black;\n"
       
    26 "    padding: 10px 10px 10px 10px;\n"
       
    27 "    text-indent: 0em;\n"
       
    28 "    text-align: left;\n"
       
    29 "    }\n"
       
    30 "div.alineo { text-indent: 2em ; text-align: justify }\n"
       
    31 "</style>\n"
       
    32 "<body>\n"
       
    33 "<script language=\"javascript\">\n"
       
    34 "var blocked = \"\";\n"
       
    35 "\n"
       
    36 "function display(element, ev)\n"
       
    37 "{\n"
       
    38 "    var x,y;\n"
       
    39 "    x = ev.screenX + window.pageXOffset;\n"
       
    40 "    y = ev.screenY + window.pageYOffset;\n"
       
    41 "\n"
       
    42 "    if (blocked != \"\")\n"
       
    43 "        return;\n"
       
    44 #if 0
       
    45 "\n"
       
    46 "    if (x + 320 > window.innerWidth)\n"
       
    47 "        x = window.innerWidth - 320;\n"
       
    48 "\n"
       
    49 "    /*\n"
       
    50 "    if (y < window.innerHeight / 2)\n"
       
    51 "    {\n"
       
    52 "        y += 5;\n"
       
    53 "        document.getElementById(element).style.top = y;\n"
       
    54 "    } else {\n"
       
    55 "        y -= 5;\n"
       
    56 "        document.getElementById(element).style.top = 0;\n"
       
    57 "        document.getElementById(element).style.bottom = y;\n"
       
    58 "    }\n"
       
    59 "    */\n"
       
    60 #endif
       
    61 "    y += 5;\n"
       
    62 "    document.getElementById(element).style.top = y + 'px';\n"
       
    63 "    \n"
       
    64 /*
       
    65 "    document.getElementById(element).style.left = 0;\n"
       
    66 */
       
    67 "    document.getElementById(element).style.display = 'block';\n"
       
    68 "}\n"
       
    69 "\n"
       
    70 "function undisplay(element)\n"
       
    71 "{\n"
       
    72 "    document.getElementById(element).style.display = 'none';\n"
       
    73 "}\n"
       
    74 "\n"
       
    75 "function click(element, ev)\n"
       
    76 "{\n"
       
    77 "    if (blocked == \"\")\n"
       
    78 "    {\n"
       
    79 "        display(element, ev);\n"
       
    80 "        blocked = element;\n"
       
    81 "    }\n"
       
    82 "    else\n"
       
    83 "    {\n"
       
    84 "        undisplay(blocked);\n"
       
    85 "        blocked = \"\";\n"
       
    86 "    }\n"
       
    87 "}\n"
       
    88 "\n"
       
    89 "</script>"
       
    90 );
       
    91 
       
    92 }
       
    93 
       
    94 static void print_html_footer()
       
    95 {
       
    96     printf("</body></html>\n");
       
    97 }
       
    98 
       
    99 static void dump_signifo_with_p(const char *word, const char *signifo)
       
   100 {
       
   101     int i;
       
   102     char last;
       
   103 
       
   104     printf("<p>## Se moto %s:</p>\n<p>", word);
       
   105 
       
   106     last = 0;
       
   107     i=0;
       
   108     do
       
   109     {
       
   110         char nun;
       
   111         nun = *signifo;
       
   112         if (nun == '\0')
       
   113             break;
       
   114         if (nun == '\n' && last == '\n')
       
   115             printf("</p><p>");
       
   116         putchar(nun);
       
   117         last = nun;
       
   118         ++signifo;
       
   119     } while(1);
       
   120     printf("</p>");
       
   121 }
       
   122 
       
   123 static void print_start_signifoj(int id)
       
   124 {
       
   125     printf("<div id=\"vorto%i\" onclick=\"click('vorto%i', event);\" "
       
   126             "class=\"difino\"><p>\n", id, id);
       
   127 }
       
   128 
       
   129 static void print_end_signifoj()
       
   130 {
       
   131     printf("</p></div>");
       
   132 }
       
   133 
       
   134 /* This expects the word to start exactly at *wordlist */
       
   135 static void print_signifoj(char *wordlist, int id)
       
   136 {
       
   137     char * next_space;
       
   138     char def[MAXDEF];
       
   139     int start_shown = 0;
       
   140 
       
   141     do {
       
   142         next_space = strchr(wordlist, ' ');
       
   143         if (next_space == 0)
       
   144             next_space = strchr(wordlist, '\n');
       
   145         if (next_space == 0)
       
   146             next_space = strchr(wordlist, '\0');
       
   147         if (next_space != 0 && next_space != wordlist)
       
   148         {
       
   149             int spacepos;
       
   150             spacepos = next_space - wordlist;
       
   151             wordlist[spacepos] = 0;
       
   152 
       
   153             /* wordlist has the word to search */
       
   154             find_def(&dsignifoj, wordlist, def);
       
   155             if (def[0])
       
   156             {
       
   157                 if (!start_shown)
       
   158                 {
       
   159                     print_start_signifoj(id);
       
   160                     start_shown = 1;
       
   161                 }
       
   162                 dump_signifo_with_p(wordlist, def);
       
   163             }
       
   164         } else
       
   165             break;
       
   166         wordlist = next_space + 1;
       
   167     } while(1);
       
   168 
       
   169     if (start_shown)
       
   170         print_end_signifoj();
       
   171 }
       
   172 
       
   173 static print_accented(const char *word, int id)
       
   174 {
       
   175     if (give_html)
       
   176     {
       
   177         printf("<span class=\"vorto\" onclick=\"click('vorto%i', event);\">"
       
   178                 "%s</span>",id,word);
       
   179     } else
       
   180         printf("%s", word);
       
   181 }
     8 
   182 
     9 static void give_accent_to_word(const char *word)
   183 static void give_accent_to_word(const char *word)
    10 {
   184 {
    11     char def[MAXDEF];
   185     char def[MAXDEF];
    12     char low[MAXWORD];
   186     char low[MAXWORD];
    13     char recased[MAXWORD];
   187     char recased[MAXWORD];
    14     enum Case vcase[MAXWORD];
   188     enum Case vcase[MAXWORD];
       
   189     static int id = 1;
    15 
   190 
    16     /* Get case */
   191     /* Get case */
    17     get_case(vcase, word);
   192     get_case(vcase, word);
    18 
   193 
    19     /* Get lowercase version */
   194     /* Get lowercase version */
    32         if (first_space != 0) /* Space found */
   207         if (first_space != 0) /* Space found */
    33         {
   208         {
    34             spacepos = first_space - def;
   209             spacepos = first_space - def;
    35             def[spacepos] = 0; /* Mark an end of string */
   210             def[spacepos] = 0; /* Mark an end of string */
    36             reapply_case(recased, def, vcase);
   211             reapply_case(recased, def, vcase);
    37             printf("%s", recased);
   212             print_accented(recased, id);
       
   213             if (give_html)
       
   214                 print_signifoj(first_space + 1 /*' '*/, id);
       
   215             ++id;
    38         }
   216         }
    39         return;
   217         return;
    40     }
   218     }
    41 
   219     else if (give_html)
    42     /* if first_space == 0 or word not found */
   220     {
       
   221         char def[MAXDEF];
       
   222         /* OPTIMIZE: This, if find, will react in a second search at
       
   223          * print_signifoj() */
       
   224         find_def(&dsignifoj, low, def);
       
   225         if (def[0])
       
   226         {
       
   227             print_accented(word, id);
       
   228             print_signifoj(low, id);
       
   229             ++id;
       
   230             return;
       
   231         }
       
   232     }
       
   233 
       
   234     /* else ... */
       
   235 
       
   236     /* if first_space == 0 or word not found _AND_ word not found in meanings
       
   237      * when give_html...*/
    43     printf("%s", word);
   238     printf("%s", word);
    44 }
   239 }
    45 
   240 
    46 static int my_fgetc(FILE *f)
   241 static int my_fgetc(FILE *f)
    47 {
   242 {
    54 static void process_text(FILE *in, int pos, int length)
   249 static void process_text(FILE *in, int pos, int length)
    55 {
   250 {
    56     unsigned char tmp[MAXWORD];
   251     unsigned char tmp[MAXWORD];
    57     int wordpos = 0;
   252     int wordpos = 0;
    58     int skip_non_ascii = 0;
   253     int skip_non_ascii = 0;
    59 
   254     int last = 0;
       
   255 
       
   256     if (give_html)
       
   257         printf("<div class=\"alineo\">");
    60     do
   258     do
    61     {
   259     {
    62         int c;
   260         int c;
    63         /* Check pos only if length >= 0 */
   261         /* Check pos only if length >= 0 */
    64         if (length >= 0 && pos >= length)
   262         if (length >= 0 && pos >= length)
    65             break;
   263             break;
    66         c = my_fgetc(in);
   264         c = my_fgetc(in);
    67         if (c == EOF || c == END_OF_URL)
   265         if (c == EOF || c == END_OF_URL)
    68             break;
   266             break;
       
   267         if (c == '\r')
       
   268             continue;
       
   269 
       
   270         /* Process 'last' for eventual line break */
       
   271         if (give_html && c == '\n' && last == '\n')
       
   272             printf("</div>\n<div class=\"alineo\">");
       
   273         last = c;
       
   274 
    69         if (skip_non_ascii || is_ASCII(c))
   275         if (skip_non_ascii || is_ASCII(c))
    70         {
   276         {
    71             if (wordpos != 0)
   277             if (wordpos != 0)
    72             {
   278             {
    73                 tmp[wordpos] = 0;
   279                 tmp[wordpos] = 0;
   103     {
   309     {
   104         tmp[wordpos] = 0;
   310         tmp[wordpos] = 0;
   105         give_accent_to_word(tmp);
   311         give_accent_to_word(tmp);
   106         wordpos = 0;
   312         wordpos = 0;
   107     }
   313     }
       
   314     if (give_html)
       
   315         printf("</div>");
   108 }
   316 }
   109 
   317 
   110 static print_http_header()
   318 static print_http_header()
   111 {
   319 {
   112     printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
   320     printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
   126 int main()
   334 int main()
   127 {
   335 {
   128     char *c;
   336     char *c;
   129 
   337 
   130     init_dictionary(&dakcentiga, "akcentiga");
   338     init_dictionary(&dakcentiga, "akcentiga");
       
   339     init_dictionary(&dsignifoj, "signifoj");
   131 
   340 
   132     if (c = getenv("CONTENT_LENGTH"))
   341     if (c = getenv("CONTENT_LENGTH"))
   133     {
   342     {
   134         content_length = atoi(c);
   343         content_length = atoi(c);
   135         is_http = 1;
   344         is_http = 1;
   138     {
   347     {
   139         print_http_header();
   348         print_http_header();
   140         if (!eat_form_ok())
   349         if (!eat_form_ok())
   141             return -1;
   350             return -1;
   142     }
   351     }
       
   352 
       
   353     if (give_html)
       
   354         print_html_header();
       
   355     /* We pass -1 so we don't check content length */
   143     process_text(stdin, 0, -1);
   356     process_text(stdin, 0, -1);
       
   357 
       
   358     if (give_html)
       
   359         print_html_footer();
   144     end_dictionary(&dakcentiga);
   360     end_dictionary(&dakcentiga);
   145 
   361 
   146     return 0;
   362     return 0;
   147 }
   363 }