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