(www-display-object-desc): Add input box for
[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
294         (princ
295          "<form action=\"http://www.chise.org/hng-ids-find\">\n")
296         (princ
297          (www-format-encode-string
298           (est-format-object object)
299           ;; (if (eq genre 'character)
300           ;;     (format "%c" object)
301           ;;   (format "%s" (concord-object-id object)))
302           ))
303         (princ
304          (format
305           " <input type=\"text\" name=\"components\"
306 size=\"30\" maxlength=\"30\" value=\"%s\" />"
307           (encode-coding-string
308            (est-format-object object)
309            ;; (if (eq genre 'character)
310            ;;     (char-to-string object)
311            ;;   (format "%s" (concord-object-id object)))
312            'utf-8-jp-er)))
313         (princ
314          (www-format-encode-string
315           "を\u542Bむ HNG の\u6F22\u5B57を\u63A2す"))
316         (princ " <input type=\"submit\" value=\"search\" />\n")
317         (princ "</form>\n")
318         )
319       (princ
320        (if (= level 1)
321            "</p>\n"
322          "<li>\n"))
323       )))
324
325 (defun www-display-feature-desc (uri-feature-name genre uri-object
326                                                   &optional lang simple)
327   (let ((rdf-uri-object (if est-hide-cgi-mode
328                             (if (string-match "=" uri-object)
329                                 (concat
330                                  (est-uri-decode-feature-name-body
331                                   (substring uri-object 0 (match-beginning 0)))
332                                  ":"
333                                  (est-uri-decode-feature-name-body
334                                   (substring uri-object (match-end 0))))
335                               uri-object)))
336         (feature-name (www-uri-decode-feature-name uri-feature-name))
337         (name@lang (intern (format "name@%s" lang))))
338     (princ
339      (encode-coding-string
340       (format "<head>
341 <title>EsT feature: %s</title>
342 </head>\n"
343               feature-name)
344       'utf-8-mcs-er))
345     (princ "<body>\n")
346     (princ
347      (if simple
348          (format
349           (if est-hide-cgi-mode
350               "<div style=\"text-align:right;\">
351 <a href=\"../../../edit/view.cgi?feature=%s&%s=%s\">
352 <input type=\"submit\" value=\"Edit\" />
353 </a>
354 <input type=\"submit\" value=\"New Account\" />
355 </div>
356 <hr />\n"
357             "<div style=\"text-align:right;\">
358 <a href=\"edit/view.cgi?feature=%s&%s=%s\">
359 <input type=\"submit\" value=\"Edit\" />
360 </a>
361 <input type=\"submit\" value=\"New Account\" />
362 </div>
363 <hr />\n")
364           uri-feature-name genre rdf-uri-object)
365        (format
366         "<div style=\"text-align:right;\">
367 <a href=\"../view/feature/%s&%s/%s\">
368 <input type=\"submit\" value=\"Simple\" />
369 </a>
370 </div>
371 <hr />\n"
372         uri-feature-name genre uri-object)))
373     (princ
374      (format "<h1>%s</h1>\n"
375              (www-format-encode-string
376               (symbol-name feature-name))))
377     (princ (format "<p>name : %s "
378                    (or (www-format-feature-name feature-name) "")))
379     (unless simple
380       (princ
381        (format
382         " <a href=\"%s?feature=%s&property=name&format=string&%s=%s\">"
383         chise-wiki-edit-url
384         uri-feature-name
385         genre
386         uri-object))
387       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
388     (princ "</p>\n")
389     (when lang
390       (princ "<p>")
391       (princ
392        (www-format-encode-string
393         (format "%s : %s"
394                 name@lang
395                 (or (char-feature-property feature-name name@lang) ""))))
396       (unless simple
397         (princ
398          (format
399           " <a href=\"%s?feature=%s&property=%s&format=string&%s=%s\">"
400           chise-wiki-edit-url
401           uri-feature-name
402           name@lang
403           genre
404           uri-object))
405         (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
406       (princ "</p>\n"))
407     (www-html-display-paragraph
408      (format "type : %s"
409              (or (www-feature-type feature-name)
410                  ;; (char-feature-property feature-name 'type)
411                  'generic)))
412     (princ (format "<p>value-format : %s "
413                    (www-format-value
414                     nil 'value-format 
415                     (or (www-feature-value-format feature-name)
416                         'default)
417                     'default
418                     'without-tags)
419                    ))
420     (unless simple
421       (princ
422        (format
423         " <a href=\"%s?feature=%s&property=value-format&format=wiki-text&%s=%s\"
424 >"
425         chise-wiki-edit-url
426         uri-feature-name
427         genre
428         uri-object))
429       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
430     (princ "</p>\n")
431
432     (princ (format "<p>value-presentation-format : %s "
433                    (www-format-value
434                     nil 'value-presentation-format 
435                     (or (www-feature-value-format feature-name)
436                         'default)
437                     'default
438                     'without-tags)
439                    ))
440     (unless simple
441       (princ
442        (format
443         " <a href=\"%s?feature=%s&property=value-presentation-format&format=wiki-text&%s=%s\"
444 >"
445         chise-wiki-edit-url
446         uri-feature-name
447         genre
448         uri-object))
449       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
450     (princ "</p>\n")
451
452     (princ "<p>format : ")
453     (www-html-display-text
454      (decode-coding-string
455       (www-xml-format-list
456        (www-feature-format feature-name))
457       'utf-8-mcs-er))
458     (unless simple
459       (princ
460        (format
461         " <a href=\"%s?feature=%s&property=format&format=wiki-text&%s=%s\"
462 >"
463         chise-wiki-edit-url
464         uri-feature-name
465         genre
466         uri-object))
467       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
468     (princ "</p>\n")
469     
470     (www-html-display-paragraph
471      (format "description : %s"
472              (or (decode-coding-string
473                   (char-feature-property feature-name 'description)
474                   'utf-8-mcs-er)
475                  "")))
476     (when lang
477       (www-html-display-paragraph
478        (format "description@%s : %s"
479                lang
480                (or (char-feature-property
481                     feature-name
482                     (intern (format "description@%s" lang)))
483                    ""))))
484     ))
485   
486 (defun www-batch-view ()
487   (setq terminal-coding-system 'binary)
488   (condition-case err
489       (let* ((target (pop command-line-args-left))
490              (user (pop command-line-args-left))
491              (accept-language (pop command-line-args-left))
492              (mode (intern (pop command-line-args-left)))
493              (lang
494               (intern
495                (car (split-string
496                      (car (split-string
497                            (car (split-string accept-language ","))
498                            ";"))
499                      "-"))))
500              ret genre)
501         (princ "Content-Type: text/html; charset=UTF-8
502
503 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
504             \"http://www.w3.org/TR/html4/loose.dtd\">
505 <html lang=\"ja\">
506 ")
507         (cond
508          ((stringp target)
509           (when (string-match "^char=\\(&[^&;]+;\\)" target)
510             (setq ret (match-end 0))
511             (setq target
512                   (concat "char="
513                           (www-uri-encode-object
514                            (www-uri-decode-object
515                             'character (match-string 1 target)))
516                           (substring target ret))))
517           (setq target
518                 (mapcar (lambda (cell)
519                           (if (string-match "=" cell)
520                               (progn
521                                 (setq genre (substring cell 0 (match-beginning 0))
522                                       ret (substring cell (match-end 0)))
523                                 (cons
524                                  (intern
525                                   (decode-uri-string genre 'utf-8-mcs-er))
526                                  ret))
527                             (list (decode-uri-string cell 'utf-8-mcs-er))))
528                         (split-string target "&")))
529           (setq ret (car target))
530           (cond ((eq (car ret) 'char)
531                  (www-display-object-desc
532                   'character (cdr ret) (cdr (assq 'feature target))
533                   lang nil
534                   (eq mode 'simple))
535                  )
536                 ((eq (car ret) 'feature)
537                  (www-display-feature-desc
538                   (decode-uri-string (cdr ret) 'utf-8-mcs-er)
539                   (car (nth 1 target))
540                   (cdr (nth 1 target))
541                   lang
542                   (eq mode 'simple))
543                  )
544                 (t
545                  (www-display-object-desc
546                   (car ret) (cdr ret) (cdr (assq 'feature target))
547                   lang nil
548                   (eq mode 'simple))
549                  ))
550           ))
551         (princ "\n<hr>\n")
552         (princ (format "mode=%S\n" mode))
553         (princ (format "user=%s\n" user))
554         ;; (princ (format "local user=%s\n" (user-login-name)))
555         (princ (format "lang=%S\n" lang))
556         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
557         ;; (princ " CHISE ")
558         ;; (princ xemacs-chise-version)
559         (princ "
560 </body>
561 </html>")
562         )
563     (error nil
564            (princ (format "%S" err)))
565     ))
566
567 (defun www-batch-view-smart ()
568   (setq debug-on-error t)
569   (setq terminal-coding-system 'binary)
570   (condition-case err
571       (let* ((est-hide-cgi-mode t)
572              (target (pop command-line-args-left))
573              (user (pop command-line-args-left))
574              (accept-language (pop command-line-args-left))
575              (mode (intern (pop command-line-args-left)))
576              (lang
577               (intern
578                (car (split-string
579                      (car (split-string
580                            (car (split-string accept-language ","))
581                            ";"))
582                      "-"))))
583              ret genre feature)
584         (princ "Content-Type: text/html; charset=UTF-8
585
586 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
587             \"http://www.w3.org/TR/html4/loose.dtd\">
588 <html lang=\"ja\">
589 ")
590         (cond
591          ((stringp target)
592           (when (string-match "^char/\\(&[^&;]+;\\)" target)
593             (setq ret (match-end 0))
594             (setq target
595                   (concat "char/"
596                           (www-uri-encode-object
597                            (www-uri-decode-object
598                             'character (match-string 1 target)))
599                           (substring target ret))))
600           (setq target
601                 (mapcar
602                  (lambda (cell)
603                    (if (string-match "/" cell)
604                        (progn
605                          (setq genre (substring cell 0 (match-beginning 0))
606                                ret (substring cell (match-end 0)))
607                          (cons
608                           (intern (decode-uri-string genre 'utf-8-mcs-er))
609                           (if (string-match "/feature=" ret)
610                               (list (substring ret 0 (match-beginning 0))
611                                     (substring ret (match-end 0)))
612                             (list ret))))
613                      (list (decode-uri-string cell 'utf-8-mcs-er)))
614                    ;; (setq ret (split-string cell "/"))
615                    ;; (cons (intern
616                    ;;        (decode-uri-string (car ret) 'utf-8-mcs-er))
617                    ;;       (cdr ret))
618                    )
619                  (split-string target "&")))
620           (setq ret (car target))
621           ;; (princ (format "<p>%S, %S, %S</p>"
622           ;;                (car ret)(nth 1 ret)(nth 2 ret)))
623           (cond ((eq (car ret) 'char)
624                  (www-display-object-desc
625                   'character (nth 1 ret) (nth 2 ret)
626                   lang nil
627                   (eq mode 'simple))
628                  )
629                 ((eq (car ret) 'feature)
630                  (www-display-feature-desc
631                   (decode-uri-string (nth 1 ret) 'utf-8-mcs-er)
632                   (car (nth 1 target))
633                   (nth 1 (nth 1 target))
634                   lang
635                   (eq mode 'simple))
636                  )
637                 (t
638                  (www-display-object-desc
639                   (car ret) (nth 1 ret) (nth 2 ret)
640                   lang nil
641                   (eq mode 'simple))
642                  ))
643           ))
644         (princ "\n<hr>\n")
645         (princ (format "mode=%S\n" mode))
646         (princ (format "user=%s\n" user))
647         ;; (princ (format "local user=%s\n" (user-login-name)))
648         (princ (format "lang=%S\n" lang))
649         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
650         ;; (princ " CHISE ")
651         ;; (princ xemacs-chise-version)
652         (princ "
653 </body>
654 </html>")
655         )
656     (error nil
657            (princ (format "%S" err)))
658     ))
659
660 (provide 'cwiki-view)