update.
[chise/est.git] / cwiki-common.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'char-db-util)
3 (require 'chiset-common)
4 ;; (require 'concord-images)
5
6 (setq file-name-coding-system 'utf-8-mcs-er)
7
8 (concord-assign-genre 'code-point "/usr/local/var/chise-ipld/db")
9 (concord-assign-genre 'coded-character "/usr/local/var/chise-ipld/db")
10 (concord-assign-genre 'glyph "/usr/local/var/chise-ipld/db")
11
12 (concord-assign-genre 'image-resource "/usr/local/var/photo/db")
13 (concord-assign-genre 'glyph-image "/usr/local/var/photo/db")
14
15 (concord-assign-genre 'creator@ruimoku "/usr/local/var/ruimoku/db")
16 (concord-assign-genre 'person-name@ruimoku "/usr/local/var/ruimoku/db")
17
18 (concord-assign-genre 'journal-volume@ruimoku "/usr/local/var/ruimoku/db")
19 (concord-assign-genre 'article@ruimoku "/usr/local/var/ruimoku/db")
20 (concord-assign-genre 'book@ruimoku "/usr/local/var/ruimoku/db")
21
22 (concord-assign-genre 'classification@ruimoku "/usr/local/var/ruimoku/db")
23 (concord-assign-genre 'region@ruimoku "/usr/local/var/ruimoku/db")
24 (concord-assign-genre 'era@ruimoku "/usr/local/var/ruimoku/db")
25 (concord-assign-genre 'period@ruimoku "/usr/local/var/ruimoku/db")
26 (concord-assign-genre 'journal@ruimoku "/usr/local/var/ruimoku/db")
27 (concord-assign-genre 'journal-name@ruimoku "/usr/local/var/ruimoku/db")
28 (concord-assign-genre 'publisher@ruimoku "/usr/local/var/ruimoku/db")
29 (concord-assign-genre 'publisher-name@ruimoku "/usr/local/var/ruimoku/db")
30
31 (mount-char-attribute-table '*instance@ruimoku/bibliography/title)
32 ;; (mount-char-attribute-table '*instance@ruimoku/bibliography/content*note)
33
34 (concord-assign-genre 'entry@zh-classical "/usr/local/var/kanbun/db")
35 ;; (concord-assign-genre 'morpheme-entry@zh-classical "/usr/local/var/kanbun/db")
36 (concord-assign-genre 'word-class@zh-classical "/usr/local/var/kanbun/db")
37 (concord-assign-genre 'morpheme@zh-classical "/usr/local/var/kanbun/db")
38 (concord-assign-genre 'sentence@zh-classical "/usr/local/var/kanbun/db")
39 ;; (concord-assign-genre 'sentence-entry@zh-classical "/usr/local/var/kanbun/db")
40
41 (mount-char-attribute-table '*instance@morpheme-entry/zh-classical)
42
43 (concord-assign-genre 'ud@zh-classical "/usr/local/var/kanbun/db")
44
45
46 (concord-assign-genre 'hng-card "/usr/local/var/hng-card/db")
47
48 (mount-char-attribute-table '->HNG)
49 (mount-char-attribute-table '<-HNG)
50 (mount-char-attribute-table '->HNG@CN/manuscript)
51 (mount-char-attribute-table '<-HNG@CN/manuscript)
52 (mount-char-attribute-table '->HNG@CN/printed)
53 (mount-char-attribute-table '<-HNG@CN/printed)
54 (mount-char-attribute-table '->HNG@JP/manuscript)
55 (mount-char-attribute-table '<-HNG@JP/manuscript)
56 (mount-char-attribute-table '->HNG@JP/printed)
57 (mount-char-attribute-table '<-HNG@JP/printed)
58 (mount-char-attribute-table '->HNG@KR)
59 (mount-char-attribute-table '<-HNG@KR)
60 (mount-char-attribute-table '->HNG@MISC)
61 (mount-char-attribute-table '<-HNG@MISC)
62
63 (mount-char-attribute-table 'abstract-glyph@iwds-1)
64
65
66 (defvar est-hide-cgi-mode nil)
67 (defvar est-view-url-prefix "..")
68 (defvar chise-wiki-view-url "view.cgi")
69 (defvar chise-wiki-edit-url "edit.cgi")
70
71 (defvar chise-wiki-bitmap-glyphs-url
72   "http://www.chise.org/glyphs")
73
74 (defvar chise-wiki-hng-bitmap-glyphs-url
75   "http://hng.chise.org/glyphs/HNG")
76
77 (defvar chise-wiki-daijiten-bitmap-glyphs-url
78   "http://hng.chise.org/glyphs/daijiten")
79
80 (defvar chise-wiki-glyph-cgi-url
81   "http://www.chise.org/chisewiki/glyph.cgi")
82
83 (defvar chise-wiki-displayed-features nil)
84
85 (defvar est-coded-charset-priority-list
86   '(; =ucs
87     =cns11643-1 =cns11643-2 =cns11643-3
88     =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
89     =gb2312 =gb12345
90     =jis-x0208 =jis-x0208@1990
91     =jis-x0213-2
92     =jis-x0212
93     =adobe-japan1
94     =cbeta =jef-china3
95     =jis-x0213-1@2000 =jis-x0213-1@2004
96     =jis-x0208@1983 =jis-x0208@1978
97     =zinbun-oracle =>zinbun-oracle
98     =daikanwa
99     =gt =gt-k
100     =>>>adobe-japan1
101     =>>>jis-x0208 =>>>jis-x0213-1 =>>>jis-x0213-2
102     =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
103     =>>adobe-japan1
104     =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
105     =+>jis-x0208@1978
106     =+>adobe-japan1
107     =>jis-x0208 =>jis-x0213-1
108     =>>gt
109     =>ucs@iso =>ucs@unicode
110     =>ucs@jis =>ucs@cns =>ucs@ks
111     =+>ucs@iso =+>ucs@unicode
112     =>>ucs@iso =>>ucs@unicode
113     =>>ucs@jis =>>ucs@cns =>>ucs@ks
114     =>>>ucs@iso =>>>ucs@unicode
115     =>>>ucs@jis =>>>ucs@cns =>>>ucs@ks
116     ===ucs@iso
117     =ruimoku-v6
118     =big5
119     =big5-cdp
120     =>cbeta
121     =mj
122     ==mj
123     ===mj
124     =ucs-itaiji-001
125     =ucs-itaiji-002
126     =ucs-itaiji-003
127     =ucs-itaiji-004
128     =ucs-itaiji-005
129     =ucs-itaiji-006
130     =ucs-itaiji-007
131     =ucs-itaiji-008
132     =ucs-itaiji-009
133     =ucs-itaiji-010
134     =ucs-itaiji-011
135     =ucs-itaiji-084
136     =ucs-var-001
137     =ucs-var-002
138     =ucs-var-003
139     =ucs-var-004
140     =ucs@unicode
141     ==ucs@unicode
142     ===ucs@unicode
143     ==>daijiten
144     ==>ucs@bucs
145     ===daikanwa/+p
146     ===gt
147     =>ucs@iwds-1
148     =>ucs@component
149     =>ucs-itaiji-001
150     =>ucs-itaiji-002
151     =>ucs-itaiji-003
152     =>ucs-itaiji-004
153     =>ucs-itaiji-005
154     =>ucs-itaiji-006
155     =>ucs-itaiji-007
156     ===adobe-japan1
157     ===cns11643-1 ===cns11643-2 ===cns11643-3
158     ===cns11643-4 ===cns11643-5 ===cns11643-6 ===cns11643-7
159     ))
160
161 (defun decode-uri-string (string &optional coding-system)
162   (if (> (length string) 0)
163       (let ((i 0)
164             dest)
165         (setq string
166               (mapconcat (lambda (char)
167                            (if (eq char ?+)
168                                " "
169                              (char-to-string char)))
170                          string ""))
171         (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
172           (setq dest (concat dest
173                              (substring string i (match-beginning 0))
174                              (char-to-string
175                               (int-char
176                                (string-to-int (match-string 1 string) 16))))
177                 i (match-end 0)))
178         (decode-coding-string
179          (concat dest (substring string i))
180          coding-system))))
181
182 (defun www-feature-type (feature-name)
183   (or (char-feature-property feature-name 'type)
184       (let ((str (symbol-name feature-name)))
185         (cond
186          ((string-match "\\*note\\(@[^*]+\\)?$" str)
187           'stext)
188          ((string-match "\\*sources\\(@[^*]+\\)?$" str)
189           'domain-list)
190          ((string-match "\\*" str)
191           nil)
192          ((string-match "^\\(->\\|<-\\)" str)
193           'relation)
194          ((string-match "^ideographic-structure\\(@\\|$\\)" str)
195           'structure)
196          ))))
197
198 (defun www-feature-format (feature-name)
199   (or (char-feature-property feature-name 'presentation-format)
200       (char-feature-property feature-name 'format)
201       (let (fn parent ret)
202         (setq fn feature-name)
203         (while (and (setq parent (char-feature-name-parent fn))
204                     (null (setq ret
205                                 (char-feature-property
206                                  parent 'format))))
207           (setq fn parent))
208         ret)
209       '((name) " : " (value))))
210
211 (defun www-feature-value-format (feature-name)
212   (or (char-feature-property feature-name 'value-presentation-format)
213       (char-feature-property feature-name 'value-format)
214       (let (fn parent ret)
215         (setq fn feature-name)
216         (while (and (setq parent (char-feature-name-parent fn))
217                     (null (setq ret
218                                 (or (char-feature-property
219                                      parent 'value-presentation-format)
220                                     (char-feature-property
221                                      parent 'value-format)))))
222           (setq fn parent))
223         ret)
224       (let ((type (www-feature-type feature-name)))
225         (cond ((eq type 'relation)
226                'space-separated)
227               ((eq type 'structure)
228                'space-separated-ids)
229               ((eq type 'stext)
230                'wiki-text)
231               ))
232       (if (find-charset feature-name)
233           (if (and (= (charset-dimension feature-name) 2)
234                    (= (charset-chars feature-name) 94))
235               '("0x" (HEX)
236                 " (" (decimal) ") <" (ku-ten) "> " (prev-char) (next-char))
237             '("0x" (HEX) " (" (decimal) ") " (prev-char) (next-char))))
238       'space-separated))
239
240 (defun char-feature-name-at-domain (feature-name domain)
241   (if domain
242       (let ((name (symbol-name feature-name)))
243         (cond
244          ((string-match "@[^*]+$" name)
245           (intern (format "%s/%s" name domain))
246           )
247          (t
248           (intern (format "%s@%s" name domain))
249           )))
250     feature-name))
251
252 (defun char-feature-name-parent (feature-name)
253   (let ((name (symbol-name feature-name)))
254     (if (string-match "@[^@/*]+\\(/[^@/*]+\\)*$" name)
255         (intern (substring name 0 (car (last (match-data) 2)))))))
256
257 (defun char-feature-name-domain (feature-name)
258   (let ((name (symbol-name feature-name)))
259     (if (string-match "@[^@/*]+\\(/[^@/*]+\\)*$" name)
260         (intern (substring name (1+ (match-beginning 0)))))))
261
262 (defun char-feature-name-sans-versions (feature)
263   (let ((feature-name (symbol-name feature)))
264     (if (string-match "[@/]\\$rev=latest$" feature-name)
265         (intern (substring feature-name 0 (match-beginning 0)))
266       feature)))
267
268 (defun est-object-genre (object)
269   (if (characterp object)
270       'character
271     (concord-object-genre object)))
272
273 (defun www-get-feature-value (object feature)
274   (let ((latest-feature (char-feature-name-at-domain feature '$rev=latest)))
275     (cond
276      ((characterp object)
277       (mount-char-attribute-table latest-feature)
278       (or (char-feature object latest-feature)
279           (char-feature object feature))
280       )
281      (t
282       (or (condition-case nil
283               (concord-object-get object latest-feature)
284             (error nil))
285           (condition-case nil
286               (concord-object-get object feature)
287             (error nil)))
288       ))))
289
290 (defun get-previous-code-point (ccs code)
291   (let ((chars (charset-chars ccs))
292         (dim (charset-dimension ccs))
293         (i 0)
294         mask byte-min byte-max
295         bytes dest)
296     (cond
297      ((= chars 94)
298       (setq mask #x7F
299             byte-min 33
300             byte-max 126)
301       )
302      ((= chars 96)
303       (setq mask #x7F
304             byte-min 32
305             byte-max 127)
306       )
307      ((= chars 128)
308       (setq mask #x7F
309             byte-min 0
310             byte-max #xFF)
311       )
312      (t ; (= chars 256)
313       (setq mask #xFF
314             byte-min 0
315             byte-max #xFF)
316       ))
317     (setq bytes (make-vector dim 0))
318     (while (< i dim)
319       (aset bytes i (logand (lsh code (* i -8)) mask))
320       (setq i (1+ i)))
321     (setq i 0)
322     (while (and (< i dim)
323                 (progn
324                   (aset bytes i (1- (aref bytes i)))
325                   (< (aref bytes i) byte-min)))
326       (aset bytes i byte-max)
327       (setq i (1+ i)))
328     (when (< i dim)
329       (setq dest (aref bytes 0)
330             i 1)
331       (while (< i dim)
332         (setq dest (logior dest (lsh (aref bytes i) (* i 8)))
333               i (1+ i)))
334       dest)))
335
336 (defun get-next-code-point (ccs code)
337   (let ((chars (charset-chars ccs))
338         (dim (charset-dimension ccs))
339         (i 0)
340         mask byte-min byte-max
341         bytes dest)
342     (cond
343      ((= chars 94)
344       (setq mask #x7F
345             byte-min 33
346             byte-max 126)
347       )
348      ((= chars 96)
349       (setq mask #x7F
350             byte-min 32
351             byte-max 127)
352       )
353      ((= chars 128)
354       (setq mask #x7F
355             byte-min 0
356             byte-max #xFF)
357       )
358      (t ; (= chars 256)
359       (setq mask #xFF
360             byte-min 0
361             byte-max #xFF)
362       ))
363     (setq bytes (make-vector dim 0))
364     (while (< i dim)
365       (aset bytes i (logand (lsh code (* i -8)) mask))
366       (setq i (1+ i)))
367     (setq i 0)
368     (while (and (< i dim)
369                 (progn
370                   (aset bytes i (1+ (aref bytes i)))
371                   (> (aref bytes i) byte-max)))
372       (aset bytes i byte-min)
373       (setq i (1+ i)))
374     (when (< i dim)
375       (setq dest (aref bytes 0)
376             i 1)
377       (while (< i dim)
378         (setq dest (logior dest (lsh (aref bytes i) (* i 8)))
379               i (1+ i)))
380       dest)))
381
382 (defun find-previous-defined-code-point (ccs code)
383   (let ((i (get-previous-code-point ccs code))
384         char)
385     (cond
386      ((eq ccs '=jis-x0208)
387       (setq ccs '=jis-x0208@1990))
388      ((eq ccs '=jis-x0213-1)
389       (setq ccs '=jis-x0213-1@2004)))
390     (while (and i
391                 (>= i 0)
392                 (null (setq char (decode-char ccs i
393                                               (unless (eq ccs '=ucs)
394                                                 'defined-only)))))
395       (setq i (get-previous-code-point ccs i)))
396     char))
397
398 (defun find-next-defined-code-point (ccs code)
399   (let ((i (get-next-code-point ccs code))
400         max char)
401     (setq max (+ code 1000))
402     (cond
403      ((eq ccs '=jis-x0208)
404       (setq ccs '=jis-x0208@1990))
405      ((eq ccs '=jis-x0213-1)
406       (setq ccs '=jis-x0213-1@2004)))
407     (while (and i
408                 (<= i max)
409                 (null (setq char (decode-char ccs i
410                                               (unless (eq ccs '=ucs)
411                                                 'defined-only)))))
412       (setq i (get-next-code-point ccs i)))
413     char))
414
415
416 ;;; @ URI representation
417 ;;;
418
419 ;; (defun est-uri-decode-feature-name-body (uri-feature)
420 ;;   (let ((len (length uri-feature))
421 ;;         (i 0)
422 ;;         ch dest)
423 ;;     (while (< i len)
424 ;;       (setq dest
425 ;;             (concat
426 ;;              dest
427 ;;              (if (eq (aref uri-feature i) ?\.)
428 ;;                  (if (and (< (+ i 2) len)
429 ;;                           (eq (aref uri-feature (+ i 2)) ?\.))
430 ;;                      (prog1
431 ;;                          (cond
432 ;;                           ((eq (setq ch (aref uri-feature (1+ i))) ?\.)
433 ;;                            "/")
434 ;;                           ((eq ch ?-)
435 ;;                            "*")
436 ;;                           ((eq ch ?_)
437 ;;                            "+")
438 ;;                           (t
439 ;;                            (substring uri-feature i (+ i 3))
440 ;;                            ))
441 ;;                        (setq i (+ i 3)))
442 ;;                    (setq i (1+ i))
443 ;;                    ".")
444 ;;                (prog1
445 ;;                    (char-to-string (aref uri-feature i))
446 ;;                  (setq i (1+ i)))))))
447 ;;     dest))
448
449 ;; (defun est-uri-encode-feature-name-body (feature)
450 ;;   (mapconcat (lambda (c)
451 ;;                (cond ((eq c ?*)
452 ;;                       ".-.")
453 ;;                      ((eq c ?/)
454 ;;                       "...")
455 ;;                      ((eq c ?+)
456 ;;                       "._.")
457 ;;                      (t (char-to-string c))))
458 ;;              feature ""))
459
460 (defun www-uri-decode-feature-name (uri-feature)
461   (let (feature)
462     (setq uri-feature (decode-uri-string uri-feature 'utf-8-mcs-er))
463     (cond
464      ((string-match "^from\\." uri-feature)
465       (intern (format "<-%s"
466                       (est-uri-decode-feature-name-body
467                        (substring uri-feature (match-end 0)))))
468       )
469      ((string-match "^to\\." uri-feature)
470       (intern (format "->%s"
471                       (est-uri-decode-feature-name-body
472                        (substring uri-feature (match-end 0)))))
473       )
474      ((string-match "^rep\\." uri-feature)
475       (intern (format "=%s"
476                       (est-uri-decode-feature-name-body
477                        (substring uri-feature (match-end 0)))))
478       )
479      ((string-match "^rep[2i]\\." uri-feature)
480       (intern (format "===%s"
481                       (est-uri-decode-feature-name-body
482                        (substring uri-feature (match-end 0)))))
483       )
484      ((string-match "^g\\." uri-feature)
485       (intern (format "=>>%s"
486                       (est-uri-decode-feature-name-body
487                        (substring uri-feature (match-end 0)))))
488       )
489      ((string-match "^g[i2]\\." uri-feature)
490       (intern (format "==%s"
491                       (est-uri-decode-feature-name-body
492                        (substring uri-feature (match-end 0)))))
493       )
494      ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
495       (intern (format "=>>%s%s"
496                       (make-string (string-to-int
497                                     (match-string 1 uri-feature))
498                                    ?>)
499                       (est-uri-decode-feature-name-body
500                        (substring uri-feature (match-end 0)))))
501       )
502      ((string-match "^o\\." uri-feature)
503       (intern (format "=+>%s"
504                       (est-uri-decode-feature-name-body
505                        (substring uri-feature (match-end 0)))))
506       )
507      ((string-match "^a\\." uri-feature)
508       (intern (format "=>%s"
509                       (est-uri-decode-feature-name-body
510                        (substring uri-feature (match-end 0)))))
511       )
512      ((string-match "^a\\([0-9]+\\)\\." uri-feature)
513       (intern (format "%s>%s"
514                       (make-string (string-to-int
515                                     (match-string 1 uri-feature))
516                                    ?=)
517                       (est-uri-decode-feature-name-body
518                        (substring uri-feature (match-end 0)))))
519       )
520      ((and (setq uri-feature (est-uri-decode-feature-name-body uri-feature))
521            (setq feature (intern (format "=>%s" uri-feature)))
522            (find-charset feature))
523       feature)
524      ((and (setq feature (intern (format "=>>%s" uri-feature)))
525            (find-charset feature))
526       feature)
527      ((and (setq feature (intern (format "=>>>%s" uri-feature)))
528            (find-charset feature))
529       feature)
530      ((and (setq feature (intern (format "=%s" uri-feature)))
531            (find-charset feature))
532       feature)
533      (t (intern uri-feature)))))
534
535 (defun www-uri-encode-feature-name (feature-name)
536   (setq feature-name (symbol-name feature-name))
537   (cond
538    ((string-match "^=\\+>\\([^=>]+\\)" feature-name)
539     (concat "o."
540             (est-uri-encode-feature-name-body
541              (substring feature-name (match-beginning 1))))
542     )
543    ((string-match "^=\\([^=>]+\\)" feature-name)
544     (concat "rep."
545             (est-uri-encode-feature-name-body
546              (substring feature-name (match-beginning 1))))
547     )
548    ((string-match "^==\\([^=>]+\\)" feature-name)
549     (concat "g2."
550             (est-uri-encode-feature-name-body
551              (substring feature-name (match-beginning 1))))
552     )
553    ((string-match "^===\\([^=>]+\\)" feature-name)
554     (concat "repi."
555             (est-uri-encode-feature-name-body
556              (substring feature-name (match-beginning 1))))
557     )
558    ((string-match "^=>>\\([^=>]+\\)" feature-name)
559     (concat "g."
560             (est-uri-encode-feature-name-body
561              (substring feature-name (match-beginning 1))))
562     )
563    ((string-match "^=>>>\\([^=>]+\\)" feature-name)
564     (concat "gi."
565             (est-uri-encode-feature-name-body
566              (substring feature-name (match-beginning 1))))
567     )
568    ((string-match "^=>>\\(>+\\)" feature-name)
569     (format "gi%d.%s"
570             (length (match-string 1 feature-name))
571             (est-uri-encode-feature-name-body
572              (substring feature-name (match-end 1))))
573     )
574    ((string-match "^=>\\([^=>]+\\)" feature-name)
575     (concat "a."
576             (est-uri-encode-feature-name-body
577              (substring feature-name (match-beginning 1))))
578     )
579    ((string-match "^\\(=+\\)>" feature-name)
580     (format "a%d.%s"
581             (length (match-string 1 feature-name))
582             (est-uri-encode-feature-name-body
583              (substring feature-name (match-end 0))))
584     )
585    ((string-match "^->" feature-name)
586     (concat "to."
587             (est-uri-encode-feature-name-body
588              (substring feature-name (match-end 0))))
589     )
590    ((string-match "^<-" feature-name)
591     (concat "from."
592             (est-uri-encode-feature-name-body
593              (substring feature-name (match-end 0))))
594     )
595    (t (est-uri-encode-feature-name-body feature-name))))
596
597 (defun www-uri-make-feature-name-url (uri-genre uri-feature-name uri-object)
598   (if est-hide-cgi-mode
599       (format "../feature/%s&%s/%s"
600               uri-feature-name uri-genre uri-object)
601     (format "%s?feature=%s&%s=%s"
602             chise-wiki-view-url uri-feature-name uri-genre uri-object)))
603
604 (defun www-uri-decode-object (genre char-rep)
605   (let (ccs cpos)
606     (cond
607      ((string-match (if est-hide-cgi-mode
608                         "\\(%3D\\|=\\|%3A\\|:\\)"
609                       "\\(%3A\\|:\\)") char-rep)
610       (setq ccs (substring char-rep 0 (match-beginning 0))
611             cpos (substring char-rep (match-end 0)))
612       (setq ccs (www-uri-decode-feature-name ccs))
613       (setq cpos (est-uri-decode-feature-name-body cpos))
614       (cond
615        ((string-match "^0x" cpos)
616         (setq cpos
617               (string-to-number (substring cpos (match-end 0)) 16))
618         )
619        (t
620         (setq cpos (car (read-from-string
621                          (decode-uri-string
622                           cpos file-name-coding-system))))
623         ))
624       (if (and (eq genre 'character)
625                (numberp cpos))
626           (decode-char ccs cpos)
627         (concord-decode-object ccs cpos genre))
628       )
629      (t
630       (setq char-rep (decode-uri-string char-rep 'utf-8-mcs-er))
631       (cond
632        ((eq genre 'character)
633         (when (= (length char-rep) 1)
634           (aref char-rep 0))
635         )
636        ((eq genre 'feature)
637         (concord-decode-object
638          '=id (www-uri-decode-feature-name char-rep) 'feature)
639         )
640        (t
641         (concord-decode-object
642          '=id (car (read-from-string char-rep)) genre)
643         ))))))
644
645 (defun www-uri-encode-object (object)
646   (if (characterp object)
647       (if (encode-char object '=ucs)
648           (mapconcat
649            (lambda (byte)
650              (format "%%%02X" byte))
651            (encode-coding-string (char-to-string object) 'utf-8-mcs-er)
652            "")
653         (let ((ccs-list est-coded-charset-priority-list)
654               ccs ret)
655           (while (and ccs-list
656                       (setq ccs (pop ccs-list))
657                       (not (setq ret (encode-char object ccs 'defined-only)))))
658           (cond (ret
659                  (format (if est-hide-cgi-mode
660                              "%s=0x%X"
661                            "%s:0x%X")
662                          (www-uri-encode-feature-name ccs)
663                          ret))
664                 ((and (setq ccs (car (split-char object)))
665                       (setq ret (encode-char object ccs)))
666                  (format (if est-hide-cgi-mode
667                              "%s=0x%X"
668                            "%s:0x%X")
669                          (www-uri-encode-feature-name ccs)
670                          ret))
671                 (t
672                  (format (if est-hide-cgi-mode
673                              "system-char-id=0x%X"
674                            "system-char-id:0x%X")
675                          (encode-char object 'system-char-id))
676                  ))))
677     (format (if est-hide-cgi-mode
678                 "rep.id=%s"
679               "rep.id:%s")
680             (est-uri-encode-feature-name-body
681              (format "%s" (concord-object-id object))))))
682
683 (defun est-format-object (object &optional readable)
684   (if (characterp object)
685       (char-to-string object)
686     (let ((ret (or (if readable
687                        (or (concord-object-get object 'name)
688                            (concord-object-get object '=name)
689                            (concord-object-get object 'title)))
690                    (concord-object-id object))))
691       (format "%s" ret))))
692
693 (defun www-uri-make-object-url (object &optional uri-object)
694   (if est-hide-cgi-mode
695       (format "%s/%s/%s"
696               est-view-url-prefix
697               (est-object-genre object)
698               (or uri-object
699                   (www-uri-encode-object object)))
700     (format "%s?%s=%s"
701             chise-wiki-view-url
702             (est-object-genre object)
703             (or uri-object
704                 (www-uri-encode-object object)))))
705
706
707 ;;; @ Feature name presentation
708 ;;;
709
710 (defun www-format-feature-name-default (feature-name)
711   (mapconcat
712    #'capitalize
713    (split-string
714     (symbol-name feature-name)
715     "-")
716    " "))
717
718 (defun www-format-feature-name-as-metadata (feature-name &optional lang)
719   (let ((str (symbol-name feature-name))
720         base meta)
721     (cond
722      ((string-match "\\*[^*]+$" str)
723       (setq base (substring str 0 (match-beginning 0))
724             meta (substring str (match-beginning 0)))
725       (concat (www-format-feature-name* (intern base) lang)
726               meta))
727      (t
728       (www-format-feature-name-default feature-name)
729       ))))
730
731 (defun www-format-feature-name-as-rel-to (feature-name)
732   (concat "\u2192" (substring (symbol-name feature-name) 2)))
733
734 (defun www-format-feature-name-as-rel-from (feature-name)
735   (concat "\u2190" (substring (symbol-name feature-name) 2)))
736
737 (defun www-format-feature-name-as-CCS (feature-name)
738   (let* ((rest
739           (split-string
740            (symbol-name feature-name)
741            "-"))
742          (dest (upcase (pop rest))))
743     (when (string-match "^=+>*" dest)
744       (setq dest (concat (substring dest 0 (match-end 0))
745                          " "
746                          (substring dest (match-end 0)))))
747     (cond
748      (rest
749       (while (cdr rest)
750         (setq dest (concat dest " " (upcase (pop rest)))))
751       (if (string-match "^[0-9]+$" (car rest))
752           (concat dest "-" (car rest))
753         (concat dest " " (upcase (car rest))))
754       )
755      (t dest))))
756
757 (defun www-format-feature-name* (feature-name &optional lang)
758   (let (name fn parent ret)
759     (cond
760      ((or (and lang
761                (char-feature-property
762                 feature-name
763                 (intern (format "name@%s" lang))))
764           (char-feature-property
765            feature-name 'name)))
766      ((and (setq name (symbol-name feature-name))
767            (string-match "\\*" name))
768       (www-format-feature-name-as-metadata feature-name lang))
769      (t
770       (setq fn feature-name)
771       (while (and (setq parent (char-feature-name-parent fn))
772                   (null (setq ret
773                               (or (and lang
774                                        (char-feature-property
775                                         parent
776                                         (intern (format "name@%s" lang))))
777                                   (char-feature-property
778                                    parent 'name)))))
779         (setq fn parent))
780       (cond
781        (ret
782         (concat ret (substring (symbol-name feature-name)
783                                (length (symbol-name parent)))))
784        ((find-charset feature-name)
785         (www-format-feature-name-as-CCS feature-name))
786        ((string-match "^\\(->\\)" name)
787         (www-format-feature-name-as-rel-to feature-name))
788        ((string-match "^\\(<-\\)" name)
789         (www-format-feature-name-as-rel-from feature-name))
790        (t
791         (www-format-feature-name-default feature-name)
792         ))
793       ))))
794
795 (defun www-format-feature-name (feature-name &optional lang)
796   (www-format-encode-string
797    (www-format-feature-name* feature-name lang)))
798
799
800 ;;; @ HTML generator
801 ;;;
802
803 (defvar www-format-char-img-style "vertical-align:bottom;")
804
805 (defun www-format-encode-string (string &optional without-tags as-body)
806   (with-temp-buffer
807     (insert string)
808     (let (plane code subcode start end char variants ret rret)
809       (when as-body
810         (goto-char (point-min))
811         (while (search-forward "&" nil t)
812           (replace-match "&amp;" nil t)))
813       (goto-char (point-min))
814       (while (search-forward "<" nil t)
815         (replace-match "&lt;" nil t))
816       (goto-char (point-min))
817       (while (search-forward ">" nil t)
818         (replace-match "&gt;" nil t))
819       (if without-tags
820           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
821         (let ((coded-charset-entity-reference-alist
822                (list*
823                 '(=gt                   "GT-" 5 d)
824                 '(=mj                    "MJ" 6 d)
825                 '(=hanyo-denshi/ja   "HD-JA-" 4 X)
826                 '(=hanyo-denshi/jb   "HD-JB-" 4 X)
827                 '(=hanyo-denshi/jc   "HD-JC-" 4 X)
828                 '(=hanyo-denshi/jd   "HD-JD-" 4 X)
829                 '(=hanyo-denshi/ft   "HD-FT-" 4 X)
830                 '(=hanyo-denshi/ia   "HD-IA-" 4 X)
831                 '(=hanyo-denshi/ib   "HD-IB-" 4 X)
832                 '(=hanyo-denshi/hg   "HD-HG-" 4 X)
833                 '(=hanyo-denshi/ip   "HD-IP-" 4 X)
834                 '(=hanyo-denshi/jt   "HD-JT-" 4 X)
835                 '(=hanyo-denshi/ks   "HD-KS-" 6 d)
836                 '(=>>hanyo-denshi/ja "G-HD-JA-" 4 X)
837                 '(=>>hanyo-denshi/jb "G-HD-JB-" 4 X)
838                 '(=>>hanyo-denshi/jc "G-HD-JC-" 4 X)
839                 '(=>>hanyo-denshi/jd "G-HD-JD-" 4 X)
840                 '(=>>hanyo-denshi/ft "G-HD-FT-" 4 X)
841                 '(=>>hanyo-denshi/ia "G-HD-IA-" 4 X)
842                 '(=>>hanyo-denshi/ib "G-HD-IB-" 4 X)
843                 '(=>>hanyo-denshi/hg "G-HD-HG-" 4 X)
844                 '(=>>hanyo-denshi/ip "G-HD-IP-" 4 X)
845                 '(=>>hanyo-denshi/jt "G-HD-JT-" 4 X)
846                 '(=>>hanyo-denshi/ks "G-HD-KS-" 6 d)
847                 '(==mj                  "g2-MJ" 6 d)
848                 '(==hanyo-denshi/ja "g2-HD-JA-" 4 X)
849                 '(==hanyo-denshi/jb "g2-HD-JB-" 4 X)
850                 '(==hanyo-denshi/jc "g2-HD-JC-" 4 X)
851                 '(==hanyo-denshi/jd "g2-HD-JD-" 4 X)
852                 '(==hanyo-denshi/ft "g2-HD-FT-" 4 X)
853                 '(==hanyo-denshi/ia "g2-HD-IA-" 4 X)
854                 '(==hanyo-denshi/ib "g2-HD-IB-" 4 X)
855                 '(==hanyo-denshi/hg "g2-HD-HG-" 4 X)
856                 '(==hanyo-denshi/ip "g2-HD-IP-" 4 X)
857                 '(==hanyo-denshi/jt "g2-HD-JT-" 4 X)
858                 '(==hanyo-denshi/ks "g2-HD-KS-" 6 d)
859                 '(==daijiten          "g2-DJT-" 5 d)
860                 '(=cns11643-1           "C1-" 4 X)
861                 '(=cns11643-2           "C2-" 4 X)
862                 '(=cns11643-3           "C3-" 4 X)
863                 '(=cns11643-4           "C4-" 4 X)
864                 '(=cns11643-5           "C5-" 4 X)
865                 '(=cns11643-6           "C6-" 4 X)
866                 '(=cns11643-7           "C7-" 4 X)
867                 '(=adobe-japan1-6       "AJ1-" 5 d)
868                 '(=big5-cdp             "CDP-" 4 X)
869                 '(=>big5-cdp          "A-CDP-" 4 X)
870                 '(=gb2312               "G0-" 4 X)
871                 '(=gb12345              "G1-" 4 X)
872                 '(=jis-x0208@1990       "J90-" 4 X)
873                 '(=jis-x0212            "JSP-" 4 X)
874                 '(=cbeta                "CB" 5 d)
875                 '(=jis-x0208@1997       "J97-" 4 X)
876                 '(=jis-x0208@1978       "J78-" 4 X)
877                 '(=jis-x0208@1983       "J83-" 4 X)
878                 '(=ruimoku-v6           "RUI6-" 4 X)
879                 '(=zinbun-oracle        "ZOB-" 4 d)
880                 '(=daijiten             "DJT-" 5 d)
881                 '(=jef-china3           "JC3-" 4 X)
882                 '(=ucs@unicode          "UU+" 4 X)
883                 '(=ucs@JP/hanazono  "hanaJU+" 4 X)
884                 '(==cns11643-1        "R-C1-" 4 X)
885                 '(==cns11643-2        "R-C2-" 4 X)
886                 '(==cns11643-3        "R-C3-" 4 X)
887                 '(==cns11643-4        "R-C4-" 4 X)
888                 '(==cns11643-5        "R-C5-" 4 X)
889                 '(==cns11643-6        "R-C6-" 4 X)
890                 '(==cns11643-7        "R-C7-" 4 X)
891                 '(=hanziku-1         "HZK01-" 4 X)
892                 '(=hanziku-2         "HZK02-" 4 X)
893                 '(=hanziku-3         "HZK03-" 4 X)
894                 '(=hanziku-4         "HZK04-" 4 X)
895                 '(=hanziku-5         "HZK05-" 4 X)
896                 '(=hanziku-6         "HZK06-" 4 X)
897                 '(=hanziku-7         "HZK07-" 4 X)
898                 '(=hanziku-8         "HZK08-" 4 X)
899                 '(=hanziku-9         "HZK09-" 4 X)
900                 '(=hanziku-10        "HZK10-" 4 X)
901                 '(=hanziku-11        "HZK11-" 4 X)
902                 '(=hanziku-12        "HZK12-" 4 X)
903                 '(==>daijiten       "A2-DJT-" 5 d)
904                 '(==cbeta               "CB" 5 d)
905                 '(=big5                  "B-" 4 X)
906                 '(=daikanwa              "M-" 5 d)
907                 coded-charset-entity-reference-alist)))
908           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
909
910           (goto-char (point-min))
911           (while (re-search-forward "&\\(A-\\|G-\\|g2-\\|R-\\)?CB\\([0-9]+\\);" nil t)
912             (setq code (string-to-int (match-string 2)))
913             (replace-match
914              (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\"
915 style=\"%s\">"
916                      code
917                      chise-wiki-bitmap-glyphs-url
918                      (/ code 1000) code
919                      www-format-char-img-style)
920              t 'literal))
921
922           (goto-char (point-min))
923           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
924             (setq plane (match-string 2)
925                   code (string-to-int (match-string 3) 16))
926             (replace-match
927              (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\"
928 style=\"%s\">"
929                      plane code
930                      chise-wiki-bitmap-glyphs-url
931                      plane
932                      (- (lsh code -8) 32)
933                      (- (logand code 255) 32)
934                      www-format-char-img-style)
935              t 'literal))
936
937           (goto-char (point-min))
938           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?J0-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
939             (setq code (string-to-int (match-string 2) 16))
940             (replace-match
941              (format "<img alt=\"J0-%04X\" src=\"%s/JIS-90/%02d-%02d.gif\"
942 style=\"%s\">"
943                      code
944                      chise-wiki-bitmap-glyphs-url
945                      (- (lsh code -8) 32)
946                      (- (logand code 255) 32)
947                      www-format-char-img-style)
948              t 'literal))
949
950           (goto-char (point-min))
951           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?HD-\\(JA\\|JB\\|JC\\|JD\\|FT\\|IA\\|IB\\|HG\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
952             (setq plane (match-string 2)
953                   code (string-to-int (match-string 3) 16))
954             (replace-match
955              (format "<img alt=\"HD-%s-%04X\" src=\"%s/IVD/HanyoDenshi/%s%02d%02d.png\"
956 style=\"%s\">"
957                      plane code
958                      chise-wiki-bitmap-glyphs-url
959                      plane
960                      (- (lsh code -8) 32)
961                      (- (logand code 255) 32)
962                      www-format-char-img-style)
963              t 'literal))
964
965           (goto-char (point-min))
966           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?HD-\\(IP\\|JT\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
967             (setq plane (match-string 2)
968                   code (string-to-int (match-string 3) 16))
969             (replace-match
970              (format "<img alt=\"HD-%s-%04X\" src=\"%s/IVD/HanyoDenshi/%s%04X.png\"
971 style=\"%s\">"
972                      plane code
973                      chise-wiki-bitmap-glyphs-url
974                      plane code
975                      www-format-char-img-style)
976              t 'literal))
977
978           (goto-char (point-min))
979           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?HD-KS-\\([0-9]+\\);" nil t)
980             (setq code (string-to-int (match-string 2)))
981             (replace-match
982              (format "<img alt=\"HD-KS%06d\" src=\"%s/IVD/HanyoDenshi/KS%06d.png\"
983 style=\"vertical-align:middle\">"
984                      code
985                      chise-wiki-bitmap-glyphs-url
986                      code
987                      www-format-char-img-style)
988              t 'literal))
989
990           (goto-char (point-min))
991           (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
992             (setq plane (string-to-int (match-string 1))
993                   code (string-to-int (match-string 2) 16))
994             (replace-match
995              (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\"
996 style=\"%s\">"
997                      plane code
998                      chise-wiki-bitmap-glyphs-url
999                      plane
1000                      (- (lsh code -8) 32)
1001                      (- (logand code 255) 32)
1002                      www-format-char-img-style)
1003              t 'literal))
1004
1005           (goto-char (point-min))
1006           (while (re-search-forward "&\\(R-\\)?C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
1007             (setq plane (string-to-int (match-string 2))
1008                   code (string-to-int (match-string 3) 16))
1009             (replace-match
1010              (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\"
1011 style=\"%s\">"
1012                      plane code
1013                      chise-wiki-bitmap-glyphs-url
1014                      plane code
1015                      www-format-char-img-style)
1016              t 'literal))
1017
1018           (goto-char (point-min))
1019           (while (re-search-forward "&\\(R-\\)?JC3-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
1020             (setq code (string-to-int (match-string 2) 16))
1021             (replace-match
1022              (format "<img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">"
1023                      code code)
1024              t 'literal))
1025
1026           (goto-char (point-min))
1027           (while (re-search-forward "&\\(A-\\)?ZOB-\\([0-9]+\\);" nil t)
1028             (setq code (string-to-int (match-string 2)))
1029             (replace-match
1030              (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\"
1031 style=\"vertical-align:middle\">"
1032                      code
1033                      chise-wiki-bitmap-glyphs-url
1034                      code
1035                      www-format-char-img-style)
1036              t 'literal))
1037
1038           (goto-char (point-min))
1039           (while (re-search-forward "&\\(A2-\\|g2-\\|R-\\)?DJT-\\([0-9]+\\);" nil t)
1040             (setq code (string-to-int (match-string 2)))
1041             (replace-match
1042              (format "<img alt=\"DJT-%05d\" src=\"%s/%05d.png\"
1043 style=\"vertical-align:middle; width: auto; max-height: 60px\">"
1044                      code
1045                      chise-wiki-daijiten-bitmap-glyphs-url
1046                      code
1047                      www-format-char-img-style)
1048              t 'literal))
1049
1050           (goto-char (point-min))
1051           (while (re-search-forward "&SW-JIGUGE\\([45]?\\)-\\([0-9]+\\);" nil t)
1052             (setq subcode (match-string 1)
1053                   code (string-to-int (match-string 2)))
1054             (setq plane
1055                   (if (string= subcode "")
1056                       "5"
1057                     subcode))
1058             (replace-match
1059              (format "<div class=\"tooltip\"><img alt=\"SW-JIGUGE%s-%05d\" src=\"%s/ShuoWen/Jiguge%s/%05d.png\"
1060 style=\"vertical-align:middle; width: auto; max-height: 80px\"><span
1061 class=\"tooltiptext\">%s</span></div>"
1062                      plane code
1063                      chise-wiki-bitmap-glyphs-url
1064                      plane code
1065                      (charset-description
1066                       (if (string= subcode "")
1067                           '=shuowen-jiguge
1068                         (intern (format "===shuowen-jiguge%s" subcode)))))
1069              t 'literal))
1070
1071           (goto-char (point-min))
1072           (while (re-search-forward "&HNG\\([0-9]+\\)-\\([0-9][0-9][0-9][0-9]\\)\\([0-9]\\);" nil t)
1073             (setq plane (match-string 1)
1074                   code (string-to-int (match-string 2))
1075                   subcode (string-to-int (match-string 3)))
1076             (setq subcode
1077                   (if (eq subcode 0)
1078                       ""
1079                     (char-to-string (decode-char 'ascii (+ 96 subcode)))))
1080             (replace-match
1081              (format
1082               "<div class=\"tooltip\"><img alt=\"HNG%s-%04d%s\" src=\"%s/%s/%04d%s.png\" style=\"
1083 vertical-align:middle; width: auto; max-height: 60px\"><span
1084 class=\"tooltiptext\">%s</span></div>"
1085               plane code subcode
1086               chise-wiki-hng-bitmap-glyphs-url
1087               plane code subcode
1088               (charset-description
1089                (car (find (format "HNG%s-" plane)
1090                           coded-charset-entity-reference-alist
1091                           :test (lambda (key cell)
1092                                   (string= key (nth 1 cell))))))
1093               )
1094              t 'literal))
1095
1096           (goto-char (point-min))
1097           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\|R-\\)?AJ1-\\([0-9]+\\);" nil t)
1098             (setq code (string-to-int (match-string 2)))
1099             (replace-match
1100              (format "<img alt=\"AJ1-%05d\" src=\"%s/IVD/AdobeJapan1/CID+%d.png\"
1101 style=\"vertical-align:middle\">"
1102                      code
1103                      chise-wiki-bitmap-glyphs-url
1104                      code
1105                      www-format-char-img-style)
1106              t 'literal))
1107
1108           (goto-char (point-min))
1109           (while (re-search-forward "&\\(A-\\|o-\\|G-\\|g2-\\|R-\\)?MJ\\([0-9]+\\);" nil t)
1110             (setq code (string-to-int (match-string 2)))
1111             (replace-match
1112              (format "<img alt=\"MJ%06d\" src=\"http://mojikiban.ipa.go.jp/MJ%06d.png\"
1113 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1114                      code
1115                      code
1116                      www-format-char-img-style)
1117              t 'literal))
1118
1119           (goto-char (point-min))
1120           (while (re-search-forward "&\\(o-\\|G-\\|g2-\\)?IU[+-]\\([0-9A-F]+\\);" nil t)
1121             (setq code (string-to-int (match-string 2) 16))
1122             (replace-match
1123              (format "<img alt=\"u%04x\" src=\"http://glyphwiki.org/glyph/u%04x.100px.png\"
1124 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1125                      code
1126                      code
1127                      www-format-char-img-style)
1128              t 'literal))
1129
1130           (goto-char (point-min))
1131           (while (re-search-forward "&A-\\(comp\\|cgn\\)U[+-]\\([0-9A-F]+\\);" nil t)
1132             (setq code (string-to-int (match-string 2) 16))
1133             (replace-match
1134              (format "<img alt=\"u%04x\" src=\"http://glyphwiki.org/glyph/u%04x.100px.png\"
1135 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1136                      code
1137                      code
1138                      www-format-char-img-style)
1139              t 'literal))
1140
1141           (goto-char (point-min))
1142           (while (re-search-forward
1143                   "&\\(A-\\|g2-\\)?U-i\\([0-9]+\\)\\+\\([0-9A-F]+\\);"
1144                   nil t)
1145             (setq plane (string-to-int (match-string 2))
1146                   code (string-to-int (match-string 3) 16))
1147             (replace-match
1148              (format "<img alt=\"u%04x-itaiji-%03d\" src=\"http://glyphwiki.org/glyph/u%04x-itaiji-%03d.100px.png\"
1149 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1150                      code
1151                      plane
1152                      code
1153                      plane
1154                      www-format-char-img-style)
1155              t 'literal))
1156
1157           (goto-char (point-min))
1158           (while (re-search-forward "&A-IWDSU\\+\\([0-9A-F]+\\);" nil t)
1159             (setq code (string-to-int (match-string 1) 16))
1160             (replace-match
1161              (format "<img alt=\"A-IWDSU+%04x\" src=\"http://glyphwiki.org/glyph/u%04x.100px.png\"
1162 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1163                      code
1164                      code
1165                      www-format-char-img-style)
1166              t 'literal))
1167
1168           (goto-char (point-min))
1169           (while (re-search-forward
1170                   "&\\(A-\\)?CDP-i\\([0-9]+\\)-\\([0-9A-F]+\\);"
1171                   nil t)
1172             (setq plane (string-to-int (match-string 2))
1173                   code (string-to-int (match-string 3) 16))
1174             (replace-match
1175              (format "<img alt=\"cdp-%04x-itaiji-%03d\" src=\"http://glyphwiki.org/glyph/cdp-%04x-itaiji-%03d.100px.png\"
1176 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1177                      code
1178                      plane
1179                      code
1180                      plane
1181                      www-format-char-img-style)
1182              t 'literal))
1183
1184           (goto-char (point-min))
1185           (while (re-search-forward "&\\(g2-\\)?U-v\\([0-9]+\\)\\+\\([0-9A-F]+\\);" nil t)
1186             (setq plane (string-to-int (match-string 2))
1187                   code (string-to-int (match-string 3) 16))
1188             (replace-match
1189              (format "<img alt=\"u%04x-var-%03d\" src=\"http://glyphwiki.org/glyph/u%04x-var-%03d.100px.png\"
1190 style=\"vertical-align:middle; width: 48px; height: 48px\">"
1191                      code
1192                      plane
1193                      code
1194                      plane
1195                      www-format-char-img-style)
1196              t 'literal))
1197
1198           (goto-char (point-min))
1199           (while (re-search-forward "&\\(A-\\|G-\\|R-\\|g2-\\)?GT-\\([0-9]+\\);" nil t)
1200             (setq code (string-to-int (match-string 2)))
1201             (replace-match
1202              (format "<img alt=\"GT-%05d\" src=\"%s?char=GT-%05d\"
1203 style=\"%s\">"
1204                      code
1205                      chise-wiki-glyph-cgi-url
1206                      code
1207                      www-format-char-img-style)
1208              t 'literal))
1209
1210           (goto-char (point-min))
1211           (while (re-search-forward "&\\(A-\\|G-\\|g2-\\)?GT-K\\([0-9]+\\);" nil t)
1212             (setq code (string-to-int (match-string 2)))
1213             (replace-match
1214              (format "<img alt=\"GT-K%05d\" src=\"%s?char=GT-K%05d\"
1215 style=\"%s\">"
1216                      code
1217                      chise-wiki-glyph-cgi-url
1218                      code
1219                      www-format-char-img-style)
1220              t 'literal))
1221
1222           (goto-char (point-min))
1223           (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
1224             (setq code (string-to-int (match-string 1) 16))
1225             (replace-match
1226              (format "<img alt=\"B-%04X\" src=\"%s?char=B-%04X\"
1227 style=\"%s\">"
1228                      code
1229                      chise-wiki-glyph-cgi-url
1230                      code
1231                      www-format-char-img-style)
1232              t 'literal))
1233
1234           (goto-char (point-min))
1235           (while (re-search-forward
1236                   "&\\(A-\\|G-\\|g2-\\|R-\\)?CDP-\\([0-9A-F]+\\);" nil t)
1237             (setq code (string-to-int (match-string 2) 16))
1238             (replace-match
1239              (format "<img alt=\"CDP-%04X\" src=\"%s?char=CDP-%04X\"
1240 style=\"%s\">"
1241                      code
1242                      chise-wiki-glyph-cgi-url
1243                      code
1244                      www-format-char-img-style)
1245              t 'literal))
1246
1247           (goto-char (point-min))
1248           (while (re-search-forward
1249                   "&\\(I-\\)?HZK\\(0[1-9]\\|1[0-2]\\)-\\([0-9A-F]+\\);" nil t)
1250             (setq plane (match-string 2)
1251                   code (string-to-int (match-string 3) 16))
1252             (replace-match
1253              (format "<img alt=\"HZK%s-%04X\" src=\"%s?char=HZK%s-%04X\"
1254 style=\"%s\">"
1255                      plane
1256                      code
1257                      chise-wiki-glyph-cgi-url
1258                      plane
1259                      code
1260                      www-format-char-img-style)
1261              t 'literal))
1262
1263           (goto-char (point-min))
1264           (while (re-search-forward "&\\(A-\\|G-\\|g2-\\|R-\\)?RUI6-\\([0-9A-F]+\\);" nil t)
1265             (setq code (string-to-int (match-string 2) 16))
1266             (replace-match
1267              (format "<img alt=\"RUI6-%04X\" src=\"%s?char=RUI6-%04X\"
1268 style=\"vertical-align:middle\">"
1269                      code
1270                      chise-wiki-glyph-cgi-url
1271                      code
1272                      www-format-char-img-style)
1273              t 'literal))
1274
1275           (goto-char (point-min))
1276           (while (re-search-forward "&hanaJU\\+\\([0-9A-F]+\\);" nil t)
1277             (setq code (string-to-int (match-string 1) 16))
1278             (replace-match
1279              (format "<img alt=\"hanaJU+%04X\" src=\"%s?char=hana-JU+%04X\"
1280 style=\"vertical-align:middle\">"
1281                      code
1282                      chise-wiki-glyph-cgi-url
1283                      code
1284                      www-format-char-img-style)
1285              t 'literal))
1286
1287           (goto-char (point-min))
1288           (while (re-search-forward "&\\(A-\\|G-\\|g2-\\|R-\\)?\\(UU\\+\\|U-\\)\\([0-9A-F]+\\);" nil t)
1289             (setq code (string-to-int (match-string 3) 16))
1290             (replace-match
1291              (format "<img alt=\"UU+%04X\" src=\"http://www.unicode.org/cgi-bin/refglyph?24-%04X\"
1292 style=\"vertical-align:middle\">"
1293                      code
1294                      code
1295                      www-format-char-img-style)
1296              t 'literal))
1297
1298           (goto-char (point-min))
1299           (while (re-search-forward "&MCS-\\([0-9A-F]+\\);" nil t)
1300             (setq code (string-to-int (match-string 1) 16))
1301             (setq start (match-beginning 0)
1302                   end (match-end 0))
1303             (setq char (decode-char 'system-char-id code))
1304             (cond
1305              ((and (setq variants
1306                          (or (www-get-feature-value char '->subsumptive)
1307                              (www-get-feature-value char '->denotational)))
1308                    (progn
1309                      (if (characterp variants)
1310                          (setq variants (list variants)))
1311                      (while (and variants
1312                                  (setq ret (www-format-encode-string
1313                                             (char-to-string (car variants))))
1314                                  (string-match "&MCS-\\([0-9A-F]+\\);" ret))
1315                        (setq variants (cdr variants)))
1316                      ret))
1317               (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
1318                 (goto-char start)
1319                 (delete-region start end)
1320                 (insert ret))
1321               )
1322              ((setq ret (or (www-get-feature-value char 'ideographic-combination)
1323                             (www-get-feature-value char 'ideographic-structure)))
1324               (setq ret
1325                     (mapconcat
1326                      (lambda (ch)
1327                        (if (listp ch)
1328                            (if (characterp (setq rret (find-char ch)))
1329                                (setq ch rret)))
1330                        (if (characterp ch)
1331                            (www-format-encode-string
1332                             (char-to-string ch) without-tags)
1333                          (www-format-encode-string
1334                           (format "%S" ch) without-tags)))
1335                      ret ""))
1336               (when ret
1337                 (goto-char start)
1338                 (delete-region start end)
1339                 (insert ret))
1340               )))
1341           ))
1342       ;; (goto-char (point-min))
1343       ;; (while (search-forward "&GT-" nil t)
1344       ;;   (replace-match "&amp;GT-" t 'literal))
1345       (buffer-string))))
1346
1347 (defun www-html-display-text (text)
1348   (princ
1349    (with-temp-buffer
1350      (insert text)
1351      (goto-char (point-min))
1352      (while (search-forward "<" nil t)
1353        (replace-match "&lt;" nil t))
1354      (goto-char (point-min))
1355      (while (search-forward ">" nil t)
1356        (replace-match "&gt;" nil t))
1357      (goto-char (point-min))
1358      (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
1359        (replace-match
1360         (format "<a href=\"%s\">%s</a>"
1361                 (match-string 2)
1362                 (match-string 1))
1363         nil t))
1364      (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
1365      (goto-char (point-min))
1366      (while (search-forward "&GT-" nil t)
1367        (replace-match "&amp;GT-" nil t))
1368      (buffer-string))))
1369
1370 (defun www-html-display-paragraph (text)
1371   (princ "<p>")
1372   (www-html-display-text text)
1373   (princ "</p>\n"))
1374
1375
1376 ;;; @ for GlyphWiki
1377 ;;;
1378
1379 (defvar coded-charset-GlyphWiki-id-alist
1380   '((===adobe-japan1-0  "aj1-"  5 d nil)
1381     (===adobe-japan1-1  "aj1-"  5 d nil)
1382     (===adobe-japan1-2  "aj1-"  5 d nil)
1383     (===adobe-japan1-3  "aj1-"  5 d nil)
1384     (===adobe-japan1-4  "aj1-"  5 d nil)
1385     (===adobe-japan1-5  "aj1-"  5 d nil)
1386     (===adobe-japan1-6  "aj1-"  5 d nil)
1387     (===ucs@jis         "u"     4 x nil)
1388     (===daikanwa        "dkw-"  5 d nil)
1389     (===ucs@ks          "u"     4 x "-k")
1390     (==ucs-itaiji-005   "u"     4 x "-itaiji-005")
1391     (=ucs-var-001       "u"     4 x "-var-001")
1392     (=ucs-var-002       "u"     4 x "-var-002")
1393     (=ucs-var-003       "u"     4 x "-var-003")
1394     (=ucs-var-004       "u"     4 x "-var-004")
1395     (=ucs-itaiji-001    "u"     4 x "-itaiji-001")
1396     (=ucs-itaiji-002    "u"     4 x "-itaiji-002")
1397     (=ucs-itaiji-003    "u"     4 x "-itaiji-003")
1398     (=ucs-itaiji-084    "u"     4 x "-itaiji-084")
1399     (=>ucs-itaiji-001   "u"     4 x "-itaiji-001")
1400     (=>ucs-itaiji-006   "u"     4 x "-itaiji-006")
1401     (==adobe-japan1-0   "aj1-"  5 d nil)
1402     (==adobe-japan1-1   "aj1-"  5 d nil)
1403     (==adobe-japan1-2   "aj1-"  5 d nil)
1404     (==adobe-japan1-3   "aj1-"  5 d nil)
1405     (==adobe-japan1-4   "aj1-"  5 d nil)
1406     (==adobe-japan1-5   "aj1-"  5 d nil)
1407     (==adobe-japan1-6   "aj1-"  5 d nil)
1408     (==ucs@jis          "u"     4 x nil)
1409     (==ucs@iso          "u"     4 x nil)
1410     (==ucs@cns          "u"     4 x "-t")
1411     (==ucs@unicode      "u"     4 x "-us")
1412     (==daikanwa         "dkw-"  5 d nil)
1413     (==ucs@ks           "u"     4 x "-k")
1414     (==jis-x0208@1978   "j78-"  4 x nil)
1415     (==jis-x0208        "j90-"  4 x nil)
1416     (==jis-x0208@1990   "j90-"  4 x nil)
1417     (==jis-x0208@1983   "j83-"  4 x nil)
1418     (==cbeta           "cbeta-" 5 d nil)
1419     (=>>hanyo-denshi/ks "koseki-" 6 d nil)
1420     (=>>jis-x0208@1978  "j78-"  4 x nil)
1421     (=>>big5-cdp        "cdp-"  4 x nil)
1422     (=>>adobe-japan1-0  "aj1-"  5 d nil)
1423     (=>>adobe-japan1-1  "aj1-"  5 d nil)
1424     (=>>adobe-japan1-2  "aj1-"  5 d nil)
1425     (=>>adobe-japan1-3  "aj1-"  5 d nil)
1426     (=>>adobe-japan1-4  "aj1-"  5 d nil)
1427     (=>>adobe-japan1-5  "aj1-"  5 d nil)
1428     (=>>adobe-japan1-6  "aj1-"  5 d nil)
1429     (=>>jis-x0208       "j90-"  4 x nil)
1430     (=>>jis-x0208@1990  "j90-"  4 x nil)
1431     (=>>jis-x0208@1983  "j83-"  4 x nil)
1432     (=>>daikanwa        "dkw-"  5 d nil)
1433     (=adobe-japan1-0    "aj1-"  5 d nil)
1434     (=adobe-japan1-1    "aj1-"  5 d nil)
1435     (=adobe-japan1-2    "aj1-"  5 d nil)
1436     (=adobe-japan1-3    "aj1-"  5 d nil)
1437     (=adobe-japan1-4    "aj1-"  5 d nil)
1438     (=adobe-japan1-5    "aj1-"  5 d nil)
1439     (=adobe-japan1-6    "aj1-"  5 d nil)
1440     (=decomposition@cid)
1441     (=decomposition@hanyo-denshi)
1442     (=hanyo-denshi/ks   "koseki-" 6 d nil)
1443     (=koseki            "koseki-" 6 d nil)
1444     (=ucs@jis           "u"     4 x nil)
1445     (=ucs@cns           "u"     4 x "-t")
1446     (=ucs@ks            "u"     4 x "-k")
1447     (=ucs@JP            "u"     4 x nil)
1448     (=ucs@gb            "u"     4 x "-g")
1449     (=ucs@iso           "u"     4 x "-u")
1450     (=ucs@unicode       "u"     4 x "-us")
1451     (=big5-cdp          "cdp-"  4 x nil)
1452     (=>big5-cdp         "cdp-"  4 x nil)
1453     (=cbeta            "cbeta-" 5 d nil)
1454     (=big5-cdp-var-3    "cdp-"  4 x "-var-3")
1455     (=big5-cdp-var-5    "cdp-"  4 x "-var-5")
1456     (=big5-cdp-itaiji-001 "cdp-" 4 x "-itaiji-001")
1457     (=big5-cdp-itaiji-002 "cdp-" 4 x "-itaiji-002")
1458     (=>big5-cdp-itaiji-001 "cdp-" 4 x "-itaiji-001")
1459     (=jef-china3        "jc3-"  4 x nil)
1460     (=jis-x0212         "jsp-"  4 x nil)
1461     (=jis-x0213-1@2000  "jx1-2000-" 4 x nil)
1462     (=jis-x0213-1@2004  "jx1-2004-" 4 x nil)
1463     (=jis-x0213-2       "jx2-"  4 x nil)
1464     (=gt-k              "gt-k"  5 d nil)
1465     (=jis-x0208@1978/1pr "j78-" 4 x nil)
1466     (=jis-x0208@1978/-4pr "j78-" 4 x nil)
1467     (=jis-x0208@1978    "j78-"  4 x nil)
1468     (=+>jis-x0208@1978  "j78-"  4 x nil)
1469     (=+>jis-x0208       "j90-"  4 x nil)
1470     (=+>jis-x0208@1990  "j90-"  4 x nil)
1471     (=+>jis-x0208@1983  "j83-"  4 x nil)
1472     (=ucs               "u"     4 x nil)
1473     (=big5              "b-"    4 x nil)
1474     (=daikanwa          "dkw-"  5 d nil)
1475     (=gt                "gt-"   5 d nil)
1476     (=ruimoku-v6        "rui6-" 4 x nil)
1477     (=>ruimoku-v6       "rui6-" 4 x nil)
1478     (=ks-x1001          "k0-"   4 x nil)
1479     (=cns11643-1        "c1-"   4 x nil)
1480     (=cns11643-2        "c2-"   4 x nil)
1481     (=cns11643-3        "c3-"   4 x nil)
1482     (=cns11643-4        "c4-"   4 x nil)
1483     (=cns11643-5        "c5-"   4 x nil)
1484     (=cns11643-6        "c6-"   4 x nil)
1485     (=cns11643-7        "c7-"   4 x nil)
1486     (=jis-x0208         "j90-"  4 x nil)
1487     (=jis-x0208@1990    "j90-"  4 x nil)
1488     (=jis-x0208@1983    "j83-"  4 x nil)
1489     ))
1490
1491 (defun char-GlyphWiki-id (char)
1492   (let ((rest coded-charset-GlyphWiki-id-alist)
1493         spec ret code)
1494     (while (and rest
1495                 (setq spec (pop rest))
1496                 (null (setq ret (get-char-attribute char (car spec))))))
1497     (when ret
1498       (or
1499        (and (listp ret)
1500             (mapconcat #'char-GlyphWiki-id ret "-"))
1501        (and (memq (car spec) '(=ucs@unicode '=ucs@iso))
1502             (cond
1503              ((and (or (encode-char char '=jis-x0208@1990)
1504                        (encode-char char '=jis-x0212)
1505                        (encode-char char '=jis-x0213-1)
1506                        (encode-char char '=jis-x0213-2))
1507                    (setq code (encode-char char '=ucs@jis)))
1508               (format "u%04x" code)
1509               )
1510              ((and (or (encode-char char '=gb2312)
1511                        (encode-char char '=gb12345))
1512                    (setq code (encode-char char '=ucs@gb)))
1513               (format "u%04x-g" code)
1514               )
1515              ((and (or (encode-char char '=cns11643-1)
1516                        (encode-char char '=cns11643-2)
1517                        (encode-char char '=cns11643-3)
1518                        (encode-char char '=cns11643-4)
1519                        (encode-char char '=cns11643-5)
1520                        (encode-char char '=cns11643-6)
1521                        (encode-char char '=cns11643-7))
1522                    (setq code (encode-char char '=ucs@cns)))
1523               (format "u%04x-t" code)
1524               )
1525              ((and (encode-char char '=ks-x1001)
1526                    (setq code (encode-char char '=ucs@ks)))
1527               (format "u%04x-k" code)
1528               )))
1529        (format (format "%s%%0%d%s%s"
1530                        (nth 1 spec)
1531                        (nth 2 spec)
1532                        (nth 3 spec)
1533                        (or (nth 4 spec) ""))
1534                ret)))))
1535
1536
1537 ;;; @ End.
1538 ;;;
1539
1540 (provide 'cwiki-common)
1541
1542 ;;; cwiki-common.el ends here