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 "^\\(->\\|<-\\)" str)
40 ((string-match "^ideographic-structure\\(@\\|$\\)" str)
44 (defun www-feature-value-format (feature-name)
45 (or (char-feature-property feature-name 'value-format)
46 (let ((type (www-feature-type feature-name)))
47 (cond ((eq type 'relation)
48 'space-separated-char-list)
50 'space-separated-ids)))
51 (if (find-charset feature-name)
52 (if (and (= (charset-dimension feature-name) 2)
53 (= (charset-chars feature-name) 94))
55 " (" (decimal) ") <" (ku-ten) ">")
56 '("0x" (HEX) " (" (decimal) ")")))))
59 ;;; @ URI representation
62 (defun www-uri-decode-feature-name (uri-feature)
65 ((string-match "^from\\." uri-feature)
66 (intern (format "<-%s" (substring uri-feature (match-end 0))))
68 ((string-match "^to\\." uri-feature)
69 (intern (format "->%s" (substring uri-feature (match-end 0))))
71 ((string-match "^rep\\." uri-feature)
72 (intern (format "=%s" (substring uri-feature (match-end 0))))
74 ((string-match "^g\\." uri-feature)
75 (intern (format "=>>%s" (substring uri-feature (match-end 0))))
77 ((string-match "^gi\\." uri-feature)
78 (intern (format "=>>>%s" (substring uri-feature (match-end 0))))
80 ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
81 (intern (format "=>>%s%s"
82 (make-string (string-to-int
83 (match-string 1 uri-feature))
85 (substring uri-feature (match-end 0))))
87 ((string-match "^a\\." uri-feature)
88 (intern (format "=>%s" (substring uri-feature (match-end 0))))
90 ((string-match "^a\\([0-9]+\\)\\." uri-feature)
91 (intern (format "%s>%s"
92 (make-string (string-to-int
93 (match-string 1 uri-feature))
95 (substring uri-feature (match-end 0))))
97 ((and (setq feature (intern (format "=>%s" uri-feature)))
98 (find-charset feature))
100 ((and (setq feature (intern (format "=>>%s" uri-feature)))
101 (find-charset feature))
103 ((and (setq feature (intern (format "=>>>%s" uri-feature)))
104 (find-charset feature))
106 ((and (setq feature (intern (format "=%s" uri-feature)))
107 (find-charset feature))
109 (t (intern uri-feature)))))
111 (defun www-uri-encode-feature-name (feature-name)
112 (setq feature-name (symbol-name feature-name))
114 ((string-match "^=\\([^=>]+\\)" feature-name)
115 (concat "rep." (substring feature-name (match-beginning 1)))
117 ((string-match "^=>>\\([^=>]+\\)" feature-name)
118 (concat "g." (substring feature-name (match-beginning 1)))
120 ((string-match "^=>>>\\([^=>]+\\)" feature-name)
121 (concat "gi." (substring feature-name (match-beginning 1)))
123 ((string-match "^=>>\\(>+\\)" feature-name)
125 (length (match-string 1 feature-name))
126 (substring feature-name (match-end 1)))
128 ((string-match "^=>\\([^=>]+\\)" feature-name)
129 (concat "a." (substring feature-name (match-beginning 1)))
131 ((string-match "^\\(=+\\)>" feature-name)
133 (length (match-string 1 feature-name))
134 (substring feature-name (match-end 0)))
136 ((string-match "^->" feature-name)
137 (concat "to." (substring feature-name (match-end 0)))
139 ((string-match "^<-" feature-name)
140 (concat "from." (substring feature-name (match-end 0)))
144 (defun www-uri-decode-char (char-rep)
147 ((string-match "\\(%3A\\|:\\)" char-rep)
148 (setq ccs (substring char-rep 0 (match-beginning 0))
149 cpos (substring char-rep (match-end 0)))
150 (setq ccs (www-uri-decode-feature-name ccs))
152 ((string-match "^0x" cpos)
154 (string-to-number (substring cpos (match-end 0)) 16))
157 (setq cpos (string-to-number cpos))
160 (decode-char ccs cpos))
163 (setq char-rep (decode-uri-string char-rep 'utf-8-mcs-er))
164 (when (= (length char-rep) 1)
168 (defun www-uri-encode-char (char)
169 (if (encode-char char '=ucs)
172 (format "%%%02X" byte))
173 (encode-coding-string (char-to-string char) 'utf-8-mcs-er)
175 (let ((ccs-list '(; =ucs
176 =cns11643-1 =cns11643-2 =cns11643-3
177 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
179 =jis-x0208 =jis-x0208@1990
182 =jis-x0213-1@2000 =jis-x0213-1@2004
183 =jis-x0208@1983 =jis-x0208@1978
187 =>>jis-x0208 =>>jis-x0213-1
188 =>jis-x0208 =>jis-x0213-1
194 (setq ccs (pop ccs-list))
195 (not (setq ret (encode-char char ccs 'defined-only)))))
198 (www-uri-encode-feature-name ccs)
200 ((and (setq ccs (car (split-char char)))
201 (setq ret (encode-char char ccs)))
203 (www-uri-encode-feature-name ccs)
206 (format "system-char-id:0x%X"
207 (encode-char char 'system-char-id))
211 ;;; @ Feature name presentation
214 (defun www-format-feature-name-default (feature-name)
218 (symbol-name feature-name)
222 (defun www-format-feature-name-as-rel-to (feature-name)
223 (concat "\u2192" (substring (symbol-name feature-name) 2)))
225 (defun www-format-feature-name-as-rel-from (feature-name)
226 (concat "\u2190" (substring (symbol-name feature-name) 2)))
228 (defun www-format-feature-name-as-CCS (feature-name)
231 (symbol-name feature-name)
233 (dest (upcase (pop rest))))
234 (when (string-match "^=+>*" dest)
235 (setq dest (concat (substring dest 0 (match-end 0))
237 (substring dest (match-end 0)))))
241 (setq dest (concat dest " " (upcase (pop rest)))))
242 (if (string-match "^[0-9]+$" (car rest))
243 (concat dest "-" (car rest))
244 (concat dest " " (upcase (car rest))))
248 (defun www-format-feature-name* (feature-name &optional lang)
252 (char-feature-property
254 (intern (format "name@%s" lang))))
255 (char-feature-property
256 feature-name 'name)))
257 ((find-charset feature-name)
258 (www-format-feature-name-as-CCS feature-name))
259 ((and (setq name (symbol-name feature-name))
260 (string-match "^\\(->\\)" name))
261 (www-format-feature-name-as-rel-to feature-name))
262 ((string-match "^\\(<-\\)" name)
263 (www-format-feature-name-as-rel-from feature-name))
265 (www-format-feature-name-default feature-name)))))
267 (defun www-format-feature-name (feature-name &optional lang)
268 (www-format-encode-string
269 (www-format-feature-name* feature-name lang)))
272 ;;; @ Feature value presentation
275 (defun www-format-value-as-kuten (value)
277 (- (lsh value -8) 32)
278 (- (logand value 255) 32)))
280 (defun www-format-value-as-char-list (value &optional without-tags)
285 (www-format-encode-string
286 (format (if (characterp unit)
292 (if (characterp unit)
293 (format "<a href=\"%s?char=%s\">%s</a>"
295 (www-uri-encode-char unit)
296 (www-format-encode-string (char-to-string unit)))
297 (www-format-encode-string (format "%s" unit)))))
299 (www-format-encode-string (format "%s" value) without-tags)))
301 (defun www-format-value-as-ids (value &optional without-tags)
306 (www-format-encode-string
307 (format (if (characterp unit)
313 (if (characterp unit)
314 (format "<a href=\"%s?char=%s\">%s</a>"
316 (www-uri-encode-char unit)
317 (www-format-encode-string (char-to-string unit)))
318 (www-format-encode-string (format "%s" unit)))))
319 (ideographic-structure-to-ids value) " ")
320 (www-format-encode-string (format "%s" value) without-tags)))
322 (defun www-format-value-as-S-exp (value &optional without-tags)
323 (www-format-encode-string (format "%S" value) without-tags))
325 (defun www-format-value-as-HEX (value)
328 (www-format-value-as-S-exp value)))
330 (defun www-format-value-as-CCS-default (value)
333 (www-format-value-as-HEX value)
335 (www-format-value-as-S-exp value)))
337 (defun www-format-value-as-CCS-94x94 (value)
339 (format "0x%s [%s] (%d)"
340 (www-format-value-as-HEX value)
341 (www-format-value-as-kuten value)
343 (www-format-value-as-S-exp value)))
345 (defun www-format-value (value &optional feature-name format without-tags)
347 ;; ((find-charset feature-name)
349 ;; ((and (= (charset-chars feature-name) 94)
350 ;; (= (charset-dimension feature-name) 2))
351 ;; (www-format-value-as-CCS-94x94 value))
353 ;; (www-format-value-as-CCS-default value)))
356 ;; (www-format-value-as-S-exp value)))
357 (www-format-apply-value format nil value nil nil without-tags)
361 ;;; @ format evaluator
364 (defun www-format-encode-string (string &optional without-tags)
367 (let (plane code start end char variants ret)
368 (goto-char (point-min))
369 (while (search-forward "<" nil t)
370 (replace-match "<" nil t))
371 (goto-char (point-min))
372 (while (search-forward ">" nil t)
373 (replace-match ">" nil t))
375 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
376 (let ((coded-charset-entity-reference-alist
378 '(=cns11643-1 "C1-" 4 X)
379 '(=cns11643-2 "C2-" 4 X)
380 '(=cns11643-3 "C3-" 4 X)
381 '(=cns11643-4 "C4-" 4 X)
382 '(=cns11643-5 "C5-" 4 X)
383 '(=cns11643-6 "C6-" 4 X)
384 '(=cns11643-7 "C7-" 4 X)
386 '(=gb12345 "G1-" 4 X)
387 '(=jis-x0208@1990 "J90-" 4 X)
388 '(=jis-x0212 "JSP-" 4 X)
390 '(=jis-x0208@1997 "J97-" 4 X)
391 '(=jis-x0208@1978 "J78-" 4 X)
392 '(=jis-x0208@1983 "J83-" 4 X)
394 '(=zinbun-oracle "ZOB-" 4 d)
395 '(=jef-china3 "JC3-" 4 X)
396 '(=daikanwa "M-" 5 d)
397 coded-charset-entity-reference-alist)))
398 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
400 (goto-char (point-min))
401 (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
402 (setq code (string-to-int (match-string 1)))
404 (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
406 chise-wiki-bitmap-glyphs-url
410 (goto-char (point-min))
411 (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
412 (setq plane (match-string 1)
413 code (string-to-int (match-string 2) 16))
415 (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
417 chise-wiki-bitmap-glyphs-url
420 (- (logand code 255) 32))
423 (goto-char (point-min))
424 (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
425 (setq plane (string-to-int (match-string 1))
426 code (string-to-int (match-string 2) 16))
428 (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
430 chise-wiki-bitmap-glyphs-url
433 (- (logand code 255) 32))
436 (goto-char (point-min))
437 (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
438 (setq plane (string-to-int (match-string 1))
439 code (string-to-int (match-string 2) 16))
441 (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
443 chise-wiki-bitmap-glyphs-url
447 (goto-char (point-min))
448 (while (re-search-forward "&JC3-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
449 (setq code (string-to-int (match-string 1) 16))
451 (format "<img alt=\"JC3-%04X\" src=\"http://kanji.zinbun.kyoto-u.ac.jp/db/CHINA3/Gaiji/%04x.gif\">"
455 (goto-char (point-min))
456 (while (re-search-forward "&ZOB-\\([0-9]+\\);" nil t)
457 (setq code (string-to-int (match-string 1)))
459 (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\">"
461 chise-wiki-bitmap-glyphs-url
465 (goto-char (point-min))
466 (while (re-search-forward "&\\(G-\\)?GT-\\([0-9]+\\);" nil t)
467 (setq code (string-to-int (match-string 2)))
469 (format "<img alt=\"GT-%05d\" src=\"%s?char=GT-%05d\">"
471 chise-wiki-glyph-cgi-url
475 (goto-char (point-min))
476 (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
477 (setq code (string-to-int (match-string 1) 16))
479 (format "<img alt=\"B-%04X\" src=\"%s?char=B-%04X\">"
481 chise-wiki-glyph-cgi-url
485 (goto-char (point-min))
486 (while (re-search-forward "&CDP-\\([0-9A-F]+\\);" nil t)
487 (setq code (string-to-int (match-string 1) 16))
489 (format "<img alt=\"CDP-%04X\" src=\"%s?char=CDP-%04X\">"
491 chise-wiki-glyph-cgi-url
495 (goto-char (point-min))
496 (while (re-search-forward "&UU\\+\\([0-9A-F]+\\);" nil t)
497 (setq code (string-to-int (match-string 1) 16))
499 (format "<img alt=\"UU+%04X\" src=\"http://www.unicode.org/cgi-bin/refglyph?24-%04X\">"
504 (goto-char (point-min))
505 (while (re-search-forward "&MCS-\\([0-9A-F]+\\);" nil t)
506 (setq code (string-to-int (match-string 1) 16))
507 (setq start (match-beginning 0)
509 (setq char (decode-char 'system-char-id code))
510 (setq variants (or (char-feature char '->subsumptive)
511 (char-feature char '->denotational)))
513 (setq ret (www-format-encode-string
514 (char-to-string (car variants))))
515 (string-match "&MCS-\\([0-9A-F]+\\);" ret))
516 (setq variants (cdr variants)))
517 (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
519 (delete-region start end)
522 ;; (goto-char (point-min))
523 ;; (while (search-forward ">-" nil t)
524 ;; (replace-match "&GT-" t 'literal))
527 (defun www-format-props-to-string (props &optional format)
529 (setq format (plist-get props :format)))
531 (plist-get props :flag)
532 (if (plist-get props :zero-padding)
534 (if (plist-get props :len)
535 (format "%d" (plist-get props :len)))
537 ((eq format 'decimal) "d")
538 ((eq format 'hex) "x")
539 ((eq format 'HEX) "X")
540 ((eq format 'S-exp) "S")
543 (defun www-format-apply-value (format props value
544 &optional uri-char uri-feature
549 ((memq format '(decimal hex HEX))
551 (format (www-format-props-to-string props format)
553 (www-format-encode-string
558 (www-format-encode-string
559 (format (www-format-props-to-string props format)
563 (www-format-value-as-kuten value))
564 ((eq format 'space-separated-char-list)
565 (www-format-value-as-char-list value without-tags))
566 ((eq format 'space-separated-ids)
567 (www-format-value-as-ids value without-tags))
569 (setq format 'default)
570 (www-format-encode-string
571 (format (www-format-props-to-string props 'default)
574 (if (or without-tags (eq (plist-get props :mode) 'peek))
576 (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
577 ><input type=\"submit\" value=\"edit\" /></a>"
580 uri-char uri-feature format))))
582 (defun www-format-eval-feature-value (char
584 &optional format lang uri-char value)
586 (setq value (char-feature char feature-name)))
588 (setq format (www-feature-value-format feature-name)))
591 (www-format-apply-value
593 uri-char (www-uri-encode-feature-name feature-name))
596 (cond ((null (cdr format))
597 (setq format (car format))
598 (www-format-apply-value
599 (car format) (nth 1 format) value
600 uri-char (www-uri-encode-feature-name feature-name))
603 (www-format-eval-list format char feature-name lang uri-char)
606 (defun www-format-eval-unit (exp char feature-name
607 &optional lang uri-char value)
609 (setq value (char-feature char feature-name)))
611 (setq uri-char (www-uri-encode-char char)))
613 ((stringp exp) (www-format-encode-string exp))
617 ((memq (car exp) '(value decimal hex HEX ku-ten S-exp default))
618 (if (eq (car exp) 'value)
619 (www-format-eval-feature-value char feature-name
620 (plist-get (nth 1 exp) :format)
622 (www-format-apply-value
623 (car exp) (nth 1 exp) value
624 uri-char (www-uri-encode-feature-name feature-name)))
626 ((eq (car exp) 'name)
627 (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
629 (www-uri-encode-feature-name feature-name)
631 (www-format-feature-name feature-name lang))
633 ((eq (car exp) 'link)
638 (www-format-eval-list (plist-get (nth 1 exp) :ref)
639 char feature-name lang uri-char)
640 (www-format-eval-list (nthcdr 2 exp)
641 char feature-name lang uri-char)))
647 (www-format-eval-list (nthcdr 2 exp) char feature-name
651 (defun www-format-eval-list (format-list char feature-name
652 &optional lang uri-char)
653 (if (consp format-list)
656 (www-format-eval-unit exp char feature-name lang uri-char))
658 (www-format-eval-unit format-list char feature-name lang uri-char)))
664 (defun www-html-display-text (text)
668 (goto-char (point-min))
669 (while (search-forward "<" nil t)
670 (replace-match "<" nil t))
671 (goto-char (point-min))
672 (while (search-forward ">" nil t)
673 (replace-match ">" nil t))
674 (goto-char (point-min))
675 (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
677 (format "<a href=\"%s\">%s</a>"
681 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
682 (goto-char (point-min))
683 (while (search-forward ">-" nil t)
684 (replace-match "&GT-" nil t))
687 (defun www-html-display-paragraph (text)
689 (www-html-display-text text)
692 (provide 'cwiki-common)