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