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