(www-uri-encode-char): Accept `=>zinbun-oracle'.
[chise/est.git] / cwiki-common.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'char-db-util)
3
4 (defvar chise-wiki-view-url "view.cgi")
5 (defvar chise-wiki-edit-url "edit/edit.cgi")
6
7 (defvar chise-wiki-bitmap-glyphs-url
8   "http://chise.zinbun.kyoto-u.ac.jp/glyphs")
9
10 (defvar chise-wiki-glyph-cgi-url
11   "http://chise.zinbun.kyoto-u.ac.jp/chisewiki/glyph.cgi")
12
13 (defun decode-uri-string (string &optional coding-system)
14   (if (> (length string) 0)
15       (let ((i 0)
16             dest)
17         (setq string
18               (mapconcat (lambda (char)
19                            (if (eq char ?+)
20                                " "
21                              (char-to-string char)))
22                          string ""))
23         (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
24           (setq dest (concat dest
25                              (substring string i (match-beginning 0))
26                              (char-to-string
27                               (int-char
28                                (string-to-int (match-string 1 string) 16))))
29                 i (match-end 0)))
30         (decode-coding-string
31          (concat dest (substring string i))
32          coding-system))))
33
34 (defun www-feature-type (feature-name)
35   (or (char-feature-property feature-name 'type)
36       (let ((str (symbol-name feature-name)))
37         (cond
38          ((string-match "\\*note\\(@[^*]+\\)?$" str)
39           'stext)
40          ((string-match "\\*sources\\(@[^*]+\\)?$" str)
41           'domain-list)
42          ((string-match "\\*" str)
43           nil)
44          ((string-match "^\\(->\\|<-\\)" str)
45           'relation)
46          ((string-match "^ideographic-structure\\(@\\|$\\)" str)
47           'structure)
48          ))))
49
50 (defun www-feature-value-format (feature-name)
51   (or (char-feature-property feature-name 'value-format)
52       (let ((type (www-feature-type feature-name)))
53         (cond ((eq type 'relation)
54                'space-separated-char-list)
55               ((eq type 'structure)
56                'space-separated-ids)
57               ((eq type 'stext)
58                'wiki-text)
59               ))
60       (if (find-charset feature-name)
61           (if (and (= (charset-dimension feature-name) 2)
62                    (= (charset-chars feature-name) 94))
63               '("0x" (HEX)
64                 " (" (decimal) ") <" (ku-ten) ">")
65             '("0x" (HEX) " (" (decimal) ")")))))
66
67 (defun char-feature-name-at-domain (feature-name domain)
68   (let ((name (symbol-name feature-name)))
69     (cond
70      ((string-match "@[^*]+$" name)
71       (intern (format "%s/%s" name domain))
72       )
73      (t
74       (intern (format "%s@%s" name domain))
75       ))))
76
77 (defun char-feature-name-sans-versions (feature)
78   (let ((feature-name (symbol-name feature)))
79     (if (string-match "[@/]\\$rev=latest$" feature-name)
80         (intern (substring feature-name 0 (match-beginning 0)))
81       feature)))
82
83 (defun www-char-feature (character feature)
84   (let ((latest-feature (char-feature-name-at-domain feature '$rev=latest)))
85     (mount-char-attribute-table latest-feature)
86     (or (char-feature character latest-feature)
87         (char-feature character feature))))
88
89
90 ;;; @ URI representation
91 ;;;
92
93 (defun www-uri-decode-feature-name (uri-feature)
94   (let (feature)
95     (cond
96      ((string-match "^from\\." uri-feature)
97       (intern (format "<-%s" (substring uri-feature (match-end 0))))
98       )
99      ((string-match "^to\\." uri-feature)
100       (intern (format "->%s" (substring uri-feature (match-end 0))))
101       )
102      ((string-match "^rep\\." uri-feature)
103       (intern (format "=%s" (substring uri-feature (match-end 0))))
104       )
105      ((string-match "^g\\." uri-feature)
106       (intern (format "=>>%s" (substring uri-feature (match-end 0))))
107       )
108      ((string-match "^gi\\." uri-feature)
109       (intern (format "=>>>%s" (substring uri-feature (match-end 0))))
110       )
111      ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
112       (intern (format "=>>%s%s"
113                       (make-string (string-to-int
114                                     (match-string 1 uri-feature))
115                                    ?>)
116                       (substring uri-feature (match-end 0))))
117       )
118      ((string-match "^a\\." uri-feature)
119       (intern (format "=>%s" (substring uri-feature (match-end 0))))
120       )
121      ((string-match "^a\\([0-9]+\\)\\." uri-feature)
122       (intern (format "%s>%s"
123                       (make-string (string-to-int
124                                     (match-string 1 uri-feature))
125                                    ?=)
126                       (substring uri-feature (match-end 0))))
127       )
128      ((and (setq feature (intern (format "=>%s" uri-feature)))
129            (find-charset feature))
130       feature)
131      ((and (setq feature (intern (format "=>>%s" uri-feature)))
132            (find-charset feature))
133       feature)
134      ((and (setq feature (intern (format "=>>>%s" uri-feature)))
135            (find-charset feature))
136       feature)
137      ((and (setq feature (intern (format "=%s" uri-feature)))
138            (find-charset feature))
139       feature)
140      (t (intern uri-feature)))))
141
142 (defun www-uri-encode-feature-name (feature-name)
143   (setq feature-name (symbol-name feature-name))
144   (cond
145    ((string-match "^=\\([^=>]+\\)" feature-name)
146     (concat "rep." (substring feature-name (match-beginning 1)))
147     )
148    ((string-match "^=>>\\([^=>]+\\)" feature-name)
149     (concat "g." (substring feature-name (match-beginning 1)))
150     )
151    ((string-match "^=>>>\\([^=>]+\\)" feature-name)
152     (concat "gi." (substring feature-name (match-beginning 1)))
153     )
154    ((string-match "^=>>\\(>+\\)" feature-name)
155     (format "gi%d.%s"
156             (length (match-string 1 feature-name))
157             (substring feature-name (match-end 1)))
158     )
159    ((string-match "^=>\\([^=>]+\\)" feature-name)
160     (concat "a." (substring feature-name (match-beginning 1)))
161     )
162    ((string-match "^\\(=+\\)>" feature-name)
163     (format "a%d.%s"
164             (length (match-string 1 feature-name))
165             (substring feature-name (match-end 0)))
166     )
167    ((string-match "^->" feature-name)
168     (concat "to." (substring feature-name (match-end 0)))
169     )
170    ((string-match "^<-" feature-name)
171     (concat "from." (substring feature-name (match-end 0)))
172     )
173    (t feature-name)))
174
175 (defun www-uri-decode-char (char-rep)
176   (let (ccs cpos)
177     (cond
178      ((string-match "\\(%3A\\|:\\)" char-rep)
179       (setq ccs (substring char-rep 0 (match-beginning 0))
180             cpos (substring char-rep (match-end 0)))
181       (setq ccs (www-uri-decode-feature-name ccs))
182       (cond
183        ((string-match "^0x" cpos)
184         (setq cpos
185               (string-to-number (substring cpos (match-end 0)) 16))
186         )
187        (t
188         (setq cpos (string-to-number cpos))
189         ))
190       (if (numberp cpos)
191           (decode-char ccs cpos))
192       )
193      (t
194       (setq char-rep (decode-uri-string char-rep 'utf-8-mcs-er))
195       (when (= (length char-rep) 1)
196         (aref char-rep 0))
197       ))))
198
199 (defun www-uri-encode-char (char)
200   (if (encode-char char '=ucs)
201       (mapconcat
202        (lambda (byte)
203          (format "%%%02X" byte))
204        (encode-coding-string (char-to-string char) 'utf-8-mcs-er)
205        "")
206     (let ((ccs-list '(; =ucs
207                       =cns11643-1 =cns11643-2 =cns11643-3
208                       =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
209                       =gb2312 =gb12345
210                       =jis-x0208 =jis-x0208@1990
211                       =jis-x0212
212                       =cbeta =jef-china3
213                       =jis-x0213-1@2000 =jis-x0213-1@2004
214                       =jis-x0208@1983 =jis-x0208@1978
215                       =zinbun-oracle =>zinbun-oracle
216                       =daikanwa
217                       =gt =gt-k
218                       =>>jis-x0208 =>>jis-x0213-1
219                       =>jis-x0208 =>jis-x0213-1
220                       =>>gt
221                       =big5
222                       =big5-cdp))
223           ccs ret)
224       (while (and ccs-list
225                   (setq ccs (pop ccs-list))
226                   (not (setq ret (encode-char char ccs 'defined-only)))))
227       (cond (ret
228              (format "%s:0x%X"
229                      (www-uri-encode-feature-name ccs)
230                      ret))
231             ((and (setq ccs (car (split-char char)))
232                   (setq ret (encode-char char ccs)))
233              (format "%s:0x%X"
234                      (www-uri-encode-feature-name ccs)
235                      ret))
236             (t
237              (format "system-char-id:0x%X"
238                      (encode-char char 'system-char-id))
239              )))))
240
241
242 ;;; @ Feature name presentation
243 ;;;
244
245 (defun www-format-feature-name-default (feature-name)
246   (mapconcat
247    #'capitalize
248    (split-string
249     (symbol-name feature-name)
250     "-")
251    " "))
252
253 (defun www-format-feature-name-as-metadata (feature-name &optional lang)
254   (let ((str (symbol-name feature-name))
255         base meta)
256     (cond
257      ((string-match "\\*[^*]+$" str)
258       (setq base (substring str 0 (match-beginning 0))
259             meta (substring str (match-beginning 0)))
260       (concat (www-format-feature-name* (intern base) lang)
261               meta))
262      (t
263       (www-format-feature-name-default feature-name)
264       ))))
265
266 (defun www-format-feature-name-as-rel-to (feature-name)
267   (concat "\u2192" (substring (symbol-name feature-name) 2)))
268
269 (defun www-format-feature-name-as-rel-from (feature-name)
270   (concat "\u2190" (substring (symbol-name feature-name) 2)))
271
272 (defun www-format-feature-name-as-CCS (feature-name)
273   (let* ((rest
274           (split-string
275            (symbol-name feature-name)
276            "-"))
277          (dest (upcase (pop rest))))
278     (when (string-match "^=+>*" dest)
279       (setq dest (concat (substring dest 0 (match-end 0))
280                          " "
281                          (substring dest (match-end 0)))))
282     (cond
283      (rest
284       (while (cdr rest)
285         (setq dest (concat dest " " (upcase (pop rest)))))
286       (if (string-match "^[0-9]+$" (car rest))
287           (concat dest "-" (car rest))
288         (concat dest " " (upcase (car rest))))
289       )
290      (t dest))))
291
292 (defun www-format-feature-name* (feature-name &optional lang)
293   (let (name)
294     (cond
295      ((or (and lang
296                (char-feature-property
297                 feature-name
298                 (intern (format "name@%s" lang))))
299           (char-feature-property
300            feature-name 'name)))
301      ((find-charset feature-name)
302       (www-format-feature-name-as-CCS feature-name))
303      ((and (setq name (symbol-name feature-name))
304            (string-match "\\*" name))
305       (www-format-feature-name-as-metadata feature-name lang))
306      ((string-match "^\\(->\\)" name)
307       (www-format-feature-name-as-rel-to feature-name))
308      ((string-match "^\\(<-\\)" name)
309       (www-format-feature-name-as-rel-from feature-name))
310      (t
311       (www-format-feature-name-default feature-name)))))
312
313 (defun www-format-feature-name (feature-name &optional lang)
314   (www-format-encode-string
315    (www-format-feature-name* feature-name lang)))
316
317
318 ;;; @ Feature value presentation
319 ;;;
320
321 (defun www-format-value-as-kuten (value)
322   (format "%02d-%02d"
323           (- (lsh value -8) 32)
324           (- (logand value 255) 32)))
325
326 (defun www-format-value-default (value &optional without-tags)
327   (if (listp value)
328       (mapconcat
329        (lambda (unit)
330          (www-format-encode-string
331           (format "%S" unit)
332           without-tags))
333        value " ")
334     (www-format-encode-string (format "%S" value) without-tags)))
335   
336 (defun www-format-value-as-char-list (value &optional without-tags)
337   (if (listp value)
338       (mapconcat
339        (if without-tags
340            (lambda (unit)
341              (www-format-encode-string
342               (format (if (characterp unit)
343                           "%c"
344                         "%s")
345                       unit)
346               'without-tags))
347          (lambda (unit)
348            (if (characterp unit)
349                (format "<a href=\"%s?char=%s\">%s</a>"
350                        chise-wiki-view-url
351                        (www-uri-encode-char unit)
352                        (www-format-encode-string (char-to-string unit)))
353              (www-format-encode-string (format "%s" unit)))))
354        value " ")
355     (www-format-encode-string (format "%s" value) without-tags)))
356
357 (defun www-format-value-as-domain-list (value &optional without-tags)
358   (let (name source0 source num dest rest unit start end ddest)
359     (if (listp value)
360         (if without-tags
361             (mapconcat
362              (lambda (unit)
363                (format "%s" unit))
364              value " ")
365           (setq rest value)
366           (while rest
367             (setq unit (pop rest))
368             (if (symbolp unit)
369                 (setq name (symbol-name unit)))
370             (setq dest
371                   (concat
372                    dest
373                    (cond
374                     ((string-match "^zob1968=" name)
375                      (setq source (intern (substring name 0 (match-end 0)))
376                            num (substring name (match-end 0)))
377                      (if (string-match "^\\([0-9]+\\)-\\([0-9]+\\)$" num)
378                          (setq start (string-to-number
379                                       (match-string 1 num))
380                                end (string-to-number
381                                     (match-string 2 num)))
382                        (setq start (string-to-number num)
383                              end start))
384                      (setq ddest
385                            (if (eq source source0)
386                                (format
387                                 ", <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
388                                 start start)
389                              (setq source0 source)
390                              (format
391                               " <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/\">%s</a>=<a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
392                               (www-format-encode-string "\u4EAC大人\u6587研甲\u9AA8")
393                               start start)))
394                      (setq start (1+ start))
395                      (while (<= start end)
396                        (setq ddest
397                              (concat
398                               ddest
399                               (format
400                                ", <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
401                                start start)))
402                        (setq start (1+ start)))
403                      ddest)
404                     (t
405                      (setq source unit)
406                      (if (eq source source0)
407                          ""
408                        (setq source0 source)
409                        (concat " " name))
410                      )))))
411           dest)
412       (www-format-encode-string (format "%s" value) without-tags))))
413
414 (defun www-format-value-as-ids (value &optional without-tags)
415   (if (listp value)
416       (mapconcat
417        (if without-tags
418            (lambda (unit)
419              (www-format-encode-string
420               (format (if (characterp unit)
421                           "%c"
422                         "%s")
423                       unit)
424               'without-tags))
425          (lambda (unit)
426            (if (characterp unit)
427                (format "<a href=\"%s?char=%s\">%s</a>"
428                        chise-wiki-view-url
429                        (www-uri-encode-char unit)
430                        (www-format-encode-string (char-to-string unit)))
431              (www-format-encode-string (format "%s" unit)))))
432        (ideographic-structure-to-ids value) " ")
433     (www-format-encode-string (format "%s" value) without-tags)))
434
435 (defun www-format-value-as-S-exp (value &optional without-tags)
436   (www-format-encode-string (format "%S" value) without-tags))
437
438 (defun www-format-value-as-HEX (value)
439   (if (integerp value)
440       (format "%X" value)
441     (www-format-value-as-S-exp value)))
442
443 (defun www-format-value-as-CCS-default (value)
444   (if (integerp value)
445       (format "0x%s (%d)"
446               (www-format-value-as-HEX value)
447               value)
448     (www-format-value-as-S-exp value)))
449
450 (defun www-format-value-as-CCS-94x94 (value)
451   (if (integerp value)
452       (format "0x%s [%s] (%d)"
453               (www-format-value-as-HEX value)
454               (www-format-value-as-kuten value)
455               value)
456     (www-format-value-as-S-exp value)))
457
458 (defun www-format-value-as-kangxi-radical (value)
459   (if (and (integerp value)
460            (<= 0 value)
461            (<= value 214))
462       (www-format-encode-string
463        (format "%c" (ideographic-radical value)))
464     (www-format-value-as-S-exp value)))
465
466 (defun www-format-value (object feature-name
467                                 &optional value format without-tags)
468   (unless value
469     (setq value (www-char-feature object feature-name)))
470   (www-format-apply-value object feature-name
471                           format nil value nil nil without-tags)
472   )
473
474
475 ;;; @ format evaluator
476 ;;;
477
478 (defun www-format-encode-string (string &optional without-tags)
479   (with-temp-buffer
480     (insert string)
481     (let (plane code start end char variants ret rret)
482       (goto-char (point-min))
483       (while (search-forward "<" nil t)
484         (replace-match "&lt;" nil t))
485       (goto-char (point-min))
486       (while (search-forward ">" nil t)
487         (replace-match "&gt;" nil t))
488       (if without-tags
489           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
490         (let ((coded-charset-entity-reference-alist
491                (list*
492                 '(=gt                   "GT-" 5 d)
493                 '(=cns11643-1           "C1-" 4 X)
494                 '(=cns11643-2           "C2-" 4 X)
495                 '(=cns11643-3           "C3-" 4 X)
496                 '(=cns11643-4           "C4-" 4 X)
497                 '(=cns11643-5           "C5-" 4 X)
498                 '(=cns11643-6           "C6-" 4 X)
499                 '(=cns11643-7           "C7-" 4 X)
500                 '(=gb2312               "G0-" 4 X)
501                 '(=gb12345              "G1-" 4 X)
502                 '(=jis-x0208@1990       "J90-" 4 X)
503                 '(=jis-x0212            "JSP-" 4 X)
504                 '(=cbeta                "CB" 5 d)
505                 '(=jis-x0208@1997       "J97-" 4 X)
506                 '(=jis-x0208@1978       "J78-" 4 X)
507                 '(=jis-x0208@1983       "J83-" 4 X)
508                 '(=zinbun-oracle        "ZOB-" 4 d)
509                 '(=jef-china3           "JC3-" 4 X)
510                 '(=daikanwa             "M-" 5 d)
511                 coded-charset-entity-reference-alist)))
512           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
513
514           (goto-char (point-min))
515           (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
516             (setq code (string-to-int (match-string 1)))
517             (replace-match
518              (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
519                      code
520                      chise-wiki-bitmap-glyphs-url
521                      (/ code 1000) code)
522              t 'literal))
523
524           (goto-char (point-min))
525           (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
526             (setq plane (match-string 1)
527                   code (string-to-int (match-string 2) 16))
528             (replace-match
529              (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
530                      plane code
531                      chise-wiki-bitmap-glyphs-url
532                      plane
533                      (- (lsh code -8) 32)
534                      (- (logand code 255) 32))
535              t 'literal))
536
537           (goto-char (point-min))
538           (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
539             (setq plane (string-to-int (match-string 1))
540                   code (string-to-int (match-string 2) 16))
541             (replace-match
542              (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
543                      plane code
544                      chise-wiki-bitmap-glyphs-url
545                      plane
546                      (- (lsh code -8) 32)
547                      (- (logand code 255) 32))
548              t 'literal))
549
550           (goto-char (point-min))
551           (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
552             (setq plane (string-to-int (match-string 1))
553                   code (string-to-int (match-string 2) 16))
554             (replace-match
555              (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
556                      plane code
557                      chise-wiki-bitmap-glyphs-url
558                      plane code)
559              t 'literal))
560
561           (goto-char (point-min))
562           (while (re-search-forward "&JC3-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
563             (setq code (string-to-int (match-string 1) 16))
564             (replace-match
565              (format "<img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">"
566                      code code)
567              t 'literal))
568
569           (goto-char (point-min))
570           (while (re-search-forward "&\\(A-\\)?ZOB-\\([0-9]+\\);" nil t)
571             (setq code (string-to-int (match-string 2)))
572             (replace-match
573              (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\">"
574                      code
575                      chise-wiki-bitmap-glyphs-url
576                      code)
577              t 'literal))
578
579           (goto-char (point-min))
580           (while (re-search-forward "&\\(G-\\)?GT-\\([0-9]+\\);" nil t)
581             (setq code (string-to-int (match-string 2)))
582             (replace-match
583              (format "<img alt=\"GT-%05d\" src=\"%s?char=GT-%05d\">"
584                      code
585                      chise-wiki-glyph-cgi-url
586                      code)
587              t 'literal))
588
589           (goto-char (point-min))
590           (while (re-search-forward "&\\(G-\\)?GT-K\\([0-9]+\\);" nil t)
591             (setq code (string-to-int (match-string 2)))
592             (replace-match
593              (format "<img alt=\"GT-K%05d\" src=\"%s?char=GT-K%05d\">"
594                      code
595                      chise-wiki-glyph-cgi-url
596                      code)
597              t 'literal))
598
599           (goto-char (point-min))
600           (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
601             (setq code (string-to-int (match-string 1) 16))
602             (replace-match
603              (format "<img alt=\"B-%04X\" src=\"%s?char=B-%04X\">"
604                      code
605                      chise-wiki-glyph-cgi-url
606                      code)
607              t 'literal))
608
609           (goto-char (point-min))
610           (while (re-search-forward "&CDP-\\([0-9A-F]+\\);" nil t)
611             (setq code (string-to-int (match-string 1) 16))
612             (replace-match
613              (format "<img alt=\"CDP-%04X\" src=\"%s?char=CDP-%04X\">"
614                      code
615                      chise-wiki-glyph-cgi-url
616                      code)
617              t 'literal))
618           
619           (goto-char (point-min))
620           (while (re-search-forward "&\\(UU\\+\\|U-\\)\\([0-9A-F]+\\);" nil t)
621             (setq code (string-to-int (match-string 2) 16))
622             (replace-match
623              (format "<img alt=\"UU+%04X\" src=\"http://www.unicode.org/cgi-bin/refglyph?24-%04X\">"
624                      code
625                      code)
626              t 'literal))
627
628           (goto-char (point-min))
629           (while (re-search-forward "&MCS-\\([0-9A-F]+\\);" nil t)
630             (setq code (string-to-int (match-string 1) 16))
631             (setq start (match-beginning 0)
632                   end (match-end 0))
633             (setq char (decode-char 'system-char-id code))
634             (cond
635              ((and (setq variants
636                          (or (www-char-feature char '->subsumptive)
637                              (www-char-feature char '->denotational)))
638                    (progn
639                      (while (and variants
640                                  (setq ret (www-format-encode-string
641                                             (char-to-string (car variants))))
642                                  (string-match "&MCS-\\([0-9A-F]+\\);" ret))
643                        (setq variants (cdr variants)))
644                      ret))
645               (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
646                 (goto-char start)
647                 (delete-region start end)
648                 (insert ret))
649               )
650              ((setq ret (or (www-char-feature char 'ideographic-combination)
651                             (www-char-feature char 'ideographic-structure)))
652               (setq ret
653                     (mapconcat
654                      (lambda (ch)
655                        (if (listp ch)
656                            (if (characterp (setq rret (find-char ch)))
657                                (setq ch rret)))
658                        (if (characterp ch)
659                            (www-format-encode-string
660                             (char-to-string ch) without-tags)
661                          (www-format-encode-string
662                           (format "%S" ch) without-tags)))
663                      ret ""))
664               (when ret
665                 (goto-char start)
666                 (delete-region start end)
667                 (insert ret))
668               )))
669           ))
670       ;; (goto-char (point-min))
671       ;; (while (search-forward "&GT-" nil t)
672       ;;   (replace-match "&amp;GT-" t 'literal))
673       (buffer-string))))
674
675 (defun www-format-props-to-string (props &optional format)
676   (unless format
677     (setq format (plist-get props :format)))
678   (concat "%"
679           (plist-get props :flag)
680           (if (plist-get props :zero-padding)
681               "0")
682           (if (plist-get props :len)
683               (format "%d" (plist-get props :len)))
684           (cond
685            ((eq format 'decimal) "d")
686            ((eq format 'hex) "x")
687            ((eq format 'HEX) "X")
688            ((eq format 'S-exp) "S")
689            (t "s"))))      
690
691 (defun www-format-apply-value (object feature-name
692                                       format props value
693                                       &optional uri-char uri-feature
694                                       without-tags)
695   (let (ret)
696     (setq ret
697           (cond
698            ((memq format '(decimal hex HEX))
699             (if (integerp value)
700                 (format (www-format-props-to-string props format)
701                         value)
702               (www-format-encode-string
703                (format "%s" value)
704                without-tags))
705             )
706            ((eq format 'wiki-text)
707             (if without-tags
708                 (www-xml-format-list value)
709               (www-format-eval-list value object feature-name nil uri-char))
710             )
711            ((eq format 'S-exp)
712             (www-format-encode-string
713              (format (www-format-props-to-string props format)
714                      value)
715              without-tags))
716            ((eq format 'ku-ten)
717             (www-format-value-as-kuten value))
718            ((eq format 'kangxi-radical)
719             (www-format-value-as-kangxi-radical value))
720            ((eq format 'space-separated-char-list)
721             (www-format-value-as-char-list value without-tags))
722            ((eq format 'space-separated-ids)
723             (www-format-value-as-ids value without-tags))
724            ((eq format 'space-separated-domain-list)
725             (www-format-value-as-domain-list value without-tags))
726            ((eq format 'string)
727             (www-format-encode-string (format "%s" value) without-tags)
728             )
729            (t
730             (www-format-value-default value without-tags)
731             ))
732           )
733     (if (or without-tags (eq (plist-get props :mode) 'peek))
734         ret
735       (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
736 ><input type=\"submit\" value=\"edit\" /></a>"
737               ret
738               chise-wiki-edit-url
739               uri-char uri-feature format))))
740
741 (defun www-format-eval-feature-value (char
742                                       feature-name
743                                       &optional format lang uri-char value)
744   (unless value
745     (setq value (www-char-feature char feature-name)))
746   (unless format
747     (setq format (www-feature-value-format feature-name)))
748   (cond
749    ((symbolp format)
750     (www-format-apply-value
751      char feature-name
752      format nil value
753      uri-char (www-uri-encode-feature-name feature-name))
754     )
755    ((consp format)
756     (cond ((null (cdr format))
757            (setq format (car format))
758            (www-format-apply-value
759             char feature-name
760             (car format) (nth 1 format) value
761             uri-char (www-uri-encode-feature-name feature-name))
762            )
763           (t
764            (www-format-eval-list format char feature-name lang uri-char)
765            )))))
766
767 (defun www-format-eval-unit (exp char feature-name
768                                  &optional lang uri-char value)
769   (unless value
770     (setq value (www-char-feature char feature-name)))
771   (unless uri-char
772     (setq uri-char (www-uri-encode-char char)))
773   (cond
774    ((stringp exp) (www-format-encode-string exp))
775    ((null exp) "")
776    ((consp exp)
777     (cond
778      ((memq (car exp) '(value decimal hex HEX ku-ten kangxi-radical
779                               S-exp default))
780       (if (eq (car exp) 'value)
781           (www-format-eval-feature-value char feature-name
782                                          (plist-get (nth 1 exp) :format)
783                                          lang uri-char value)
784         (www-format-apply-value
785          char feature-name
786          (car exp) (nth 1 exp) value
787          uri-char (www-uri-encode-feature-name feature-name)))
788       )
789      ((eq (car exp) 'name)
790       (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
791               chise-wiki-view-url
792               (www-uri-encode-feature-name feature-name)
793               uri-char
794               (www-format-feature-name feature-name lang))
795       )
796      ((eq (car exp) 'link)
797       (format "<a
798  href=\"%s\"
799 >%s</a
800 >"
801               (www-format-eval-list (plist-get (nth 1 exp) :ref)
802                                     char feature-name lang uri-char)
803               (www-format-eval-list (nthcdr 2 exp)
804                                     char feature-name lang uri-char)))
805      (t
806       (format "<%s
807 >%s</%s
808 >"
809               (car exp)
810               (www-format-eval-list (nthcdr 2 exp) char feature-name
811                                     lang uri-char)
812               (car exp)))))))
813
814 (defun www-format-eval-list (format-list char feature-name
815                                          &optional lang uri-char)
816   (if (consp format-list)
817       (mapconcat
818        (lambda (exp)
819          (www-format-eval-unit exp char feature-name lang uri-char))
820        format-list "")
821     (www-format-eval-unit format-list char feature-name lang uri-char)))
822
823
824 ;;; @ XML generator
825 ;;;
826
827 (defun www-xml-format-props (props)
828   (let ((dest "")
829         key val)
830     (while props
831       (setq key (pop props)
832             val (pop props))
833       (if (symbolp key)
834           (setq key (symbol-name key)))
835       (if (eq (aref key 0) ?:)
836           (setq key (substring key 1)))
837       (setq dest
838             (format "%s %s=\"%s\""
839                     dest key
840                     (www-format-encode-string
841                      (format "%s" val) 'without-tags))))
842     dest))
843
844 (defun www-xml-format-unit (format-unit)
845   (let (name props children ret)
846     (cond
847      ((stringp format-unit)
848       (mapconcat (lambda (c)
849                    (cond
850                     ((eq c ?&) "&amp;")
851                     ;; ((eq c ?<) "&amp;lt;")
852                     ;; ((eq c ?>) "&amp;gt;")
853                     (t
854                      (char-to-string c))))
855                  (www-format-encode-string format-unit 'without-tags)
856                  "")
857       )
858      ((consp format-unit)
859       (setq name (car format-unit)
860             props (nth 1 format-unit)
861             children (nthcdr 2 format-unit))
862       (when (eq name 'link)
863         (setq ret (plist-get props :ref))
864         (unless (stringp ret)
865           (setq props (plist-remprop (copy-list props) :ref))
866           (setq children
867                 (cons (list* 'ref nil ret)
868                       children))))
869       (if children
870           (format "<%s%s>%s</%s>"
871                   name
872                   (if props
873                       (www-xml-format-props props)
874                     "")
875                   (www-xml-format-list children)
876                   name)
877         (format "<%s%s/>"
878                 name (www-xml-format-props props)))
879       )
880      (t
881       (format "%s" format-unit)))))
882
883 (defun www-xml-format-list (format-list)
884   (if (atom format-list)
885       (www-xml-format-unit format-list)
886     (mapconcat #'www-xml-format-unit
887                format-list "")))
888
889
890 ;;; @ HTML generator
891 ;;;
892
893 (defun www-html-display-text (text)
894   (princ
895    (with-temp-buffer
896      (insert text)
897      (goto-char (point-min))
898      (while (search-forward "<" nil t)
899        (replace-match "&lt;" nil t))
900      (goto-char (point-min))
901      (while (search-forward ">" nil t)
902        (replace-match "&gt;" nil t))
903      (goto-char (point-min))
904      (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
905        (replace-match
906         (format "<a href=\"%s\">%s</a>"
907                 (match-string 2)
908                 (match-string 1))
909         nil t))
910      (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
911      (goto-char (point-min))
912      (while (search-forward "&GT-" nil t)
913        (replace-match "&amp;GT-" nil t))
914      (buffer-string))))
915
916 (defun www-html-display-paragraph (text)
917   (princ "<p>")
918   (www-html-display-text text)
919   (princ "</p>\n"))
920
921
922 ;;; @ for GlyphWiki
923 ;;;
924
925 (defvar coded-charset-GlyphWiki-id-alist
926   '((=ucs               "u"     4 x nil)
927     (=ucs@JP            "u"     4 x nil)
928     (=ucs@jis           "u"     4 x nil)
929     (=ucs@gb            "u"     4 x "-g")
930     (=ucs@cns           "u"     4 x "-t")
931     (=ucs@ks            "u"     4 x "-k")
932     (=ucs@iso           "u"     4 x "-u")
933     (=ucs@unicode       "u"     4 x "-us")
934     (=adobe-japan1-6    "aj1-"  5 d nil)
935     (=gt                "gt-"   5 d nil)
936     (=big5-cdp          "cdp-"  4 x nil)
937     (=cbeta             "cb"    5 d nil)
938     (=jis-x0208@1978/1pr "j78-" 4 x nil)
939     (=jis-x0208@1978/-4pr "j78-" 4 x nil)
940     (=jis-x0208@1978    "j78-"  4 x nil)
941     (=jis-x0208@1983    "j83-"  4 x nil)
942     (=jis-x0208@1990    "j90-"  4 x nil)
943     (=jis-x0212         "jsp-"  4 x nil)
944     (=jis-x0213-1@2000  "jx1-2000-" 4 x nil)
945     (=jis-x0213-1@2004  "jx1-2004-" 4 x nil)
946     (=jis-x0213-2       "jx2-"  4 x nil)
947     (=cns11643-1        "c1-"   4 x nil)
948     (=cns11643-2        "c2-"   4 x nil)
949     (=cns11643-3        "c3-"   4 x nil)
950     (=cns11643-4        "c4-"   4 x nil)
951     (=cns11643-5        "c5-"   4 x nil)
952     (=cns11643-6        "c6-"   4 x nil)
953     (=cns11643-7        "c7-"   4 x nil)
954     (=daikanwa          "dkw-"  5 d nil)
955     (=gt-k              "gt-k"  5 d nil)
956     (=jef-china3        "jc3-"  4 x nil)
957     (=big5              "b-"    4 x nil)
958     (=ks-x1001          "k0-"   4 x nil)
959     ))
960
961 (defun char-GlyphWiki-id (char)
962   (let ((rest coded-charset-GlyphWiki-id-alist)
963         spec ret code)
964     (while (and rest
965                 (setq spec (pop rest))
966                 (null (setq ret (char-feature char (car spec))))))
967     (when ret
968       (or
969        (and (memq (car spec) '(=ucs@unicode '=ucs@iso))
970             (cond
971              ((and (or (encode-char char '=jis-x0208@1990)
972                        (encode-char char '=jis-x0212)
973                        (encode-char char '=jis-x0213-1))
974                    (setq code (encode-char char '=ucs@jis)))
975               (format "u%04x" code)
976               )
977              ((and (or (encode-char char '=gb2312)
978                        (encode-char char '=gb12345))
979                    (setq code (encode-char char '=ucs@gb)))
980               (format "u%04x-g" code)
981               )
982              ((and (or (encode-char char '=cns11643-1)
983                        (encode-char char '=cns11643-2)
984                        (encode-char char '=cns11643-3)
985                        (encode-char char '=cns11643-4)
986                        (encode-char char '=cns11643-5)
987                        (encode-char char '=cns11643-6)
988                        (encode-char char '=cns11643-7))
989                    (setq code (encode-char char '=ucs@cns)))
990               (format "u%04x-t" code)
991               )
992              ((and (encode-char char '=ks-x1001)
993                    (setq code (encode-char char '=ucs@ks)))
994               (format "u%04x-k" code)
995               )))
996        (format (format "%s%%0%d%s%s"
997                        (nth 1 spec)
998                        (nth 2 spec)
999                        (nth 3 spec)
1000                        (or (nth 4 spec) ""))
1001                ret)))))
1002
1003
1004 ;;; @ End.
1005 ;;;
1006
1007 (provide 'cwiki-common)
1008
1009 ;;; cwiki-common.el ends here