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-glyphs-url
8 "http://chise.zinbun.kyoto-u.ac.jp/glyphs/")
10 (defun decode-uri-string (string &optional coding-system)
11 (if (> (length string) 0)
15 (mapconcat (lambda (char)
18 (char-to-string char)))
20 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
21 (setq dest (concat dest
22 (substring string i (match-beginning 0))
25 (string-to-int (match-string 1 string) 16))))
28 (concat dest (substring string i))
31 (defun www-feature-type (feature-name)
32 (or (char-feature-property feature-name 'type)
33 (let ((str (symbol-name feature-name)))
35 ((string-match "^\\(->\\|<-\\)" str)
37 ((string-match "^ideographic-structure\\(@\\|$\\)" str)
41 (defun www-feature-value-format (feature-name)
42 (or (char-feature-property feature-name 'value-format)
43 (let ((type (www-feature-type feature-name)))
44 (cond ((eq type 'relation)
45 'space-separated-char-list)
47 'space-separated-ids)))
48 (if (find-charset feature-name)
49 (if (and (= (charset-dimension feature-name) 2)
50 (= (charset-chars feature-name) 94))
52 " (" (decimal) ") <" (ku-ten) ">")
53 '("0x" (HEX) " (" (decimal) ")")))))
56 ;;; @ URI representation
59 (defun www-uri-decode-feature-name (uri-feature)
62 ((string-match "^from\\." uri-feature)
63 (intern (format "<-%s" (substring uri-feature (match-end 0))))
65 ((string-match "^to\\." uri-feature)
66 (intern (format "->%s" (substring uri-feature (match-end 0))))
68 ((string-match "^rep\\." uri-feature)
69 (intern (format "=%s" (substring uri-feature (match-end 0))))
71 ((string-match "^g\\." uri-feature)
72 (intern (format "=>>%s" (substring uri-feature (match-end 0))))
74 ((string-match "^gi\\." uri-feature)
75 (intern (format "=>>>%s" (substring uri-feature (match-end 0))))
77 ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
78 (intern (format "=>>%s%s"
79 (make-string (string-to-int
80 (match-string 1 uri-feature))
82 (substring uri-feature (match-end 0))))
84 ((string-match "^a\\." uri-feature)
85 (intern (format "=>%s" (substring uri-feature (match-end 0))))
87 ((string-match "^a\\([0-9]+\\)\\." uri-feature)
88 (intern (format "%s>%s"
89 (make-string (string-to-int
90 (match-string 1 uri-feature))
92 (substring uri-feature (match-end 0))))
94 ((and (setq feature (intern (format "=>%s" uri-feature)))
95 (find-charset feature))
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 (t (intern uri-feature)))))
108 (defun www-uri-encode-feature-name (feature-name)
109 (setq feature-name (symbol-name feature-name))
111 ((string-match "^=\\([^=>]+\\)" feature-name)
112 (concat "rep." (substring feature-name (match-beginning 1)))
114 ((string-match "^=>>\\([^=>]+\\)" feature-name)
115 (concat "g." (substring feature-name (match-beginning 1)))
117 ((string-match "^=>>>\\([^=>]+\\)" feature-name)
118 (concat "gi." (substring feature-name (match-beginning 1)))
120 ((string-match "^=>>\\(>+\\)" feature-name)
122 (length (match-string 1 feature-name))
123 (substring feature-name (match-end 1)))
125 ((string-match "^=>\\([^=>]+\\)" feature-name)
126 (concat "a." (substring feature-name (match-beginning 1)))
128 ((string-match "^\\(=+\\)>" feature-name)
130 (length (match-string 1 feature-name))
131 (substring feature-name (match-end 0)))
133 ((string-match "^->" feature-name)
134 (concat "to." (substring feature-name (match-end 0)))
136 ((string-match "^<-" feature-name)
137 (concat "from." (substring feature-name (match-end 0)))
141 (defun www-uri-decode-char (char-rep)
144 ((string-match "\\(%3A\\|:\\)" char-rep)
145 (setq ccs (substring char-rep 0 (match-beginning 0))
146 cpos (substring char-rep (match-end 0)))
147 (setq ccs (www-uri-decode-feature-name ccs))
149 ((string-match "^0x" cpos)
151 (string-to-number (substring cpos (match-end 0)) 16))
154 (setq cpos (string-to-number cpos))
157 (decode-char ccs cpos))
160 (setq char-rep (decode-uri-string char-rep 'utf-8-mcs-er))
161 (when (= (length char-rep) 1)
165 (defun www-uri-encode-char (char)
166 (if (encode-char char '=ucs)
169 (format "%%%02X" byte))
170 (encode-coding-string (char-to-string char) 'utf-8-mcs-er)
172 (let ((ccs-list '(; =ucs
173 =cns11643-1 =cns11643-2 =cns11643-3
174 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
176 =jis-x0208 =jis-x0208@1990
179 =jis-x0213-1@2000 =jis-x0213-1@2004
180 =jis-x0208@1983 =jis-x0208@1978
184 =>>jis-x0208 =>>jis-x0213-1
185 =>jis-x0208 =>jis-x0213-1
190 (setq ccs (pop ccs-list))
191 (not (setq ret (encode-char char ccs 'defined-only)))))
194 (www-uri-encode-feature-name ccs)
196 ((and (setq ccs (car (split-char char)))
197 (setq ret (encode-char char ccs)))
199 (www-uri-encode-feature-name ccs)
202 (format "system-char-id:0x%X"
203 (encode-char char 'system-char-id))
207 ;;; @ Feature name presentation
210 (defun www-format-feature-name-default (feature-name)
214 (symbol-name feature-name)
218 (defun www-format-feature-name-as-rel-to (feature-name)
219 (concat "\u2192" (substring (symbol-name feature-name) 2)))
221 (defun www-format-feature-name-as-rel-from (feature-name)
222 (concat "\u2190" (substring (symbol-name feature-name) 2)))
224 (defun www-format-feature-name-as-CCS (feature-name)
227 (symbol-name feature-name)
229 (dest (upcase (pop rest))))
230 (when (string-match "^=+>*" dest)
231 (setq dest (concat (substring dest 0 (match-end 0))
233 (substring dest (match-end 0)))))
237 (setq dest (concat dest " " (upcase (pop rest)))))
238 (if (string-match "^[0-9]+$" (car rest))
239 (concat dest "-" (car rest))
240 (concat dest " " (upcase (car rest))))
244 (defun www-format-feature-name* (feature-name &optional lang)
248 (char-feature-property
250 (intern (format "name@%s" lang))))
251 (char-feature-property
252 feature-name 'name)))
253 ((find-charset feature-name)
254 (www-format-feature-name-as-CCS feature-name))
255 ((and (setq name (symbol-name feature-name))
256 (string-match "^\\(->\\)" name))
257 (www-format-feature-name-as-rel-to feature-name))
258 ((string-match "^\\(<-\\)" name)
259 (www-format-feature-name-as-rel-from feature-name))
261 (www-format-feature-name-default feature-name)))))
263 (defun www-format-feature-name (feature-name &optional lang)
264 (www-format-encode-string
265 (www-format-feature-name* feature-name lang)))
268 ;;; @ Feature value presentation
271 (defun www-format-value-as-kuten (value)
273 (- (lsh value -8) 32)
274 (- (logand value 255) 32)))
276 (defun www-format-value-as-char-list (value &optional without-tags)
281 (www-format-encode-string
282 (format (if (characterp unit)
288 (if (characterp unit)
289 (format "<a href=\"%s?char=%s\">%s</a>"
291 (www-uri-encode-char unit)
292 (www-format-encode-string (char-to-string unit)))
293 (www-format-encode-string (format "%s" unit)))))
295 (www-format-encode-string (format "%s" value) without-tags)))
297 (defun www-format-value-as-ids (value &optional without-tags)
302 (www-format-encode-string
303 (format (if (characterp unit)
309 (if (characterp unit)
310 (format "<a href=\"%s?char=%s\">%s</a>"
312 (www-uri-encode-char unit)
313 (www-format-encode-string (char-to-string unit)))
314 (www-format-encode-string (format "%s" unit)))))
315 (ideographic-structure-to-ids value) " ")
316 (www-format-encode-string (format "%s" value) without-tags)))
318 (defun www-format-value-as-S-exp (value &optional without-tags)
319 (www-format-encode-string (format "%S" value) without-tags))
321 (defun www-format-value-as-HEX (value)
324 (www-format-value-as-S-exp value)))
326 (defun www-format-value-as-CCS-default (value)
329 (www-format-value-as-HEX value)
331 (www-format-value-as-S-exp value)))
333 (defun www-format-value-as-CCS-94x94 (value)
335 (format "0x%s [%s] (%d)"
336 (www-format-value-as-HEX value)
337 (www-format-value-as-kuten value)
339 (www-format-value-as-S-exp value)))
341 (defun www-format-value (value &optional feature-name format without-tags)
343 ;; ((find-charset feature-name)
345 ;; ((and (= (charset-chars feature-name) 94)
346 ;; (= (charset-dimension feature-name) 2))
347 ;; (www-format-value-as-CCS-94x94 value))
349 ;; (www-format-value-as-CCS-default value)))
352 ;; (www-format-value-as-S-exp value)))
353 (www-format-apply-value format nil value nil nil without-tags)
357 ;;; @ format evaluator
360 (defun www-format-encode-string (string &optional without-tags)
364 (goto-char (point-min))
365 (while (search-forward "<" nil t)
366 (replace-match "<" nil t))
367 (goto-char (point-min))
368 (while (search-forward ">" nil t)
369 (replace-match ">" nil t))
371 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
372 (let ((coded-charset-entity-reference-alist
374 '(=cns11643-1 "C1-" 4 X)
375 '(=cns11643-2 "C2-" 4 X)
376 '(=cns11643-3 "C3-" 4 X)
377 '(=cns11643-4 "C4-" 4 X)
378 '(=cns11643-5 "C5-" 4 X)
379 '(=cns11643-6 "C6-" 4 X)
380 '(=cns11643-7 "C7-" 4 X)
382 '(=gb12345 "G1-" 4 X)
383 '(=jis-x0208@1990 "J90-" 4 X)
384 '(=jis-x0212 "JSP-" 4 X)
386 '(=jef-china3 "JC3-" 4 X)
387 '(=jis-x0208@1997 "J97-" 4 X)
388 '(=jis-x0208@1978 "J78-" 4 X)
389 '(=jis-x0208@1983 "J83-" 4 X)
390 '(=zinbun-oracle "ZOB-" 4 d)
391 '(=daikanwa "M-" 5 d)
392 coded-charset-entity-reference-alist)))
393 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
395 (goto-char (point-min))
396 (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
397 (setq code (string-to-int (match-string 1)))
399 (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
401 chise-wiki-glyphs-url
405 (goto-char (point-min))
406 (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
407 (setq plane (match-string 1)
408 code (string-to-int (match-string 2) 16))
410 (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
412 chise-wiki-glyphs-url
415 (- (logand code 255) 32))
418 (goto-char (point-min))
419 (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
420 (setq plane (string-to-int (match-string 1))
421 code (string-to-int (match-string 2) 16))
423 (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
425 chise-wiki-glyphs-url
428 (- (logand code 255) 32))
431 (goto-char (point-min))
432 (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
433 (setq plane (string-to-int (match-string 1))
434 code (string-to-int (match-string 2) 16))
436 (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
438 chise-wiki-glyphs-url
442 (goto-char (point-min))
443 (while (search-forward ">-" nil t)
444 (replace-match "&GT-" t 'literal))
448 (defun www-format-props-to-string (props &optional format)
450 (setq format (plist-get props :format)))
452 (plist-get props :flag)
453 (if (plist-get props :zero-padding)
455 (if (plist-get props :len)
456 (format "%d" (plist-get props :len)))
458 ((eq format 'decimal) "d")
459 ((eq format 'hex) "x")
460 ((eq format 'HEX) "X")
461 ((eq format 'S-exp) "S")
464 (defun www-format-apply-value (format props value
465 &optional uri-char uri-feature
470 ((memq format '(decimal hex HEX))
472 (format (www-format-props-to-string props format)
474 (www-format-encode-string
479 (www-format-encode-string
480 (format (www-format-props-to-string props format)
484 (www-format-value-as-kuten value))
485 ((eq format 'space-separated-char-list)
486 (www-format-value-as-char-list value without-tags))
487 ((eq format 'space-separated-ids)
488 (www-format-value-as-ids value without-tags))
490 (setq format 'default)
491 (www-format-encode-string
492 (format (www-format-props-to-string props 'default)
495 (if (or without-tags (eq (plist-get props :mode) 'peek))
497 (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
498 ><input type=\"submit\" value=\"edit\" /></a>"
501 uri-char uri-feature format))))
503 (defun www-format-eval-feature-value (char
505 &optional format lang uri-char value)
507 (setq value (char-feature char feature-name)))
509 (setq format (www-feature-value-format feature-name)))
512 (www-format-apply-value
514 uri-char (www-uri-encode-feature-name feature-name))
517 (cond ((null (cdr format))
518 (setq format (car format))
519 (www-format-apply-value
520 (car format) (nth 1 format) value
521 uri-char (www-uri-encode-feature-name feature-name))
524 (www-format-eval-list format char feature-name lang uri-char)
527 (defun www-format-eval-unit (exp char feature-name
528 &optional lang uri-char value)
530 (setq value (char-feature char feature-name)))
532 (setq uri-char (www-uri-encode-char char)))
534 ((stringp exp) (www-format-encode-string exp))
538 ((memq (car exp) '(value decimal hex HEX ku-ten S-exp default))
539 (if (eq (car exp) 'value)
540 (www-format-eval-feature-value char feature-name
541 (plist-get (nth 1 exp) :format)
543 (www-format-apply-value
544 (car exp) (nth 1 exp) value
545 uri-char (www-uri-encode-feature-name feature-name)))
547 ((eq (car exp) 'name)
548 (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
550 (www-uri-encode-feature-name feature-name)
552 (www-format-feature-name feature-name lang))
554 ((eq (car exp) 'link)
559 (www-format-eval-list (plist-get (nth 1 exp) :ref)
560 char feature-name lang uri-char)
561 (www-format-eval-list (nthcdr 2 exp)
562 char feature-name lang uri-char)))
568 (www-format-eval-list (nthcdr 2 exp) char feature-name
572 (defun www-format-eval-list (format-list char feature-name
573 &optional lang uri-char)
574 (if (consp format-list)
577 (www-format-eval-unit exp char feature-name lang uri-char))
579 (www-format-eval-unit format-list char feature-name lang uri-char)))
585 (defun www-html-display-text (text)
589 (goto-char (point-min))
590 (while (search-forward "<" nil t)
591 (replace-match "<" nil t))
592 (goto-char (point-min))
593 (while (search-forward ">" nil t)
594 (replace-match ">" nil t))
595 (goto-char (point-min))
596 (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
598 (format "<a href=\"%s\">%s</a>"
602 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
603 (goto-char (point-min))
604 (while (search-forward ">-" nil t)
605 (replace-match "&GT-" nil t))
608 (defun www-html-display-paragraph (text)
610 (www-html-display-text text)
613 (provide 'cwiki-common)