(www-set-display-char-desc):
[chise/est.git] / cwiki-set.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (defvar chise-wiki-view-url "../view.cgi")
3 (defvar chise-wiki-edit-url "edit.cgi")
4
5 (require 'cwiki-common)
6 (require 'cwiki-view)
7
8
9 (defun www-parse-string-default (string)
10   (setq string (decode-uri-string string 'utf-8-mcs-er))
11   (condition-case nil
12       (let ((ret
13              (mapcar #'read (split-string string " "))))
14         (if (cdr ret)
15             ret
16           (car ret)))
17     (error nil)))
18
19 (defun www-parse-string-as-space-separated-char-list (string)
20   (let (dest char)
21     (dolist (unit (split-string string "\\+"))
22       (if (setq char (www-uri-decode-char unit))
23           (setq dest (cons char dest))))
24     (nreverse dest)))
25
26 (defun www-parse-string-as-space-separated-ids (string)
27   (cdar
28    (ids-parse-string
29     (let (char)
30       (mapconcat
31        (lambda (unit)
32          (if (setq char (www-uri-decode-char unit))
33              (char-to-string char)
34            unit))
35        (split-string string "\\+")
36        "")))))
37
38 (defun www-parse-string-as-ku-ten (string)
39   (if (string-match "^\\([0-9][0-9]?\\)-\\([0-9][0-9]?\\)" string)
40       (let ((ku (string-to-int (match-string 1 string)))
41             (ten (string-to-int (match-string 2 string))))
42         (if (and (<= 1 ku)(<= ku 94)
43                  (<= 1 ten)(<= ten 94))
44             (+ (lsh (+ ku 32) 8)
45                ten 32)))))
46
47 (defun www-parse-string-as-wiki-text (string)
48   (list (decode-uri-string string 'utf-8-mcs-er)))
49
50 (defun www-feature-parse-string (feature-name string &optional format)
51   (unless format
52     (setq format (www-feature-value-format feature-name)))
53   (cond ((eq format 'space-separated-char-list)
54          (www-parse-string-as-space-separated-char-list string))
55         ((eq format 'space-separated-ids)
56          (www-parse-string-as-space-separated-ids string))
57         ((eq format 'ku-ten)
58          (www-parse-string-as-ku-ten string))
59         ((eq format 'decimal)
60          (string-to-number string))
61         ((or (eq format 'HEX)(eq format 'hex))
62          (string-to-number string 16))
63         ((eq format 'string)
64          (decode-uri-string string 'utf-8-mcs-er)
65          )
66         ((eq format 'wiki-text)
67          (www-parse-string-as-wiki-text string)
68          )
69         ((eq format 'S-exp)
70          (if (= (length string) 0)
71              nil
72            (read (decode-uri-string string 'utf-8-mcs-er)))
73          )
74         (t 
75          (www-parse-string-default string)
76          )))
77
78 (defun www-set-display-char-desc (uri-char feature value format &optional lang)
79   (when (stringp feature)
80     (setq feature (intern feature)))
81   (when (stringp format)
82     (setq format (intern format)))
83   (let ((char (www-uri-decode-char uri-char))
84         latest-feature
85         logical-feature displayed-features
86         ret)
87     (when (characterp char)
88       (princ
89        (encode-coding-string
90         (format "<head>
91 <title>CHISE-wiki character: %s</title>
92 </head>\n"
93                 (decode-uri-string uri-char 'utf-8-mcs-er))
94         'utf-8-mcs-er))
95       (princ "<body>\n")
96       (www-html-display-paragraph
97        (format "char: %S %S %S %S\n"
98                uri-char feature value lang))
99       (setq value (www-feature-parse-string feature value format))
100       (www-html-display-paragraph
101        (format "char = %c : %S \u2190 %S"
102                char feature value))
103       (setq latest-feature
104             (char-feature-name-at-domain feature '$rev=latest))
105       (if value
106           (if (equal (www-char-feature char feature) value)
107               (www-html-display-paragraph
108                "Feature-value is not changed.")
109             (www-html-display-paragraph
110              (format "New feature-value = %S is different from old value %S"
111                      value
112                      (www-char-feature char feature)))
113             (put-char-attribute char latest-feature value)
114             (save-char-attribute-table latest-feature)
115             (setq ret (char-feature-property '$object 'additional-features))
116             (unless (memq feature ret)
117               (put-char-feature-property
118                '$object 'additional-features (cons feature ret)))
119             )
120         (www-html-display-paragraph
121          "New feature-value is nil, so it is ignored (may be syntax error).")
122         )
123       (princ (format "<h1>%s</h1>\n"
124                      (www-format-encode-string (char-to-string char))))
125       (dolist (feature (char-feature-property '$object 'additional-features))
126         (mount-char-attribute-table
127          (char-feature-name-at-domain feature '$rev=latest)))
128       (dolist (cell (sort (char-attribute-alist char)
129                           (lambda (a b)
130                             (char-attribute-name< (car a)(car b)))))
131         (setq logical-feature
132               (char-feature-name-sans-versions (car cell)))
133         (unless (memq logical-feature displayed-features)
134           (push logical-feature displayed-features)
135           (princ "<p>")
136           (princ
137            (www-format-eval-list
138             (or (char-feature-property logical-feature 'format)
139                 '((name) " : " (value)))
140             char logical-feature lang uri-char))
141           (princ
142            (format " <a href=\"%s?char=%s&feature=%s&format=wiki-text\"
143 ><input type=\"submit\" value=\"note\" /></a>"
144                    chise-wiki-edit-url
145                    (www-format-encode-string uri-char)
146                    (www-format-encode-string
147                     (www-uri-encode-feature-name
148                      (intern (format "%s*note" logical-feature))))))
149           (princ "</p>\n")
150           ))
151       (princ
152        (format "<p><a href=\"%s?char=%s\"
153 ><input type=\"submit\" value=\"add feature\" /></a></p>"
154                chise-wiki-add-url
155                (www-format-encode-string uri-char)))
156       )))
157
158 (defun www-set-display-feature-desc (feature-name property-name value
159                                                   &optional lang uri-char)
160   (www-html-display-paragraph
161    (format
162     "set: feature: %S, property-name: %S, value: %S, lang: %S, char: %S\n"
163     feature-name property-name value lang uri-char))
164   (put-char-feature-property feature-name property-name value)
165   (let ((name@lang (intern (format "name@%s" lang)))
166         (uri-feature-name (www-uri-encode-feature-name feature-name)))
167     (princ
168      (encode-coding-string
169       (format "<head>
170 <title>CHISE-wiki feature: %s</title>
171 </head>\n"
172               feature-name)
173       'utf-8-mcs-er))
174     (princ "<body>\n")
175     (princ
176      (encode-coding-string
177       (format "<h1>%s</h1>\n"
178               feature-name)
179       'utf-8-mcs-er))
180     (princ
181      (format "<p>name : %s <a href=\"%s?feature=%s&property=name\"
182 ><input type=\"submit\" value=\"edit\" /></a></p>
183 "
184              (or (www-format-feature-name feature-name) "")
185              chise-wiki-edit-url
186              ;; (char-feature-property feature-name 'name)
187              uri-feature-name ; (www-uri-encode-feature-name feature-name)
188              ))
189     (when lang
190       (princ
191        (format "<p>%s : %s <a href=\"%s?feature=%s&property=%s\"
192 ><input type=\"submit\" value=\"edit\" /></a></p>
193 "
194                name@lang
195                (www-format-encode-string
196                 (or (char-feature-property feature-name name@lang) ""))
197                chise-wiki-edit-url
198                uri-feature-name
199                name@lang)))
200     (www-html-display-paragraph
201      (format "type : %s"
202              (or (www-feature-type feature-name)
203                  ;; (char-feature-property feature-name 'type)
204                  'generic)))
205     (www-html-display-paragraph
206      (format "description : %s"
207              (or (char-feature-property feature-name 'description)
208                  "")))
209     (when lang
210       (www-html-display-paragraph
211        (format "description@%s : %s"
212                lang
213                (or (char-feature-property
214                     feature-name
215                     (intern (format "description@%s" lang)))
216                    ""))))
217     (princ "<hr />")
218     (www-html-display-paragraph
219      (format "「[[%c|../view.cgi?char=%s]]」に\u623Bる"
220              (www-uri-decode-char uri-char) uri-char))
221     ))
222
223 (defun www-batch-set ()
224   (setq terminal-coding-system 'binary)
225   (condition-case err
226       (let* ((target (pop command-line-args-left))
227              (user (pop command-line-args-left))
228              (accept-language (pop command-line-args-left))
229              (lang
230               (intern (car (split-string
231                             (car (split-string
232                                   (car (split-string accept-language ","))
233                                   ";"))
234                             "-"))))
235              ret name val prop)
236         (princ "Content-Type: text/html; charset=UTF-8
237
238 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
239             \"http://www.w3.org/TR/html4/loose.dtd\">
240 <html lang=\"ja\">
241 ")
242         (setq target
243               (mapcar (lambda (cell)
244                         (if (string-match "=" cell)
245                             (progn
246                               (setq name (substring
247                                           cell 0 (match-beginning 0))
248                                     val (substring cell (match-end 0)))
249                               (cons
250                                (intern
251                                 (decode-uri-string name 'utf-8-mcs-er))
252                                val))
253                           (list (decode-uri-string cell 'utf-8-mcs-er))))
254                       (split-string target "&")))
255         (setq ret (car target))
256         (cond ((eq (car ret) 'char)
257                (setq prop (nth 2 target))
258                (www-set-display-char-desc
259                 (cdr ret) ; (decode-uri-string (cdr ret) 'utf-8-mcs-er)
260                 (intern (decode-uri-string
261                          (cdr (assq 'feature-name target))
262                          'utf-8-mcs-er))
263                 (cdr prop) ; (decode-uri-string (cdr prop) 'utf-8-mcs-er)
264                 (car prop)
265                 lang)
266                )
267               ((eq (car ret) 'feature)
268                (setq prop (nth 2 target))
269                (www-set-display-feature-desc
270                 (intern (decode-uri-string (cdr ret) 'utf-8-mcs-er))
271                 (car prop)
272                 (decode-uri-string (cdr prop) 'utf-8-mcs-er)
273                 lang
274                 (cdr (assq 'char target))
275                 ;; (decode-uri-string (cdr (assq 'char target)))
276                 )
277                ))
278         (www-html-display-paragraph
279          (format "%S" target))
280         (princ "\n<hr>\n")
281         (princ (format "user=%s\n" user))
282         (princ (format "local user=%s\n" (user-login-name)))
283         (princ (format "lang=%S\n" lang))
284         (princ emacs-version)
285         (princ " CHISE ")
286         (princ xemacs-chise-version)
287         (princ "
288 </body>
289 </html>")
290         )
291     (error nil
292            (princ (format "%S" err)))
293     ))