(coded-charset-GlyphWiki-id-alist): New variable.
[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
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-ids (value &optional without-tags)
358   (if (listp value)
359       (mapconcat
360        (if without-tags
361            (lambda (unit)
362              (www-format-encode-string
363               (format (if (characterp unit)
364                           "%c"
365                         "%s")
366                       unit)
367               'without-tags))
368          (lambda (unit)
369            (if (characterp unit)
370                (format "<a href=\"%s?char=%s\">%s</a>"
371                        chise-wiki-view-url
372                        (www-uri-encode-char unit)
373                        (www-format-encode-string (char-to-string unit)))
374              (www-format-encode-string (format "%s" unit)))))
375        (ideographic-structure-to-ids value) " ")
376     (www-format-encode-string (format "%s" value) without-tags)))
377
378 (defun www-format-value-as-S-exp (value &optional without-tags)
379   (www-format-encode-string (format "%S" value) without-tags))
380
381 (defun www-format-value-as-HEX (value)
382   (if (integerp value)
383       (format "%X" value)
384     (www-format-value-as-S-exp value)))
385
386 (defun www-format-value-as-CCS-default (value)
387   (if (integerp value)
388       (format "0x%s (%d)"
389               (www-format-value-as-HEX value)
390               value)
391     (www-format-value-as-S-exp value)))
392
393 (defun www-format-value-as-CCS-94x94 (value)
394   (if (integerp value)
395       (format "0x%s [%s] (%d)"
396               (www-format-value-as-HEX value)
397               (www-format-value-as-kuten value)
398               value)
399     (www-format-value-as-S-exp value)))
400
401 (defun www-format-value-as-kangxi-radical (value)
402   (if (and (integerp value)
403            (<= 0 value)
404            (<= value 214))
405       (www-format-encode-string
406        (format "%c" (ideographic-radical value)))
407     (www-format-value-as-S-exp value)))
408
409 (defun www-format-value (object feature-name
410                                 &optional value format without-tags)
411   (unless value
412     (setq value (www-char-feature object feature-name)))
413   (www-format-apply-value object feature-name
414                           format nil value nil nil without-tags)
415   )
416
417
418 ;;; @ format evaluator
419 ;;;
420
421 (defun www-format-encode-string (string &optional without-tags)
422   (with-temp-buffer
423     (insert string)
424     (let (plane code start end char variants ret)
425       (goto-char (point-min))
426       (while (search-forward "<" nil t)
427         (replace-match "&lt;" nil t))
428       (goto-char (point-min))
429       (while (search-forward ">" nil t)
430         (replace-match "&gt;" nil t))
431       (if without-tags
432           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
433         (let ((coded-charset-entity-reference-alist
434                (list*
435                 '(=gt                   "GT-" 5 d)
436                 '(=cns11643-1           "C1-" 4 X)
437                 '(=cns11643-2           "C2-" 4 X)
438                 '(=cns11643-3           "C3-" 4 X)
439                 '(=cns11643-4           "C4-" 4 X)
440                 '(=cns11643-5           "C5-" 4 X)
441                 '(=cns11643-6           "C6-" 4 X)
442                 '(=cns11643-7           "C7-" 4 X)
443                 '(=gb2312               "G0-" 4 X)
444                 '(=gb12345              "G1-" 4 X)
445                 '(=jis-x0208@1990       "J90-" 4 X)
446                 '(=jis-x0212            "JSP-" 4 X)
447                 '(=cbeta                "CB" 5 d)
448                 '(=jis-x0208@1997       "J97-" 4 X)
449                 '(=jis-x0208@1978       "J78-" 4 X)
450                 '(=jis-x0208@1983       "J83-" 4 X)
451                 '(=zinbun-oracle        "ZOB-" 4 d)
452                 '(=jef-china3           "JC3-" 4 X)
453                 '(=daikanwa             "M-" 5 d)
454                 coded-charset-entity-reference-alist)))
455           (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
456
457           (goto-char (point-min))
458           (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
459             (setq code (string-to-int (match-string 1)))
460             (replace-match
461              (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
462                      code
463                      chise-wiki-bitmap-glyphs-url
464                      (/ code 1000) code)
465              t 'literal))
466
467           (goto-char (point-min))
468           (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
469             (setq plane (match-string 1)
470                   code (string-to-int (match-string 2) 16))
471             (replace-match
472              (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
473                      plane code
474                      chise-wiki-bitmap-glyphs-url
475                      plane
476                      (- (lsh code -8) 32)
477                      (- (logand code 255) 32))
478              t 'literal))
479
480           (goto-char (point-min))
481           (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
482             (setq plane (string-to-int (match-string 1))
483                   code (string-to-int (match-string 2) 16))
484             (replace-match
485              (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
486                      plane code
487                      chise-wiki-bitmap-glyphs-url
488                      plane
489                      (- (lsh code -8) 32)
490                      (- (logand code 255) 32))
491              t 'literal))
492
493           (goto-char (point-min))
494           (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
495             (setq plane (string-to-int (match-string 1))
496                   code (string-to-int (match-string 2) 16))
497             (replace-match
498              (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
499                      plane code
500                      chise-wiki-bitmap-glyphs-url
501                      plane code)
502              t 'literal))
503
504           (goto-char (point-min))
505           (while (re-search-forward "&JC3-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
506             (setq code (string-to-int (match-string 1) 16))
507             (replace-match
508              (format "<img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">"
509                      code code)
510              t 'literal))
511
512           (goto-char (point-min))
513           (while (re-search-forward "&ZOB-\\([0-9]+\\);" nil t)
514             (setq code (string-to-int (match-string 1)))
515             (replace-match
516              (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\">"
517                      code
518                      chise-wiki-bitmap-glyphs-url
519                      code)
520              t 'literal))
521
522           (goto-char (point-min))
523           (while (re-search-forward "&\\(G-\\)?GT-\\([0-9]+\\);" nil t)
524             (setq code (string-to-int (match-string 2)))
525             (replace-match
526              (format "<img alt=\"GT-%05d\" src=\"%s?char=GT-%05d\">"
527                      code
528                      chise-wiki-glyph-cgi-url
529                      code)
530              t 'literal))
531
532           (goto-char (point-min))
533           (while (re-search-forward "&\\(G-\\)?GT-K\\([0-9]+\\);" nil t)
534             (setq code (string-to-int (match-string 2)))
535             (replace-match
536              (format "<img alt=\"GT-K%05d\" src=\"%s?char=GT-K%05d\">"
537                      code
538                      chise-wiki-glyph-cgi-url
539                      code)
540              t 'literal))
541
542           (goto-char (point-min))
543           (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
544             (setq code (string-to-int (match-string 1) 16))
545             (replace-match
546              (format "<img alt=\"B-%04X\" src=\"%s?char=B-%04X\">"
547                      code
548                      chise-wiki-glyph-cgi-url
549                      code)
550              t 'literal))
551
552           (goto-char (point-min))
553           (while (re-search-forward "&CDP-\\([0-9A-F]+\\);" nil t)
554             (setq code (string-to-int (match-string 1) 16))
555             (replace-match
556              (format "<img alt=\"CDP-%04X\" src=\"%s?char=CDP-%04X\">"
557                      code
558                      chise-wiki-glyph-cgi-url
559                      code)
560              t 'literal))
561           
562           (goto-char (point-min))
563           (while (re-search-forward "&\\(UU\\+\\|U-\\)\\([0-9A-F]+\\);" nil t)
564             (setq code (string-to-int (match-string 2) 16))
565             (replace-match
566              (format "<img alt=\"UU+%04X\" src=\"http://www.unicode.org/cgi-bin/refglyph?24-%04X\">"
567                      code
568                      code)
569              t 'literal))
570
571           (goto-char (point-min))
572           (while (re-search-forward "&MCS-\\([0-9A-F]+\\);" nil t)
573             (setq code (string-to-int (match-string 1) 16))
574             (setq start (match-beginning 0)
575                   end (match-end 0))
576             (setq char (decode-char 'system-char-id code))
577             (setq variants (or (www-char-feature char '->subsumptive)
578                                (www-char-feature char '->denotational)))
579             (while (and variants
580                         (setq ret (www-format-encode-string
581                                    (char-to-string (car variants))))
582                         (string-match "&MCS-\\([0-9A-F]+\\);" ret))
583               (setq variants (cdr variants)))
584             (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
585               (goto-char start)
586               (delete-region start end)
587               (insert ret)))
588           ))
589       ;; (goto-char (point-min))
590       ;; (while (search-forward "&GT-" nil t)
591       ;;   (replace-match "&amp;GT-" t 'literal))
592       (buffer-string))))
593
594 (defun www-format-props-to-string (props &optional format)
595   (unless format
596     (setq format (plist-get props :format)))
597   (concat "%"
598           (plist-get props :flag)
599           (if (plist-get props :zero-padding)
600               "0")
601           (if (plist-get props :len)
602               (format "%d" (plist-get props :len)))
603           (cond
604            ((eq format 'decimal) "d")
605            ((eq format 'hex) "x")
606            ((eq format 'HEX) "X")
607            ((eq format 'S-exp) "S")
608            (t "s"))))      
609
610 (defun www-format-apply-value (object feature-name
611                                       format props value
612                                       &optional uri-char uri-feature
613                                       without-tags)
614   (let (ret)
615     (setq ret
616           (cond
617            ((memq format '(decimal hex HEX))
618             (if (integerp value)
619                 (format (www-format-props-to-string props format)
620                         value)
621               (www-format-encode-string
622                (format "%s" value)
623                without-tags))
624             )
625            ((eq format 'wiki-text)
626             (if without-tags
627                 (www-xml-format-list value)
628               (www-format-eval-list value object feature-name nil uri-char))
629             )
630            ((eq format 'S-exp)
631             (www-format-encode-string
632              (format (www-format-props-to-string props format)
633                      value)
634              without-tags))
635            ((eq format 'ku-ten)
636             (www-format-value-as-kuten value))
637            ((eq format 'kangxi-radical)
638             (www-format-value-as-kangxi-radical value))
639            ((eq format 'space-separated-char-list)
640             (www-format-value-as-char-list value without-tags))
641            ((eq format 'space-separated-ids)
642             (www-format-value-as-ids value without-tags))
643            ((eq format 'string)
644             (www-format-encode-string (format "%s" value) without-tags)
645             )
646            (t
647             (www-format-value-default value without-tags)
648             ))
649           )
650     (if (or without-tags (eq (plist-get props :mode) 'peek))
651         ret
652       (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
653 ><input type=\"submit\" value=\"edit\" /></a>"
654               ret
655               chise-wiki-edit-url
656               uri-char uri-feature format))))
657
658 (defun www-format-eval-feature-value (char
659                                       feature-name
660                                       &optional format lang uri-char value)
661   (unless value
662     (setq value (www-char-feature char feature-name)))
663   (unless format
664     (setq format (www-feature-value-format feature-name)))
665   (cond
666    ((symbolp format)
667     (www-format-apply-value
668      char feature-name
669      format nil value
670      uri-char (www-uri-encode-feature-name feature-name))
671     )
672    ((consp format)
673     (cond ((null (cdr format))
674            (setq format (car format))
675            (www-format-apply-value
676             char feature-name
677             (car format) (nth 1 format) value
678             uri-char (www-uri-encode-feature-name feature-name))
679            )
680           (t
681            (www-format-eval-list format char feature-name lang uri-char)
682            )))))
683
684 (defun www-format-eval-unit (exp char feature-name
685                                  &optional lang uri-char value)
686   (unless value
687     (setq value (www-char-feature char feature-name)))
688   (unless uri-char
689     (setq uri-char (www-uri-encode-char char)))
690   (cond
691    ((stringp exp) (www-format-encode-string exp))
692    ((null exp) "")
693    ((consp exp)
694     (cond
695      ((memq (car exp) '(value decimal hex HEX ku-ten kangxi-radical
696                               S-exp default))
697       (if (eq (car exp) 'value)
698           (www-format-eval-feature-value char feature-name
699                                          (plist-get (nth 1 exp) :format)
700                                          lang uri-char value)
701         (www-format-apply-value
702          char feature-name
703          (car exp) (nth 1 exp) value
704          uri-char (www-uri-encode-feature-name feature-name)))
705       )
706      ((eq (car exp) 'name)
707       (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
708               chise-wiki-view-url
709               (www-uri-encode-feature-name feature-name)
710               uri-char
711               (www-format-feature-name feature-name lang))
712       )
713      ((eq (car exp) 'link)
714       (format "<a
715  href=\"%s\"
716 >%s</a
717 >"
718               (www-format-eval-list (plist-get (nth 1 exp) :ref)
719                                     char feature-name lang uri-char)
720               (www-format-eval-list (nthcdr 2 exp)
721                                     char feature-name lang uri-char)))
722      (t
723       (format "<%s
724 >%s</%s
725 >"
726               (car exp)
727               (www-format-eval-list (nthcdr 2 exp) char feature-name
728                                     lang uri-char)
729               (car exp)))))))
730
731 (defun www-format-eval-list (format-list char feature-name
732                                          &optional lang uri-char)
733   (if (consp format-list)
734       (mapconcat
735        (lambda (exp)
736          (www-format-eval-unit exp char feature-name lang uri-char))
737        format-list "")
738     (www-format-eval-unit format-list char feature-name lang uri-char)))
739
740
741 ;;; @ XML generator
742 ;;;
743
744 (defun www-xml-format-props (props)
745   (let ((dest "")
746         key val)
747     (while props
748       (setq key (pop props)
749             val (pop props))
750       (if (symbolp key)
751           (setq key (symbol-name key)))
752       (if (eq (aref key 0) ?:)
753           (setq key (substring key 1)))
754       (setq dest
755             (format "%s %s=\"%s\""
756                     dest key
757                     (www-format-encode-string
758                      (format "%s" val) 'without-tags))))
759     dest))
760
761 (defun www-xml-format-unit (format-unit)
762   (let (name props children ret)
763     (cond
764      ((stringp format-unit)
765       (mapconcat (lambda (c)
766                    (cond
767                     ((eq c ?&) "&amp;")
768                     ;; ((eq c ?<) "&amp;lt;")
769                     ;; ((eq c ?>) "&amp;gt;")
770                     (t
771                      (char-to-string c))))
772                  (www-format-encode-string format-unit 'without-tags)
773                  "")
774       )
775      ((consp format-unit)
776       (setq name (car format-unit)
777             props (nth 1 format-unit)
778             children (nthcdr 2 format-unit))
779       (when (eq name 'link)
780         (setq ret (plist-get props :ref))
781         (unless (stringp ret)
782           (setq props (plist-remprop (copy-list props) :ref))
783           (setq children
784                 (cons (list* 'ref nil ret)
785                       children))))
786       (if children
787           (format "<%s%s>%s</%s>"
788                   name
789                   (if props
790                       (www-xml-format-props props)
791                     "")
792                   (www-xml-format-list children)
793                   name)
794         (format "<%s%s/>"
795                 name (www-xml-format-props props)))
796       )
797      (t
798       (format "%s" format-unit)))))
799
800 (defun www-xml-format-list (format-list)
801   (if (atom format-list)
802       (www-xml-format-unit format-list)
803     (mapconcat #'www-xml-format-unit
804                format-list "")))
805
806
807 ;;; @ HTML generator
808 ;;;
809
810 (defun www-html-display-text (text)
811   (princ
812    (with-temp-buffer
813      (insert text)
814      (goto-char (point-min))
815      (while (search-forward "<" nil t)
816        (replace-match "&lt;" nil t))
817      (goto-char (point-min))
818      (while (search-forward ">" nil t)
819        (replace-match "&gt;" nil t))
820      (goto-char (point-min))
821      (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
822        (replace-match
823         (format "<a href=\"%s\">%s</a>"
824                 (match-string 2)
825                 (match-string 1))
826         nil t))
827      (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
828      (goto-char (point-min))
829      (while (search-forward "&GT-" nil t)
830        (replace-match "&amp;GT-" nil t))
831      (buffer-string))))
832
833 (defun www-html-display-paragraph (text)
834   (princ "<p>")
835   (www-html-display-text text)
836   (princ "</p>\n"))
837
838
839 ;;; @ for GlyphWiki
840 ;;;
841
842 (defvar coded-charset-GlyphWiki-id-alist
843   '((=ucs               "u"     4 x nil)
844     (=ucs@iso           "u"     4 x "-u")
845     (=ucs@unicode       "u"     4 x "-us")
846     (=ucs@JP            "u"     4 x nil)
847     (=ucs@gb            "u"     4 x "-g")
848     (=ucs@cns           "u"     4 x "-t")
849     (=ucs@ks            "u"     4 x "-k")
850     (=adobe-japan1-6    "aj1-"  5 d nil)
851     (=gt                "gt-"   5 d nil)
852     (=gt-k              "gt-k"  5 d nil)
853     (=big5-cdp          "cdp-"  4 x nil)
854     (=cbeta             "cb"    5 d nil)
855     (=jis-x0208@1978/1pr "j78-" 4 x nil)
856     (=jis-x0208@1983    "j83-"  4 x nil)
857     (=jis-x0208@1990    "j90-"  4 x nil)
858     (=jis-x0212         "jsp-"  4 x nil)
859     (=jis-x0213-1@2000  "jx1-2000-" 4 x nil)
860     (=jis-x0213-1@2004  "jx1-2004-" 4 x nil)
861     (=jis-x0213-2       "jx2-"  4 x nil)
862     (=cns11643-1        "c1-"   4 x nil)
863     (=cns11643-2        "c2-"   4 x nil)
864     (=cns11643-3        "c3-"   4 x nil)
865     (=cns11643-4        "c4-"   4 x nil)
866     (=cns11643-5        "c5-"   4 x nil)
867     (=cns11643-6        "c6-"   4 x nil)
868     (=cns11643-7        "c7-"   4 x nil)
869     (=daikanwa          "dkw-"  5 d nil)
870     (=jef-china3        "jc3-"  4 x nil)
871     (=big5              "b-"    4 x nil)
872     (=ks-x1001          "k0-"   4 x nil)
873     ))
874
875 (defun char-GlyphWiki-id (char)
876   (let ((rest coded-charset-GlyphWiki-id-alist)
877         spec
878         ret)
879     (while (and rest
880                 (setq spec (pop rest))
881                 (null (setq ret (encode-char char (car spec))))))
882     (when ret
883       (format (format "%s%%0%d%s%s"
884                       (nth 1 spec)
885                       (nth 2 spec)
886                       (nth 3 spec)
887                       (or (nth 4 spec) ""))
888               ret))))
889
890
891 ;;; @ End.
892 ;;;
893
894 (provide 'cwiki-common)
895
896 ;;; cwiki-common.el ends here