1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'char-db-util)
4 (defvar chise-wiki-view-url "view.cgi")
5 (defvar chise-wiki-edit-url "edit/edit.cgi")
7 (defvar chise-wiki-bitmap-glyphs-url
8 "http://chise.zinbun.kyoto-u.ac.jp/glyphs")
10 (defvar chise-wiki-glyph-cgi-url
11 "http://chise.zinbun.kyoto-u.ac.jp/chisewiki/glyph.cgi")
13 (defun decode-uri-string (string &optional coding-system)
14 (if (> (length string) 0)
18 (mapconcat (lambda (char)
21 (char-to-string char)))
23 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
24 (setq dest (concat dest
25 (substring string i (match-beginning 0))
28 (string-to-int (match-string 1 string) 16))))
31 (concat dest (substring string i))
34 (defun www-feature-type (feature-name)
35 (or (char-feature-property feature-name 'type)
36 (let ((str (symbol-name feature-name)))
38 ((string-match "\\*note\\(@[^*]+\\)?$" str)
40 ((string-match "\\*sources\\(@[^*]+\\)?$" str)
42 ((string-match "\\*" str)
44 ((string-match "^\\(->\\|<-\\)" str)
46 ((string-match "^ideographic-structure\\(@\\|$\\)" str)
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)
60 (if (find-charset feature-name)
61 (if (and (= (charset-dimension feature-name) 2)
62 (= (charset-chars feature-name) 94))
64 " (" (decimal) ") <" (ku-ten) ">")
65 '("0x" (HEX) " (" (decimal) ")")))))
67 (defun char-feature-name-at-domain (feature-name domain)
68 (let ((name (symbol-name feature-name)))
70 ((string-match "@[^*]+$" name)
71 (intern (format "%s/%s" name domain))
74 (intern (format "%s@%s" name domain))
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)))
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))))
90 ;;; @ URI representation
93 (defun www-uri-decode-feature-name (uri-feature)
96 ((string-match "^from\\." uri-feature)
97 (intern (format "<-%s" (substring uri-feature (match-end 0))))
99 ((string-match "^to\\." uri-feature)
100 (intern (format "->%s" (substring uri-feature (match-end 0))))
102 ((string-match "^rep\\." uri-feature)
103 (intern (format "=%s" (substring uri-feature (match-end 0))))
105 ((string-match "^g\\." uri-feature)
106 (intern (format "=>>%s" (substring uri-feature (match-end 0))))
108 ((string-match "^gi\\." uri-feature)
109 (intern (format "=>>>%s" (substring uri-feature (match-end 0))))
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))
116 (substring uri-feature (match-end 0))))
118 ((string-match "^a\\." uri-feature)
119 (intern (format "=>%s" (substring uri-feature (match-end 0))))
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))
126 (substring uri-feature (match-end 0))))
128 ((and (setq feature (intern (format "=>%s" uri-feature)))
129 (find-charset feature))
131 ((and (setq feature (intern (format "=>>%s" uri-feature)))
132 (find-charset feature))
134 ((and (setq feature (intern (format "=>>>%s" uri-feature)))
135 (find-charset feature))
137 ((and (setq feature (intern (format "=%s" uri-feature)))
138 (find-charset feature))
140 (t (intern uri-feature)))))
142 (defun www-uri-encode-feature-name (feature-name)
143 (setq feature-name (symbol-name feature-name))
145 ((string-match "^=\\([^=>]+\\)" feature-name)
146 (concat "rep." (substring feature-name (match-beginning 1)))
148 ((string-match "^=>>\\([^=>]+\\)" feature-name)
149 (concat "g." (substring feature-name (match-beginning 1)))
151 ((string-match "^=>>>\\([^=>]+\\)" feature-name)
152 (concat "gi." (substring feature-name (match-beginning 1)))
154 ((string-match "^=>>\\(>+\\)" feature-name)
156 (length (match-string 1 feature-name))
157 (substring feature-name (match-end 1)))
159 ((string-match "^=>\\([^=>]+\\)" feature-name)
160 (concat "a." (substring feature-name (match-beginning 1)))
162 ((string-match "^\\(=+\\)>" feature-name)
164 (length (match-string 1 feature-name))
165 (substring feature-name (match-end 0)))
167 ((string-match "^->" feature-name)
168 (concat "to." (substring feature-name (match-end 0)))
170 ((string-match "^<-" feature-name)
171 (concat "from." (substring feature-name (match-end 0)))
175 (defun www-uri-decode-char (char-rep)
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))
183 ((string-match "^0x" cpos)
185 (string-to-number (substring cpos (match-end 0)) 16))
188 (setq cpos (string-to-number cpos))
191 (decode-char ccs cpos))
194 (setq char-rep (decode-uri-string char-rep 'utf-8-mcs-er))
195 (when (= (length char-rep) 1)
199 (defun www-uri-encode-char (char)
200 (if (encode-char char '=ucs)
203 (format "%%%02X" byte))
204 (encode-coding-string (char-to-string char) 'utf-8-mcs-er)
206 (let ((ccs-list '(; =ucs
207 =cns11643-1 =cns11643-2 =cns11643-3
208 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
210 =jis-x0208 =jis-x0208@1990
213 =jis-x0213-1@2000 =jis-x0213-1@2004
214 =jis-x0208@1983 =jis-x0208@1978
218 =>>jis-x0208 =>>jis-x0213-1
219 =>jis-x0208 =>jis-x0213-1
225 (setq ccs (pop ccs-list))
226 (not (setq ret (encode-char char ccs 'defined-only)))))
229 (www-uri-encode-feature-name ccs)
231 ((and (setq ccs (car (split-char char)))
232 (setq ret (encode-char char ccs)))
234 (www-uri-encode-feature-name ccs)
237 (format "system-char-id:0x%X"
238 (encode-char char 'system-char-id))
242 ;;; @ Feature name presentation
245 (defun www-format-feature-name-default (feature-name)
249 (symbol-name feature-name)
253 (defun www-format-feature-name-as-metadata (feature-name &optional lang)
254 (let ((str (symbol-name feature-name))
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)
263 (www-format-feature-name-default feature-name)
266 (defun www-format-feature-name-as-rel-to (feature-name)
267 (concat "\u2192" (substring (symbol-name feature-name) 2)))
269 (defun www-format-feature-name-as-rel-from (feature-name)
270 (concat "\u2190" (substring (symbol-name feature-name) 2)))
272 (defun www-format-feature-name-as-CCS (feature-name)
275 (symbol-name feature-name)
277 (dest (upcase (pop rest))))
278 (when (string-match "^=+>*" dest)
279 (setq dest (concat (substring dest 0 (match-end 0))
281 (substring dest (match-end 0)))))
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))))
292 (defun www-format-feature-name* (feature-name &optional lang)
296 (char-feature-property
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))
311 (www-format-feature-name-default feature-name)))))
313 (defun www-format-feature-name (feature-name &optional lang)
314 (www-format-encode-string
315 (www-format-feature-name* feature-name lang)))
318 ;;; @ Feature value presentation
321 (defun www-format-value-as-kuten (value)
323 (- (lsh value -8) 32)
324 (- (logand value 255) 32)))
326 (defun www-format-value-as-char-list (value &optional without-tags)
331 (www-format-encode-string
332 (format (if (characterp unit)
338 (if (characterp unit)
339 (format "<a href=\"%s?char=%s\">%s</a>"
341 (www-uri-encode-char unit)
342 (www-format-encode-string (char-to-string unit)))
343 (www-format-encode-string (format "%s" unit)))))
345 (www-format-encode-string (format "%s" value) without-tags)))
347 (defun www-format-value-as-ids (value &optional without-tags)
352 (www-format-encode-string
353 (format (if (characterp unit)
359 (if (characterp unit)
360 (format "<a href=\"%s?char=%s\">%s</a>"
362 (www-uri-encode-char unit)
363 (www-format-encode-string (char-to-string unit)))
364 (www-format-encode-string (format "%s" unit)))))
365 (ideographic-structure-to-ids value) " ")
366 (www-format-encode-string (format "%s" value) without-tags)))
368 (defun www-format-value-as-S-exp (value &optional without-tags)
369 (www-format-encode-string (format "%S" value) without-tags))
371 (defun www-format-value-as-HEX (value)
374 (www-format-value-as-S-exp value)))
376 (defun www-format-value-as-CCS-default (value)
379 (www-format-value-as-HEX value)
381 (www-format-value-as-S-exp value)))
383 (defun www-format-value-as-CCS-94x94 (value)
385 (format "0x%s [%s] (%d)"
386 (www-format-value-as-HEX value)
387 (www-format-value-as-kuten value)
389 (www-format-value-as-S-exp value)))
391 (defun www-format-value (object feature-name
392 &optional value format without-tags)
394 (setq value (www-char-feature object feature-name)))
395 (www-format-apply-value object feature-name
396 format nil value nil nil without-tags)
400 ;;; @ format evaluator
403 (defun www-format-encode-string (string &optional without-tags)
406 (let (plane code start end char variants ret)
407 (goto-char (point-min))
408 (while (search-forward "<" nil t)
409 (replace-match "<" nil t))
410 (goto-char (point-min))
411 (while (search-forward ">" nil t)
412 (replace-match ">" nil t))
414 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
415 (let ((coded-charset-entity-reference-alist
417 '(=cns11643-1 "C1-" 4 X)
418 '(=cns11643-2 "C2-" 4 X)
419 '(=cns11643-3 "C3-" 4 X)
420 '(=cns11643-4 "C4-" 4 X)
421 '(=cns11643-5 "C5-" 4 X)
422 '(=cns11643-6 "C6-" 4 X)
423 '(=cns11643-7 "C7-" 4 X)
425 '(=gb12345 "G1-" 4 X)
426 '(=jis-x0208@1990 "J90-" 4 X)
427 '(=jis-x0212 "JSP-" 4 X)
429 '(=jis-x0208@1997 "J97-" 4 X)
430 '(=jis-x0208@1978 "J78-" 4 X)
431 '(=jis-x0208@1983 "J83-" 4 X)
433 '(=zinbun-oracle "ZOB-" 4 d)
434 '(=jef-china3 "JC3-" 4 X)
435 '(=daikanwa "M-" 5 d)
436 coded-charset-entity-reference-alist)))
437 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
439 (goto-char (point-min))
440 (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
441 (setq code (string-to-int (match-string 1)))
443 (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
445 chise-wiki-bitmap-glyphs-url
449 (goto-char (point-min))
450 (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
451 (setq plane (match-string 1)
452 code (string-to-int (match-string 2) 16))
454 (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
456 chise-wiki-bitmap-glyphs-url
459 (- (logand code 255) 32))
462 (goto-char (point-min))
463 (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
464 (setq plane (string-to-int (match-string 1))
465 code (string-to-int (match-string 2) 16))
467 (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
469 chise-wiki-bitmap-glyphs-url
472 (- (logand code 255) 32))
475 (goto-char (point-min))
476 (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
477 (setq plane (string-to-int (match-string 1))
478 code (string-to-int (match-string 2) 16))
480 (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
482 chise-wiki-bitmap-glyphs-url
486 (goto-char (point-min))
487 (while (re-search-forward "&JC3-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
488 (setq code (string-to-int (match-string 1) 16))
490 (format "<img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">"
494 (goto-char (point-min))
495 (while (re-search-forward "&ZOB-\\([0-9]+\\);" nil t)
496 (setq code (string-to-int (match-string 1)))
498 (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\">"
500 chise-wiki-bitmap-glyphs-url
504 (goto-char (point-min))
505 (while (re-search-forward "&\\(G-\\)?GT-\\([0-9]+\\);" nil t)
506 (setq code (string-to-int (match-string 2)))
508 (format "<img alt=\"GT-%05d\" src=\"%s?char=GT-%05d\">"
510 chise-wiki-glyph-cgi-url
514 (goto-char (point-min))
515 (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
516 (setq code (string-to-int (match-string 1) 16))
518 (format "<img alt=\"B-%04X\" src=\"%s?char=B-%04X\">"
520 chise-wiki-glyph-cgi-url
524 (goto-char (point-min))
525 (while (re-search-forward "&CDP-\\([0-9A-F]+\\);" nil t)
526 (setq code (string-to-int (match-string 1) 16))
528 (format "<img alt=\"CDP-%04X\" src=\"%s?char=CDP-%04X\">"
530 chise-wiki-glyph-cgi-url
534 (goto-char (point-min))
535 (while (re-search-forward "&UU\\+\\([0-9A-F]+\\);" nil t)
536 (setq code (string-to-int (match-string 1) 16))
538 (format "<img alt=\"UU+%04X\" src=\"http://www.unicode.org/cgi-bin/refglyph?24-%04X\">"
543 (goto-char (point-min))
544 (while (re-search-forward "&MCS-\\([0-9A-F]+\\);" nil t)
545 (setq code (string-to-int (match-string 1) 16))
546 (setq start (match-beginning 0)
548 (setq char (decode-char 'system-char-id code))
549 (setq variants (or (www-char-feature char '->subsumptive)
550 (www-char-feature char '->denotational)))
552 (setq ret (www-format-encode-string
553 (char-to-string (car variants))))
554 (string-match "&MCS-\\([0-9A-F]+\\);" ret))
555 (setq variants (cdr variants)))
556 (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
558 (delete-region start end)
561 ;; (goto-char (point-min))
562 ;; (while (search-forward ">-" nil t)
563 ;; (replace-match "&GT-" t 'literal))
566 (defun www-format-props-to-string (props &optional format)
568 (setq format (plist-get props :format)))
570 (plist-get props :flag)
571 (if (plist-get props :zero-padding)
573 (if (plist-get props :len)
574 (format "%d" (plist-get props :len)))
576 ((eq format 'decimal) "d")
577 ((eq format 'hex) "x")
578 ((eq format 'HEX) "X")
579 ((eq format 'S-exp) "S")
582 (defun www-format-apply-value (object feature-name
584 &optional uri-char uri-feature
589 ((memq format '(decimal hex HEX))
591 (format (www-format-props-to-string props format)
593 (www-format-encode-string
597 ((eq format 'wiki-text)
599 (www-xml-format-list value)
600 (www-format-eval-list value object feature-name nil uri-char))
603 (www-format-encode-string
604 (format (www-format-props-to-string props format)
608 (www-format-value-as-kuten value))
609 ((eq format 'space-separated-char-list)
610 (www-format-value-as-char-list value without-tags))
611 ((eq format 'space-separated-ids)
612 (www-format-value-as-ids value without-tags))
614 ;; (setq format 'default)
615 (www-format-encode-string
616 (format (www-format-props-to-string props 'default)
619 (if (or without-tags (eq (plist-get props :mode) 'peek))
621 (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
622 ><input type=\"submit\" value=\"edit\" /></a>"
625 uri-char uri-feature format))))
627 (defun www-format-eval-feature-value (char
629 &optional format lang uri-char value)
631 (setq value (www-char-feature char feature-name)))
633 (setq format (www-feature-value-format feature-name)))
636 (www-format-apply-value
639 uri-char (www-uri-encode-feature-name feature-name))
642 (cond ((null (cdr format))
643 (setq format (car format))
644 (www-format-apply-value
646 (car format) (nth 1 format) value
647 uri-char (www-uri-encode-feature-name feature-name))
650 (www-format-eval-list format char feature-name lang uri-char)
653 (defun www-format-eval-unit (exp char feature-name
654 &optional lang uri-char value)
656 (setq value (www-char-feature char feature-name)))
658 (setq uri-char (www-uri-encode-char char)))
660 ((stringp exp) (www-format-encode-string exp))
664 ((memq (car exp) '(value decimal hex HEX ku-ten S-exp default))
665 (if (eq (car exp) 'value)
666 (www-format-eval-feature-value char feature-name
667 (plist-get (nth 1 exp) :format)
669 (www-format-apply-value
671 (car exp) (nth 1 exp) value
672 uri-char (www-uri-encode-feature-name feature-name)))
674 ((eq (car exp) 'name)
675 (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
677 (www-uri-encode-feature-name feature-name)
679 (www-format-feature-name feature-name lang))
681 ((eq (car exp) 'link)
686 (www-format-eval-list (plist-get (nth 1 exp) :ref)
687 char feature-name lang uri-char)
688 (www-format-eval-list (nthcdr 2 exp)
689 char feature-name lang uri-char)))
695 (www-format-eval-list (nthcdr 2 exp) char feature-name
699 (defun www-format-eval-list (format-list char feature-name
700 &optional lang uri-char)
701 (if (consp format-list)
704 (www-format-eval-unit exp char feature-name lang uri-char))
706 (www-format-eval-unit format-list char feature-name lang uri-char)))
712 (defun www-xml-format-props (props)
716 (setq key (pop props)
719 (setq key (symbol-name key)))
720 (if (eq (aref key 0) ?:)
721 (setq key (substring key 1)))
723 (format "%s %s=\"%s\""
727 (defun www-xml-format-unit (format-unit)
728 (let (name props children ret)
730 ((stringp format-unit)
733 (setq name (car format-unit)
734 props (nth 1 format-unit)
735 children (nthcdr 2 format-unit))
736 (when (eq name 'link)
737 (setq ret (plist-get props :ref))
738 (unless (stringp ret)
739 (setq props (plist-remprop (copy-list props) :ref))
741 (cons (list* 'ref nil ret)
744 (format "<%s%s>%s</%s>"
747 (www-xml-format-props props)
749 (www-xml-format-list children)
752 name (www-xml-format-props props)))
755 (format "%s" format-unit)))))
757 (defun www-xml-format-list (format-list)
758 (if (atom format-list)
759 (www-xml-format-unit format-list)
760 (mapconcat #'www-xml-format-unit
767 (defun www-html-display-text (text)
771 (goto-char (point-min))
772 (while (search-forward "<" nil t)
773 (replace-match "<" nil t))
774 (goto-char (point-min))
775 (while (search-forward ">" nil t)
776 (replace-match ">" nil t))
777 (goto-char (point-min))
778 (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
780 (format "<a href=\"%s\">%s</a>"
784 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
785 (goto-char (point-min))
786 (while (search-forward ">-" nil t)
787 (replace-match "&GT-" nil t))
790 (defun www-html-display-paragraph (text)
792 (www-html-display-text text)
795 (provide 'cwiki-common)