1605d6811cce7c374cc1dca5d7cc7b59bf4b776a
[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   (princ
18    (format "<p><input type=\"text\" name=\"feature-name\"
19 size=\"32\" maxlength=\"256\" value=\"%s\">"
20            feature-name))
21   (princ (encode-coding-string " \u2190 " 'utf-8-mcs-er))
22   (princ
23    (format "%s<input type=\"text\" name=\"%s\"
24 size=\"64\" maxlength=\"256\" value=\"%s\">
25 <input type=\"submit\" value=\"set\" /></p>
26 "
27            (if (or (eq format 'HEX)(eq format 'hex))
28                "0x"
29              "")
30            format
31            (mapconcat (lambda (c)
32                         (cond
33                          ;; ((eq c ?<) "&amp;lt;")
34                          ;; ((eq c ?>) "&amp;gt;")
35                          ((eq c ?\u0022) "&quot;")
36                          (t
37                           (char-to-string c))))
38                       (www-format-value char feature-name
39                                         value format 'without-tags)
40                       "")))
41   )
42
43 (defun www-display-object-desc (genre uri-object &optional lang level simple
44                                       uri-feature-name-to-edit editing-format)
45   (unless level
46     (setq level 0))
47   (let ((object (www-uri-decode-object genre uri-object))
48         feature-name-to-edit
49         base-name-to-edit metadata-name-to-edit
50         without-header
51         logical-feature chise-wiki-displayed-features
52         parents
53         GlyphWiki-id ret object-spec)
54     (if (eq level 0)
55         (setq level 1
56               without-header nil)
57       (setq without-header t))
58     (when object
59       (when uri-feature-name-to-edit
60         (setq feature-name-to-edit
61               (www-uri-decode-feature-name uri-feature-name-to-edit))
62         (setq ret (symbol-name feature-name-to-edit))
63         (if (string-match "\\*" ret)
64             (setq base-name-to-edit (intern
65                                      (substring ret 0 (match-beginning 0)))
66                   metadata-name-to-edit (intern
67                                          (substring ret (match-end 0))))
68           (setq base-name-to-edit feature-name-to-edit))
69         (when (stringp editing-format)
70           (setq editing-format (intern editing-format))))
71       (when (and (eq genre 'character)
72                  (= (length uri-object) 1))
73         (setq uri-object (www-uri-encode-object object)))
74       (when (= level 1)
75         (princ
76          (encode-coding-string
77           (format "<head>
78 <title>EsT %s = %s</title>
79 </head>\n"
80                   genre
81                   (decode-uri-string uri-object 'utf-8-mcs-er))
82           'utf-8-mcs-er))
83         (princ "<body>\n"))
84       (when (eq genre 'character)
85         (dolist (feature (char-feature-property '$object 'additional-features))
86           (mount-char-attribute-table
87            (char-feature-name-at-domain feature '$rev=latest))))
88       (princ
89        (format
90         (if simple
91             "<div style=\"text-align:right;\">
92 <a href=\"edit/view.cgi?%s=%s\">
93 <input type=\"submit\" value=\"Edit\" />
94 </a>
95 <input type=\"submit\" value=\"New Account\" />
96 </div>
97 <hr />\n"
98           "<div style=\"text-align:right;\">
99 <a href=\"../view.cgi?%s=%s\">
100 <input type=\"submit\" value=\"Simple\" />
101 </a>
102 </div>
103 <hr />\n")
104         genre
105         uri-object))
106       (when (setq parents (www-get-feature-value object '<-denotational))
107         (princ (format "<p>%s %s</p>\n<hr>\n"
108                        (www-format-value-as-char-list parents)
109                        (www-format-feature-name '->denotational lang))))
110       (when (setq parents (www-get-feature-value object '<-subsumptive))
111         (princ (format "<p>%s %s</p>\n<hr>\n"
112                        (www-format-value-as-char-list parents)
113                        (www-format-feature-name '->subsumptive lang))))
114       (when (eq genre 'character)
115         (setq GlyphWiki-id (char-GlyphWiki-id object)))
116       (princ (format "<h%d>%s%s</h%d>\n"
117                      level
118                      (www-format-encode-string
119                       (est-format-object object)
120                       ;; (if (eq genre 'character)
121                       ;;     (char-to-string object)
122                       ;;   (format "%s" (concord-object-id object)))
123                       )
124                      (if GlyphWiki-id
125                          (format
126                           " <a href=\"http://glyphwiki.org/wiki/%s\"><img alt=\"%s\" src=\"http://glyphwiki.org/glyph/%s.50px.png\" /></a>"
127                           GlyphWiki-id
128                           GlyphWiki-id GlyphWiki-id)
129                        "")
130                      level))
131       (if (> level 1)
132           (princ "<ul>"))
133       (when feature-name-to-edit
134         (princ "<form action=\"set.cgi\" method=\"GET\">\n")
135         (princ
136          (encode-coding-string
137           (format "<p>(%s : <input type=\"text\" name=\"%s\"
138 size=\"30\" maxlength=\"30\" value=\"%s\">)</p>
139 "
140                   genre genre
141                   (decode-uri-string uri-object 'utf-8-mcs-er))
142           'utf-8-mcs-er)))
143       (setq object-spec
144             (if (eq genre 'character)
145                 (char-attribute-alist object)
146               (concord-object-spec object)))
147       (when feature-name-to-edit
148         (unless (assq base-name-to-edit object-spec)
149           (setq object-spec (cons (cons base-name-to-edit nil)
150                                   object-spec))))
151       (dolist (cell (sort object-spec
152                           (lambda (a b)
153                             (char-attribute-name<
154                              (char-feature-name-sans-versions (car a))
155                              (char-feature-name-sans-versions (car b))))))
156         (setq logical-feature (char-feature-name-sans-versions (car cell)))
157         (unless (memq logical-feature chise-wiki-displayed-features)
158           (push logical-feature chise-wiki-displayed-features)
159           (cond
160            ((and feature-name-to-edit
161                  (eq (car cell) feature-name-to-edit))
162             (www-edit-display-feature-input-box
163              object feature-name-to-edit editing-format)
164             )
165            (t
166             (princ
167              (if (= level 1)
168                  "<div class=\"feature\" style=\"line-height:150%\">\n"
169                "<li>\n"))
170             (princ
171              (www-format-eval-list
172               (www-feature-format logical-feature)
173               object
174               logical-feature ; (car cell)
175               lang uri-object
176               nil simple))
177             (unless simple
178               (princ
179                (format " <a href=\"%s?%s=%s&feature=%s&format=wiki-text\"
180 ><input type=\"submit\" value=\"note\" /></a>"
181                        chise-wiki-edit-url
182                        genre
183                        (www-format-encode-string uri-object)
184                        (www-format-encode-string
185                         (www-uri-encode-feature-name
186                          (intern (format "%s*note"
187                                          logical-feature ; (car cell)
188                                          )))))))
189             (when (and feature-name-to-edit
190                        (eq base-name-to-edit (car cell)) metadata-name-to-edit)
191               (princ "<ul>\n")
192               (princ "<li>")
193               (www-edit-display-feature-input-box
194                object feature-name-to-edit editing-format)
195               (princ "</li>")
196               (princ "</ul>"))
197             (princ
198              (if (= level 1)
199                  "</div>\n"
200                "<li>\n"))
201             ))
202           ))
203       (princ
204        (if (= level 1)
205            "<p>\n"
206          "<li>\n"))
207       (when feature-name-to-edit
208         (princ "</form>\n"))
209       (unless simple
210         (princ
211          (format "<a href=\"%s?%s=%s\"
212 ><input type=\"submit\" value=\"add feature\" /></a>
213 "
214                  chise-wiki-add-url
215                  genre
216                  (www-format-encode-string uri-object))))
217       (princ
218        (if (= level 1)
219            "<p>\n"
220          "<li>\n"))
221       (when (eq genre 'character)
222         (princ
223          "<form action=\"http://www.chise.org/ids-find\">\n")
224         (princ
225          (www-format-encode-string
226           (est-format-object object)
227           ;; (if (eq genre 'character)
228           ;;     (format "%c" object)
229           ;;   (format "%s" (concord-object-id object)))
230           ))
231         (princ
232          (format
233           " <input type=\"text\" name=\"components\"
234 size=\"30\" maxlength=\"30\" value=\"%s\" />"
235           (encode-coding-string
236            (est-format-object object)
237            ;; (if (eq genre 'character)
238            ;;     (char-to-string object)
239            ;;   (format "%s" (concord-object-id object)))
240            'utf-8-jp-er)))
241         (princ
242          (www-format-encode-string
243           "を\u542Bむ\u6F22\u5B57を\u63A2す"))
244         (princ " <input type=\"submit\" value=\"search\" />\n")
245         (princ "</form>\n"))
246       (princ
247        (if (= level 1)
248            "</p>\n"
249          "<li>\n"))
250       )))
251
252 (defun www-display-feature-desc (uri-feature-name genre uri-object
253                                                   &optional lang simple)
254   (let ((feature-name (www-uri-decode-feature-name uri-feature-name))
255         (name@lang (intern (format "name@%s" lang))))
256     (princ
257      (encode-coding-string
258       (format "<head>
259 <title>EsT feature: %s</title>
260 </head>\n"
261               feature-name)
262       'utf-8-mcs-er))
263     (princ "<body>\n")
264     (princ
265      (format
266       (if simple
267           "<div style=\"text-align:right;\">
268 <a href=\"edit/view.cgi?feature=%s&%s=%s\">
269 <input type=\"submit\" value=\"Edit\" />
270 </a>
271 <input type=\"submit\" value=\"New Account\" />
272 </div>
273 <hr />\n"
274           "<div style=\"text-align:right;\">
275 <a href=\"../view.cgi?feature=%s&%s=%s\">
276 <input type=\"submit\" value=\"Simple\" />
277 </a>
278 </div>
279 <hr />\n")
280       uri-feature-name genre uri-object))
281     (princ
282      (format "<h1>%s</h1>\n"
283              (www-format-encode-string
284               (symbol-name feature-name))))
285     (princ (format "<p>name : %s "
286                    (or (www-format-feature-name feature-name) "")))
287     (unless simple
288       (princ
289        (format
290         " <a href=\"%s?feature=%s&property=name&format=string&%s=%s\">"
291         chise-wiki-edit-url
292         uri-feature-name
293         genre
294         uri-object))
295       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
296     (princ "</p>\n")
297     (when lang
298       (princ "<p>")
299       (princ
300        (www-format-encode-string
301         (format "%s : %s"
302                 name@lang
303                 (or (char-feature-property feature-name name@lang) ""))))
304       (unless simple
305         (princ
306          (format
307           " <a href=\"%s?feature=%s&property=%s&format=string&%s=%s\">"
308           chise-wiki-edit-url
309           uri-feature-name
310           name@lang
311           genre
312           uri-object))
313         (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
314       (princ "</p>\n"))
315     (www-html-display-paragraph
316      (format "type : %s"
317              (or (www-feature-type feature-name)
318                  ;; (char-feature-property feature-name 'type)
319                  'generic)))
320     (princ (format "<p>value-format : %s "
321                    (www-format-value
322                     nil 'value-format 
323                     (or (www-feature-value-format feature-name)
324                         'default)
325                     'default
326                     'without-tags)))
327     (unless simple
328       (princ
329        (format
330         " <a href=\"%s?feature=%s&property=value-format&format=wiki-text&%s=%s\"
331 >"
332         chise-wiki-edit-url
333         uri-feature-name
334         genre
335         uri-object))
336       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
337     (princ "</p>\n")
338
339     (princ "<p>format : ")
340     (www-html-display-text
341      (decode-coding-string
342       (www-xml-format-list
343        (www-feature-format feature-name))
344       'utf-8-mcs-er))
345     (unless simple
346       (princ
347        (format
348         " <a href=\"%s?feature=%s&property=format&format=wiki-text&%s=%s\"
349 >"
350         chise-wiki-edit-url
351         uri-feature-name
352         genre
353         uri-object))
354       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
355     (princ "</p>\n")
356     
357     (www-html-display-paragraph
358      (format "description : %s"
359              (or (char-feature-property feature-name 'description)
360                  "")))
361     (when lang
362       (www-html-display-paragraph
363        (format "description@%s : %s"
364                lang
365                (or (char-feature-property
366                     feature-name
367                     (intern (format "description@%s" lang)))
368                    ""))))
369     ))
370   
371 (defun www-batch-view ()
372   (setq terminal-coding-system 'binary)
373   (condition-case err
374       (let* ((target (pop command-line-args-left))
375              (user (pop command-line-args-left))
376              (accept-language (pop command-line-args-left))
377              (mode (intern (pop command-line-args-left)))
378              (lang
379               (intern
380                (car (split-string
381                      (car (split-string
382                            (car (split-string accept-language ","))
383                            ";"))
384                      "-"))))
385              ret)
386         (princ "Content-Type: text/html; charset=UTF-8
387
388 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
389             \"http://www.w3.org/TR/html4/loose.dtd\">
390 <html lang=\"ja\">
391 ")
392         (cond
393          ((stringp target)
394           (when (string-match "^char=\\(&[^&;]+;\\)" target)
395             (setq ret (match-end 0))
396             (setq target
397                   (concat "char="
398                           (www-uri-encode-object
399                            (www-uri-decode-object
400                             'character (match-string 1 target)))
401                           (substring target ret))))
402           (setq target
403                 (mapcar (lambda (cell)
404                           (if (string-match "=" cell)
405                               (cons
406                                (intern
407                                 (decode-uri-string
408                                  (substring cell 0 (match-beginning 0))
409                                  'utf-8-mcs-er))
410                                (substring cell (match-end 0)))
411                             (list (decode-uri-string cell 'utf-8-mcs-er))))
412                         (split-string target "&")))
413           (setq ret (car target))
414           (cond ((eq (car ret) 'char)
415                  (www-display-object-desc
416                   'character
417                   (cdr ret)
418                   lang nil
419                   (eq mode 'simple))
420                  )
421                 ((eq (car ret) 'feature)
422                  (www-display-feature-desc
423                   (decode-uri-string (cdr ret) 'utf-8-mcs-er)
424                   (car (nth 1 target))
425                   (cdr (nth 1 target))
426                   lang
427                   (eq mode 'simple))
428                  )
429                 (t
430                  (www-display-object-desc
431                   (car ret)
432                   (cdr ret)
433                   lang nil
434                   (eq mode 'simple))
435                  ))
436           ))
437         (princ "\n<hr>\n")
438         (princ (format "mode=%S\n" mode))
439         (princ (format "user=%s\n" user))
440         ;; (princ (format "local user=%s\n" (user-login-name)))
441         (princ (format "lang=%S\n" lang))
442         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
443         ;; (princ " CHISE ")
444         ;; (princ xemacs-chise-version)
445         (princ "
446 </body>
447 </html>")
448         )
449     (error nil
450            (princ (format "%S" err)))
451     ))
452
453 (provide 'cwiki-view)