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