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