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