(www-ids-find-version): New constant.
[chise/ids.git] / www / www-ids-find.el
1 (require 'ids-find)
2
3 (defun decode-url-string (string &optional coding-system)
4   (if (> (length string) 0)
5       (let ((i 0)
6             dest)
7         (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
8           (setq dest (concat dest
9                              (substring string i (match-beginning 0))
10                              (char-to-string
11                               (int-char
12                                (string-to-int (match-string 1 string) 16))))
13                 i (match-end 0)))
14         (decode-coding-string
15          (concat dest (substring string i))
16          coding-system))))
17
18 (defconst www-ids-find-version "0.22.1")
19
20 (defvar www-ids-find-ideographic-products-file-name
21   (expand-file-name "ideographic-products"
22                     (expand-file-name
23                      "feature"
24                      (expand-file-name
25                       "character"
26                       chise-system-db-directory))))
27
28 (defvar www-ids-find-tang-chars-file-name
29   "~tomo/projects/chise/ids/www/tang-chars.udd")
30
31 (defun www-ids-find-format-line (c is)
32   (let ((str (encode-coding-string (format "%c" c) 'utf-8-er))
33         plane code ucs)
34     (princ
35      (with-temp-buffer
36        (cond
37         ((string-match "&CB\\([0-9]+\\);" str)
38          (setq code (string-to-int (match-string 1 str)))
39          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
40          (insert str)
41          (insert (format "\"><img alt=\"CB%05d\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/cb-gaiji/%02d/CB%05d.gif\">\n"
42                          code (/ code 1000) code))
43          (insert (format "CB%05d</a>" code))
44          )
45         ((string-match "&JC3-\\([0-9A-F]+\\);" str)
46          (setq code (string-to-int (match-string 1 str) 16))
47          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
48          (insert str)
49          (insert (format "\"><img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">\n"
50                          code code))
51          (insert (format "JC3-%04X</a>" code))
52          )
53         ((string-match "&J\\(78\\|83\\|90\\|SP\\)-\\([0-9A-F]+\\);" str)
54          (setq plane (match-string 1 str)
55                code (string-to-int (match-string 2 str) 16))
56          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
57          (insert str)
58          (insert (format "\"><img alt=\"J%s-%04X\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/JIS-%s/%02d-%02d.gif\">\n"
59                          plane code plane
60                          (- (lsh code -8) 32)
61                          (- (logand code 255) 32)))
62          (insert (format "J%s-%04X</a>" plane code))
63          )
64         ((string-match "&G\\([01]\\)-\\([0-9A-F]+\\);" str)
65          (setq plane (string-to-int (match-string 1 str))
66                code (string-to-int (match-string 2 str) 16))
67          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
68          (insert str)
69          (insert (format "\"><img alt=\"G%d-%04X\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/GB%d/%02d-%02d.gif\">\n"
70                          plane code plane
71                          (- (lsh code -8) 32)
72                          (- (logand code 255) 32)))
73          (insert (format "G%d-%04X</a>" plane code))
74          )
75         ((string-match "&C\\([1-7]\\)-\\([0-9A-F]+\\);" str)
76          (setq plane (string-to-int (match-string 1 str))
77                code (string-to-int (match-string 2 str) 16))
78          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
79          (insert str)
80          (insert (format "\"><img alt=\"C%d-%04X\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/CNS%d/%04X.gif\">\n"
81                          plane code plane code))
82          (insert (format "C%d-%04X</a>" plane code))
83          )
84         ((string-match "&ZOB-\\([0-9]+\\);" str)
85          (setq code (string-to-int (match-string 1 str)))
86          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
87          (insert str)
88          (insert (format "\"><img alt=\"ZOB-%04d\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/ZOB-1968/%04d.png\">\n"
89                          code code))
90          (insert (format "ZOB-%04d</a>" code))
91          )
92         (t
93          (insert "<a href=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/char-desc?char=")
94          ;; (insert str)
95          (insert
96           (mapconcat (lambda (c)
97                        (if (<= (char-int c) #x7F)
98                            (char-to-string c)
99                          (format "%%%02X" c)))
100                      str ""))
101          (insert "\">")
102          (insert str)
103          (insert "</a>")
104          ))
105        (goto-char (point-min))
106        (while (search-forward "&" nil t)
107          (replace-match "&amp;" t 'literal))
108        (buffer-string)
109        ))
110     (princ
111      (or (if (setq ucs (or (char-ucs c)
112                            (encode-char c 'ucs)))
113              (format " <a href=\"http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=%X\">%s</a>"
114                      ucs
115                      (cond ((<= ucs #xFFFF)
116                             (format "U+%04X" ucs))
117                            ((<= ucs #x10FFFF)
118                             (format "U-%08X" ucs))))
119            "          ")))
120     (princ " ")
121     (when is
122       (princ
123        (with-temp-buffer
124          (insert
125           (encode-coding-string
126            (ideographic-structure-to-ids is)
127            'utf-8-jp-er))
128          (goto-char (point-min))
129          (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
130            (setq code (string-to-int (match-string 1)))
131            (replace-match
132             (format "<img alt=\"CB%05d\" src=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/glyphs/cb-gaiji/%02d/CB%05d.gif\">"
133                     code (/ code 1000) code)
134             t 'literal))
135          (buffer-string))))
136     (when (and ucs
137                (with-current-buffer
138                    (find-file-noselect
139                     www-ids-find-tang-chars-file-name)
140                  (goto-char (point-min))
141                  (re-search-forward (format "^%d$" ucs) nil t)))
142       (princ
143        (format " <a href=\"http://coe21.zinbun.kyoto-u.ac.jp/djvuchar?query=%s\">"
144                (mapconcat
145                 (lambda (c)
146                   (format "%%%02X" (char-int c)))
147                 (encode-coding-string (char-to-string c)
148                                       'utf-8-jp)
149                 "")))
150       (princ (encode-coding-string "\e$B"M\e(B[\e$BEbBeBsK\\e(B]</a>" 'utf-8-jp-er)))
151     (princ "<br>\n")))
152
153 (defun www-ids-insert-chars-including-components (components)
154   (let (is)
155     (dolist (c (ideographic-products-find components))
156       (setq is (char-feature c 'ideographic-structure))
157       ;; to avoid problems caused by wrong indexes
158       (when (every (lambda (cc)
159                      (ideographic-structure-member cc is))
160                    components)
161         (princ "<li>")
162         (www-ids-find-format-line c is)
163         (princ "<ul>\n")
164         (www-ids-insert-chars-including-components (char-to-string c))
165         (princ "</ul>\n")
166         )
167       )))
168
169 (defun www-batch-ids-find ()
170   (let ((components (car command-line-args-left))
171         (coded-charset-entity-reference-alist
172          (list*
173           '(=cns11643-1         "C1-" 4 X)
174           '(=cns11643-2         "C2-" 4 X)
175           '(=cns11643-3         "C3-" 4 X)
176           '(=cns11643-4         "C4-" 4 X)
177           '(=cns11643-5         "C5-" 4 X)
178           '(=cns11643-6         "C6-" 4 X)
179           '(=cns11643-7         "C7-" 4 X)
180           '(=gb2312             "G0-" 4 X)
181           '(=gb12345            "G1-" 4 X)
182           '(=jis-x0208@1990     "J90-" 4 X)
183           '(=jis-x0212          "JSP-" 4 X)
184           '(=cbeta              "CB" 5 d)
185           '(=jef-china3         "JC3-" 4 X)
186           '(=jis-x0208@1978     "J78-" 4 X)
187           '(=jis-x0208@1983     "J83-" 4 X)
188           '(=daikanwa           "M-" 5 d)
189           coded-charset-entity-reference-alist))
190         )
191     (setq command-line-args-left (cdr command-line-args-left))
192     (cond
193      ((stringp components)
194       (if (string-match "^components=" components)
195           (setq components (substring components (match-end 0))))
196       (setq components
197             (if (> (length components) 0)
198                 (decode-url-string components 'utf-8-jp-er)
199               nil))
200       )
201      (t
202       (setq components nil)
203       ))
204     (princ "Content-Type: text/html; charset=UTF-8
205
206 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
207             \"http://www.w3.org/TR/html4/loose.dtd\">
208 <html lang=\"ja\">
209 <head>
210 <title>CHISE IDS Find</title>
211 </head>
212
213 <body>
214
215 <h1>")
216     (princ (encode-coding-string "CHISE IDS \e$B4A;z8!:w\e(B" 'utf-8-jp-er))
217     (princ "</h1>")
218     (princ "
219 <p>Version ")
220     (princ www-ids-find-version)
221     (princ (format-time-string
222             " (Last-modified: %Y-%m-%d %H:%M:%S)"
223             (nth 5
224                  (file-attributes
225                   www-ids-find-ideographic-products-file-name))))
226     (princ "
227 <p>
228 Copyright (C) 2005 <a href=\"http://kanji.zinbun.kyoto-u.ac.jp/~tomo/\"
229 >MORIOKA Tomohiko</a>
230 <hr>
231 <p>
232 <form action=\"http://mousai.kanji.zinbun.kyoto-u.ac.jp/ids-find\" method=\"GET\">
233 ")
234     (princ (encode-coding-string "\e$BItIJJ8;zNs\e(B" 'utf-8-jp-er))
235     (princ " <input type=\"text\" name=\"components\" size=\"30\" maxlength=\"30\" value=\"")
236     (if (> (length components) 0)
237         (princ (encode-coding-string components 'utf-8-jp-er)))
238     (princ "\">
239 <input type=\"submit\" value=\"")
240     (princ (encode-coding-string "\e$B8!:w3+;O\e(B" 'utf-8-jp-er))
241     (princ "\">
242 </form>
243
244 ")
245     (unless (file-newer-than-file-p
246              www-ids-find-ideographic-products-file-name
247              (locate-file (car command-line-args) exec-path))
248       (princ (encode-coding-string "<hr>
249 <p>
250 \e$B8=:_!"%7%9%F%`$N99?7:n6HCf$G$9!#$7$P$i$/$*BT$A$/$@$5$$!#\e(B
251 <hr>
252 " 'utf-8-jp-er))
253       ;; (setq components nil)
254       )
255     (cond
256      (components
257       ;; (map-char-attribute
258       ;;  (lambda (c v)
259       ;;    (when (every (lambda (p)
260       ;;                   (ideographic-structure-member p v))
261       ;;                 components)
262       ;;      (princ (encode-coding-string
263       ;;              (ids-find-format-line c v)
264       ;;              'utf-8-jp-er))
265       ;;      (princ "<br>\n")
266       ;;      )
267       ;;    nil)
268       ;;  'ideographic-structure)
269       (when (= (length components) 1)
270         (www-ids-find-format-line (aref components 0)
271                                   (char-feature (aref components 0)
272                                                 'ideographic-structure)))
273       ;; (dolist (c (ideographic-products-find components))
274       ;;   (setq is (char-feature c 'ideographic-structure))
275       ;;   ;; to avoid problems caused by wrong indexes
276       ;;   (when (every (lambda (c)
277       ;;                  (ideographic-structure-member c is))
278       ;;                components)
279       ;;     (www-ids-find-format-line c is)))
280       (princ "<ul>\n")
281       (www-ids-insert-chars-including-components components)
282       (princ "</ul>\n")
283       )
284      (t
285       (princ (encode-coding-string "<hr>
286 <p>
287 \e$B;XDj$7$?ItIJ$rA4$F4^$`4A;z$N0lMw$rI=<($7$^$9!#\e(B
288 <p>
289 CHISE \e$B$GMQ$$$i$l$k<BBV;2>H7A<0!JNc!'\e(B&amp;M-00256;\e$B!K$GItIJ$r;XDj$9$k;v$b$G$-$^$9!#\e(B" 'utf-8-jp-er))
290       (princ (encode-coding-string "
291 <p>
292 \[Links\]
293 <ul>
294 <li><a href=\"http://www.shuiren.org/chuden/toyoshi/syoseki/chise_ids.html\"
295 >\e$B!V\e(BCHISE IDS FIND\e$B$G4A;z$r8!:w!W\e(B</a> \e$B!=\e(B \e$B;3ED?r?N$5$s!J\e(B<a
296 href=\"http://www.shuiren.org/\">\e$B?g?MDb\e(B</a>\e$B!K$K$h$k2r@b\e(B
297 </ul>
298 <ul>
299 <li><a href=\"http://www.karitsu.org/tools/firefox_plugin.htm\"
300 >Firefox \e$BMQ\e(B plugin</a> by \e$B=);3M[0lO:$5$s!J\e(B<a href=\"http://www.karitsu.org/\"
301 >\e$B2aN)c7\e(B</a>\e$B!K\e(B
302 </ul>
303 <ul>
304 <li><a href=\"http://cvs.m17n.org/viewcvs/chise/ids/www/www-ids-find.el?view=markup\"
305 >www-ids-find.el (source file (Emacs Lisp part))
306 <li><a href=\"http://kanji.zinbun.kyoto-u.ac.jp/projects/chise/ids/\"
307 >\e$B!V\e(BCHISE \e$B4A;z9=B$>pJs%G!<%?%Y!<%9!W\e(B</a>
308 <li><a href=\"http://kanji.zinbun.kyoto-u.ac.jp/projects/chise/\"
309 >CHISE Project</a>
310 </ul>
311 <ul>
312 <li><a href=\"http://coe21.zinbun.kyoto-u.ac.jp/djvuchar\"
313 >\e$B!VBsK\J8;z%G!<%?%Y!<%9!W\e(B</a> by
314 <a href=\"http://coe21.zinbun.kyoto-u.ac.jp/\"
315 >\e$B5~ETBg3X\e(B21\e$B@$5*\e(BCOE\e$B!VEl%"%8%"@$3&$N?MJ8>pJs3X8&5f650i5rE@!W\e(B</a>
316 <li><a href=\"http://www.unicode.org/\"
317 >Unicode</a>
318 </ul>"
319  'utf-8-jp-er))
320
321       ))
322     (princ "<hr>")
323     (princ
324      (format
325       "Powered by <a
326 href=\"http://kanji.zinbun.kyoto-u.ac.jp/projects/chise/xemacs/\"
327 >XEmacs CHISE</a> %s."
328       xemacs-chise-version))
329     (princ "
330 </body>
331 </html>
332 ")))