(www-display-feature-desc): Decode return value of
[chise/est.git] / cwiki-view.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'cwiki-format)
3
4 (defvar chise-wiki-view-url "view.cgi")
5 (defvar chise-wiki-edit-url "edit.cgi")
6 (defvar chise-wiki-add-url "add.cgi")
7
8 (defun www-edit-display-feature-input-box (char feature-name
9                                                 &optional format value)
10   (if (symbolp char)
11       (setq char (or (concord-decode-object '=id char 'feature)
12                      (concord-make-object 'feature char))))
13   (unless format
14     (setq format 'default))
15   (unless value
16     (setq value (www-get-feature-value char feature-name)))
17   (if (and (symbolp value)
18            (eq format 'wiki-text))
19       (setq value (list (list value))))
20   (princ
21    (format "<p><input type=\"text\" name=\"feature-name\"
22 size=\"32\" maxlength=\"256\" value=\"%s\">"
23            feature-name))
24   (princ (encode-coding-string " \u2190 " 'utf-8-mcs-er))
25   (princ
26    (format "%s<input type=\"text\" name=\"%s\"
27 size=\"64\" maxlength=\"256\" value=\"%s\">
28 <input type=\"submit\" value=\"set\" /></p>
29 "
30            (if (or (eq format 'HEX)(eq format 'hex))
31                "0x"
32              "")
33            format
34            (mapconcat (lambda (c)
35                         (cond
36                          ;; ((eq c ?<) "&amp;lt;")
37                          ;; ((eq c ?>) "&amp;gt;")
38                          ((eq c ?\u0022) "&quot;")
39                          (t
40                           (char-to-string c))))
41                       (est-format-list value format nil nil " ")
42                       "")))
43   )
44
45 (defun www-display-object-desc (genre uri-object &optional uri-feature-name
46                                       lang level simple
47                                       uri-feature-name-to-edit editing-format)
48   (unless level
49     (setq level 0))
50   (let ((object (www-uri-decode-object genre uri-object))
51         (est-eval-list-feature-items-limit est-eval-list-feature-items-limit)
52         (est-view-url-prefix (if uri-feature-name
53                                  "../.."
54                                ".."))
55         (rdf-uri-object (if est-hide-cgi-mode
56                             (if (string-match "=" uri-object)
57                                 (concat
58                                  (est-uri-decode-feature-name-body
59                                   (substring uri-object 0 (match-beginning 0)))
60                                  ":"
61                                  (est-uri-decode-feature-name-body
62                                   (substring uri-object (match-end 0))))
63                               uri-object)))
64         feature-name-to-display feature-name-to-edit
65         base-name-to-edit metadata-name-to-edit
66         without-header
67         logical-feature chise-wiki-displayed-features
68         parents
69         GlyphWiki-id ret object-spec)
70     (if (eq level 0)
71         (setq level 1
72               without-header nil)
73       (setq without-header t))
74     (when object
75       (when uri-feature-name-to-edit
76         (setq feature-name-to-edit
77               (www-uri-decode-feature-name uri-feature-name-to-edit))
78         (setq ret (symbol-name feature-name-to-edit))
79         (if (string-match "\\*" ret)
80             (setq base-name-to-edit (intern
81                                      (substring ret 0 (match-beginning 0)))
82                   metadata-name-to-edit (intern
83                                          (substring ret (match-end 0))))
84           (setq base-name-to-edit feature-name-to-edit))
85         (when (stringp editing-format)
86           (setq editing-format (intern editing-format))))
87       (when (and (eq genre 'character)
88                  (= (length uri-object) 1))
89         (setq uri-object (www-uri-encode-object object)))
90       (when (= level 1)
91         (princ
92          (encode-coding-string
93           (format "<head>
94 <title>EsT %s = %s</title>
95 </head>\n"
96                   genre
97                   (decode-uri-string uri-object 'utf-8-mcs-er))
98           'utf-8-mcs-er))
99         (princ "<body>\n"))
100       (when (eq genre 'character)
101         (dolist (feature (char-feature-property '$object 'additional-features))
102           (mount-char-attribute-table
103            (char-feature-name-at-domain feature '$rev=latest))))
104       (princ
105        (if simple
106            (format
107             (if est-hide-cgi-mode
108                 "<div style=\"text-align:right;\">
109 <a href=\"../../edit/view.cgi?%s=%s\">
110 <input type=\"submit\" value=\"Edit\" />
111 </a>\n"
112               "<div style=\"text-align:right;\">
113 <a href=\"edit/view.cgi?%s=%s\">
114 <input type=\"submit\" value=\"Edit\" />
115 </a>\n")
116             genre rdf-uri-object)
117          (format
118           "<div style=\"text-align:right;\">
119 <a href=\"../view/%s/%s\">
120 <input type=\"submit\" value=\"Simple\" />
121 </a>\n"
122           genre
123           (if (string-match ":" uri-object)
124               (concat
125                (est-uri-encode-feature-name-body
126                 (substring uri-object 0 (match-beginning 0)))
127                "="
128                (est-uri-encode-feature-name-body
129                 (substring uri-object (match-end 0))))))))
130       (princ
131        (format "<input type=\"submit\" value=\"New Account\" />
132 <a href=\"http://www.chise.org/est/rdf.cgi?%s=%s\">
133 <input type=\"submit\" value=\"RDF\" />
134 </a>
135 </div>
136 <hr />\n"
137                genre rdf-uri-object))
138       (when (setq parents (www-get-feature-value object '<-denotational))
139         (princ (format "<p>%s %s</p>\n<hr>\n"
140                        (www-format-value-as-char-list parents)
141                        (www-format-feature-name '->denotational lang))))
142       (when (setq parents (www-get-feature-value object '<-subsumptive))
143         (princ (format "<p>%s %s</p>\n<hr>\n"
144                        (www-format-value-as-char-list parents)
145                        (www-format-feature-name '->subsumptive lang))))
146       (when (eq genre 'character)
147         (setq GlyphWiki-id (char-GlyphWiki-id object)))
148       (setq ret (www-format-encode-string
149                  (est-format-object object 'readable)))
150       (princ (format "<h%d>%s%s</h%d>\n"
151                      level
152                      (if uri-feature-name
153                          (format "<a href=\"%s\">%s</a>"
154                                  ;; (if est-hide-cgi-mode
155                                  ;;     "<a href=\"../%s\">%s</a>"
156                                  ;;   "<a href=\"%s\">%s</a>")
157                                  (www-uri-make-object-url object uri-object)
158                                  ret)
159                        ret)
160                      (if GlyphWiki-id
161                          (format
162                           " <a href=\"http://glyphwiki.org/wiki/%s\"><img alt=\"%s\" src=\"http://glyphwiki.org/glyph/%s.50px.png\" /></a>"
163                           GlyphWiki-id
164                           GlyphWiki-id GlyphWiki-id)
165                        "")
166                      level))
167       (if (> level 1)
168           (princ "<ul>"))
169       (when feature-name-to-edit
170         (princ "<form action=\"set.cgi\" method=\"GET\">\n")
171         (princ
172          (encode-coding-string
173           (format "<p>(%s : <input type=\"text\" name=\"%s\"
174 size=\"30\" maxlength=\"30\" value=\"%s\">)</p>
175 "
176                   genre genre
177                   (decode-uri-string uri-object 'utf-8-mcs-er))
178           'utf-8-mcs-er)))
179       (setq object-spec
180             (cond
181              (uri-feature-name
182               (setq feature-name-to-display
183                     (www-uri-decode-feature-name uri-feature-name))
184               (setq est-eval-list-feature-items-limit nil)
185               (list
186                (cons feature-name-to-display
187                      (if (eq genre 'character)
188                          (get-char-attribute object feature-name-to-display)
189                        (concord-object-get object feature-name-to-display)))))
190              (t
191               (if (eq genre 'character)
192                   (char-attribute-alist object)
193                 (concord-object-spec object)))))
194       (when feature-name-to-edit
195         (unless (assq base-name-to-edit object-spec)
196           (setq object-spec (cons (cons base-name-to-edit nil)
197                                   object-spec))))
198       (dolist (cell (sort object-spec
199                           (lambda (a b)
200                             (char-attribute-name<
201                              (char-feature-name-sans-versions (car a))
202                              (char-feature-name-sans-versions (car b))))))
203         (setq logical-feature (char-feature-name-sans-versions (car cell)))
204         (unless (memq logical-feature chise-wiki-displayed-features)
205           (push logical-feature chise-wiki-displayed-features)
206           (cond
207            ((and feature-name-to-edit
208                  (eq (car cell) feature-name-to-edit))
209             (www-edit-display-feature-input-box
210              object feature-name-to-edit editing-format)
211             )
212            (t
213             (princ
214              (if (= level 1)
215                  "<div class=\"feature\" style=\"line-height:150%\">\n"
216                "<li>\n"))
217             (princ
218              (www-format-eval-list
219               (www-feature-format logical-feature)
220               object
221               logical-feature ; (car cell)
222               lang uri-object
223               nil simple))
224             (unless simple
225               (princ
226                (format " <a href=\"%s?%s=%s&feature=%s&format=wiki-text\"
227 ><input type=\"submit\" value=\"note\" /></a>"
228                        chise-wiki-edit-url
229                        genre
230                        (www-format-encode-string uri-object)
231                        (www-format-encode-string
232                         (www-uri-encode-feature-name
233                          (intern (format "%s*note"
234                                          logical-feature ; (car cell)
235                                          )))))))
236             (when (and feature-name-to-edit
237                        (eq base-name-to-edit (car cell)) metadata-name-to-edit)
238               (princ "<ul>\n")
239               (princ "<li>")
240               (www-edit-display-feature-input-box
241                object feature-name-to-edit editing-format)
242               (princ "</li>")
243               (princ "</ul>"))
244             (princ
245              (if (= level 1)
246                  "</div>\n"
247                "<li>\n"))
248             ))
249           ))
250       (princ
251        (if (= level 1)
252            "<p>\n"
253          "<li>\n"))
254       (when feature-name-to-edit
255         (princ "</form>\n"))
256       (unless simple
257         (princ
258          (format "<a href=\"%s?%s=%s\"
259 ><input type=\"submit\" value=\"add feature\" /></a>
260 "
261                  chise-wiki-add-url
262                  genre
263                  (www-format-encode-string uri-object))))
264       (princ
265        (if (= level 1)
266            "<p>\n"
267          "<li>\n"))
268       (when (eq genre 'character)
269         (princ
270          "<form action=\"http://www.chise.org/ids-find\">\n")
271         (princ
272          (www-format-encode-string
273           (est-format-object object)
274           ;; (if (eq genre 'character)
275           ;;     (format "%c" object)
276           ;;   (format "%s" (concord-object-id object)))
277           ))
278         (princ
279          (format
280           " <input type=\"text\" name=\"components\"
281 size=\"30\" maxlength=\"30\" value=\"%s\" />"
282           (encode-coding-string
283            (est-format-object object)
284            ;; (if (eq genre 'character)
285            ;;     (char-to-string object)
286            ;;   (format "%s" (concord-object-id object)))
287            'utf-8-jp-er)))
288         (princ
289          (www-format-encode-string
290           "を\u542Bむ\u6F22\u5B57を\u63A2す"))
291         (princ " <input type=\"submit\" value=\"search\" />\n")
292         (princ "</form>\n"))
293       (princ
294        (if (= level 1)
295            "</p>\n"
296          "<li>\n"))
297       )))
298
299 (defun www-display-feature-desc (uri-feature-name genre uri-object
300                                                   &optional lang simple)
301   (let ((rdf-uri-object (if est-hide-cgi-mode
302                             (if (string-match "=" uri-object)
303                                 (concat
304                                  (est-uri-decode-feature-name-body
305                                   (substring uri-object 0 (match-beginning 0)))
306                                  ":"
307                                  (est-uri-decode-feature-name-body
308                                   (substring uri-object (match-end 0))))
309                               uri-object)))
310         (feature-name (www-uri-decode-feature-name uri-feature-name))
311         (name@lang (intern (format "name@%s" lang))))
312     (princ
313      (encode-coding-string
314       (format "<head>
315 <title>EsT feature: %s</title>
316 </head>\n"
317               feature-name)
318       'utf-8-mcs-er))
319     (princ "<body>\n")
320     (princ
321      (if simple
322          (format
323           (if est-hide-cgi-mode
324               "<div style=\"text-align:right;\">
325 <a href=\"../../../edit/view.cgi?feature=%s&%s=%s\">
326 <input type=\"submit\" value=\"Edit\" />
327 </a>
328 <input type=\"submit\" value=\"New Account\" />
329 </div>
330 <hr />\n"
331             "<div style=\"text-align:right;\">
332 <a href=\"edit/view.cgi?feature=%s&%s=%s\">
333 <input type=\"submit\" value=\"Edit\" />
334 </a>
335 <input type=\"submit\" value=\"New Account\" />
336 </div>
337 <hr />\n")
338           uri-feature-name genre rdf-uri-object)
339        (format
340         "<div style=\"text-align:right;\">
341 <a href=\"../view/feature/%s&%s/%s\">
342 <input type=\"submit\" value=\"Simple\" />
343 </a>
344 </div>
345 <hr />\n"
346         uri-feature-name genre uri-object)))
347     (princ
348      (format "<h1>%s</h1>\n"
349              (www-format-encode-string
350               (symbol-name feature-name))))
351     (princ (format "<p>name : %s "
352                    (or (www-format-feature-name feature-name) "")))
353     (unless simple
354       (princ
355        (format
356         " <a href=\"%s?feature=%s&property=name&format=string&%s=%s\">"
357         chise-wiki-edit-url
358         uri-feature-name
359         genre
360         uri-object))
361       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
362     (princ "</p>\n")
363     (when lang
364       (princ "<p>")
365       (princ
366        (www-format-encode-string
367         (format "%s : %s"
368                 name@lang
369                 (or (char-feature-property feature-name name@lang) ""))))
370       (unless simple
371         (princ
372          (format
373           " <a href=\"%s?feature=%s&property=%s&format=string&%s=%s\">"
374           chise-wiki-edit-url
375           uri-feature-name
376           name@lang
377           genre
378           uri-object))
379         (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
380       (princ "</p>\n"))
381     (www-html-display-paragraph
382      (format "type : %s"
383              (or (www-feature-type feature-name)
384                  ;; (char-feature-property feature-name 'type)
385                  'generic)))
386     (princ (format "<p>value-format : %s "
387                    (www-format-value
388                     nil 'value-format 
389                     (or (www-feature-value-format feature-name)
390                         'default)
391                     'default
392                     'without-tags)
393                    ))
394     (unless simple
395       (princ
396        (format
397         " <a href=\"%s?feature=%s&property=value-format&format=wiki-text&%s=%s\"
398 >"
399         chise-wiki-edit-url
400         uri-feature-name
401         genre
402         uri-object))
403       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
404     (princ "</p>\n")
405
406     (princ (format "<p>value-presentation-format : %s "
407                    (www-format-value
408                     nil 'value-presentation-format 
409                     (or (www-feature-value-format feature-name)
410                         'default)
411                     'default
412                     'without-tags)
413                    ))
414     (unless simple
415       (princ
416        (format
417         " <a href=\"%s?feature=%s&property=value-presentation-format&format=wiki-text&%s=%s\"
418 >"
419         chise-wiki-edit-url
420         uri-feature-name
421         genre
422         uri-object))
423       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
424     (princ "</p>\n")
425
426     (princ "<p>format : ")
427     (www-html-display-text
428      (decode-coding-string
429       (www-xml-format-list
430        (www-feature-format feature-name))
431       'utf-8-mcs-er))
432     (unless simple
433       (princ
434        (format
435         " <a href=\"%s?feature=%s&property=format&format=wiki-text&%s=%s\"
436 >"
437         chise-wiki-edit-url
438         uri-feature-name
439         genre
440         uri-object))
441       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
442     (princ "</p>\n")
443     
444     (www-html-display-paragraph
445      (format "description : %s"
446              (or (decode-coding-string
447                   (char-feature-property feature-name 'description)
448                   'utf-8-mcs-er)
449                  "")))
450     (when lang
451       (www-html-display-paragraph
452        (format "description@%s : %s"
453                lang
454                (or (char-feature-property
455                     feature-name
456                     (intern (format "description@%s" lang)))
457                    ""))))
458     ))
459   
460 (defun www-batch-view ()
461   (setq terminal-coding-system 'binary)
462   (condition-case err
463       (let* ((target (pop command-line-args-left))
464              (user (pop command-line-args-left))
465              (accept-language (pop command-line-args-left))
466              (mode (intern (pop command-line-args-left)))
467              (lang
468               (intern
469                (car (split-string
470                      (car (split-string
471                            (car (split-string accept-language ","))
472                            ";"))
473                      "-"))))
474              ret genre)
475         (princ "Content-Type: text/html; charset=UTF-8
476
477 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
478             \"http://www.w3.org/TR/html4/loose.dtd\">
479 <html lang=\"ja\">
480 ")
481         (cond
482          ((stringp target)
483           (when (string-match "^char=\\(&[^&;]+;\\)" target)
484             (setq ret (match-end 0))
485             (setq target
486                   (concat "char="
487                           (www-uri-encode-object
488                            (www-uri-decode-object
489                             'character (match-string 1 target)))
490                           (substring target ret))))
491           (setq target
492                 (mapcar (lambda (cell)
493                           (if (string-match "=" cell)
494                               (progn
495                                 (setq genre (substring cell 0 (match-beginning 0))
496                                       ret (substring cell (match-end 0)))
497                                 (cons
498                                  (intern
499                                   (decode-uri-string genre 'utf-8-mcs-er))
500                                  ret))
501                             (list (decode-uri-string cell 'utf-8-mcs-er))))
502                         (split-string target "&")))
503           (setq ret (car target))
504           (cond ((eq (car ret) 'char)
505                  (www-display-object-desc
506                   'character (cdr ret) (cdr (assq 'feature target))
507                   lang nil
508                   (eq mode 'simple))
509                  )
510                 ((eq (car ret) 'feature)
511                  (www-display-feature-desc
512                   (decode-uri-string (cdr ret) 'utf-8-mcs-er)
513                   (car (nth 1 target))
514                   (cdr (nth 1 target))
515                   lang
516                   (eq mode 'simple))
517                  )
518                 (t
519                  (www-display-object-desc
520                   (car ret) (cdr ret) (cdr (assq 'feature target))
521                   lang nil
522                   (eq mode 'simple))
523                  ))
524           ))
525         (princ "\n<hr>\n")
526         (princ (format "mode=%S\n" mode))
527         (princ (format "user=%s\n" user))
528         ;; (princ (format "local user=%s\n" (user-login-name)))
529         (princ (format "lang=%S\n" lang))
530         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
531         ;; (princ " CHISE ")
532         ;; (princ xemacs-chise-version)
533         (princ "
534 </body>
535 </html>")
536         )
537     (error nil
538            (princ (format "%S" err)))
539     ))
540
541 (defun www-batch-view-smart ()
542   (setq debug-on-error t)
543   (setq terminal-coding-system 'binary)
544   (condition-case err
545       (let* ((est-hide-cgi-mode t)
546              (target (pop command-line-args-left))
547              (user (pop command-line-args-left))
548              (accept-language (pop command-line-args-left))
549              (mode (intern (pop command-line-args-left)))
550              (lang
551               (intern
552                (car (split-string
553                      (car (split-string
554                            (car (split-string accept-language ","))
555                            ";"))
556                      "-"))))
557              ret genre feature)
558         (princ "Content-Type: text/html; charset=UTF-8
559
560 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
561             \"http://www.w3.org/TR/html4/loose.dtd\">
562 <html lang=\"ja\">
563 ")
564         (cond
565          ((stringp target)
566           (when (string-match "^char/\\(&[^&;]+;\\)" target)
567             (setq ret (match-end 0))
568             (setq target
569                   (concat "char/"
570                           (www-uri-encode-object
571                            (www-uri-decode-object
572                             'character (match-string 1 target)))
573                           (substring target ret))))
574           (setq target
575                 (mapcar
576                  (lambda (cell)
577                    (if (string-match "/" cell)
578                        (progn
579                          (setq genre (substring cell 0 (match-beginning 0))
580                                ret (substring cell (match-end 0)))
581                          (cons
582                           (intern (decode-uri-string genre 'utf-8-mcs-er))
583                           (if (string-match "/feature=" ret)
584                               (list (substring ret 0 (match-beginning 0))
585                                     (substring ret (match-end 0)))
586                             (list ret))))
587                      (list (decode-uri-string cell 'utf-8-mcs-er)))
588                    ;; (setq ret (split-string cell "/"))
589                    ;; (cons (intern
590                    ;;        (decode-uri-string (car ret) 'utf-8-mcs-er))
591                    ;;       (cdr ret))
592                    )
593                  (split-string target "&")))
594           (setq ret (car target))
595           ;; (princ (format "<p>%S, %S, %S</p>"
596           ;;                (car ret)(nth 1 ret)(nth 2 ret)))
597           (cond ((eq (car ret) 'char)
598                  (www-display-object-desc
599                   'character (nth 1 ret) (nth 2 ret)
600                   lang nil
601                   (eq mode 'simple))
602                  )
603                 ((eq (car ret) 'feature)
604                  (www-display-feature-desc
605                   (decode-uri-string (nth 1 ret) 'utf-8-mcs-er)
606                   (car (nth 1 target))
607                   (nth 1 (nth 1 target))
608                   lang
609                   (eq mode 'simple))
610                  )
611                 (t
612                  (www-display-object-desc
613                   (car ret) (nth 1 ret) (nth 2 ret)
614                   lang nil
615                   (eq mode 'simple))
616                  ))
617           ))
618         (princ "\n<hr>\n")
619         (princ (format "mode=%S\n" mode))
620         (princ (format "user=%s\n" user))
621         ;; (princ (format "local user=%s\n" (user-login-name)))
622         (princ (format "lang=%S\n" lang))
623         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
624         ;; (princ " CHISE ")
625         ;; (princ xemacs-chise-version)
626         (princ "
627 </body>
628 </html>")
629         )
630     (error nil
631            (princ (format "%S" err)))
632     ))
633
634 (provide 'cwiki-view)