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