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