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