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 (if (memq (www-feature-type feature-name)
44 '(relation structure))
45 'space-separated-char-list)
46 (if (find-charset feature-name)
47 (if (and (= (charset-dimension feature-name) 2)
48 (= (charset-chars feature-name) 94))
50 " (" (decimal) ") <" (ku-ten) ">")
51 '("0x" (HEX) " (" (decimal) ")")))))
54 ;;; @ URI representation
57 (defun www-uri-decode-feature-name (uri-feature)
60 ((string-match "^from\\." uri-feature)
61 (intern (format "<-%s" (substring uri-feature (match-end 0))))
63 ((string-match "^to\\." uri-feature)
64 (intern (format "->%s" (substring uri-feature (match-end 0))))
66 ((string-match "^rep\\." uri-feature)
67 (intern (format "=%s" (substring uri-feature (match-end 0))))
69 ((string-match "^g\\." uri-feature)
70 (intern (format "=>>%s" (substring uri-feature (match-end 0))))
72 ((string-match "^gi\\." uri-feature)
73 (intern (format "=>>>%s" (substring uri-feature (match-end 0))))
75 ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
76 (intern (format "=>>%s%s"
77 (make-string (string-to-int
78 (match-string 1 uri-feature))
80 (substring uri-feature (match-end 0))))
82 ((string-match "^a\\." uri-feature)
83 (intern (format "=>%s" (substring uri-feature (match-end 0))))
85 ((string-match "^a\\([0-9]+\\)\\." uri-feature)
86 (intern (format "%s>%s"
87 (make-string (string-to-int
88 (match-string 1 uri-feature))
90 (substring uri-feature (match-end 0))))
92 ((and (setq feature (intern (format "=>%s" uri-feature)))
93 (find-charset feature))
95 ((and (setq feature (intern (format "=>>%s" uri-feature)))
96 (find-charset feature))
98 ((and (setq feature (intern (format "=>>>%s" uri-feature)))
99 (find-charset feature))
101 ((and (setq feature (intern (format "=%s" uri-feature)))
102 (find-charset feature))
104 (t (intern uri-feature)))))
106 (defun www-uri-encode-feature-name (feature-name)
107 (setq feature-name (symbol-name feature-name))
109 ((string-match "^=\\([^=>]+\\)" feature-name)
110 (concat "rep." (substring feature-name (match-beginning 1)))
112 ((string-match "^=>>\\([^=>]+\\)" feature-name)
113 (concat "g." (substring feature-name (match-beginning 1)))
115 ((string-match "^=>>>\\([^=>]+\\)" feature-name)
116 (concat "gi." (substring feature-name (match-beginning 1)))
118 ((string-match "^=>>\\(>+\\)" feature-name)
120 (length (match-string 1 feature-name))
121 (substring feature-name (match-end 1)))
123 ((string-match "^=>\\([^=>]+\\)" feature-name)
124 (concat "a." (substring feature-name (match-beginning 1)))
126 ((string-match "^\\(=+\\)>" feature-name)
128 (length (match-string 1 feature-name))
129 (substring feature-name (match-end 0)))
131 ((string-match "^->" feature-name)
132 (concat "to." (substring feature-name (match-end 0)))
134 ((string-match "^<-" feature-name)
135 (concat "from." (substring feature-name (match-end 0)))
139 (defun www-uri-decode-char (char-rep)
142 ((string-match ":" char-rep)
143 (setq ccs (substring char-rep 0 (match-beginning 0))
144 cpos (substring char-rep (match-end 0)))
145 (setq ccs (www-uri-decode-feature-name ccs))
147 ((string-match "^0x" cpos)
149 (string-to-number (substring cpos (match-end 0)) 16))
152 (setq cpos (string-to-number cpos))
155 (decode-char ccs cpos))
157 ((= (length char-rep) 1)
161 (defun www-uri-encode-char (char)
162 (let ((ccs-list '(=ucs
163 =cns11643-1 =cns11643-2 =cns11643-3
164 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
166 =jis-x0208 =jis-x0208@1990
169 =jis-x0213-1@2000 =jis-x0213-1@2004
170 =jis-x0208@1983 =jis-x0208@1978
176 =>>jis-x0208 =>>jis-x0213-1
177 =>jis-x0208 =>jis-x0213-1))
180 (setq ccs (pop ccs-list))
181 (not (setq ret (encode-char char ccs 'defined-only)))))
184 (www-uri-encode-feature-name ccs)
186 ((setq ccs (car (split-char char)))
188 (www-uri-encode-feature-name ccs)
189 (encode-char char ccs))))))
192 ;;; @ Feature name presentation
195 (defun www-format-feature-name-default (feature-name)
199 (symbol-name feature-name)
203 (defun www-format-feature-name-as-rel-to (feature-name)
204 (concat "\u2192" (substring (symbol-name feature-name) 2)))
206 (defun www-format-feature-name-as-rel-from (feature-name)
207 (concat "\u2190" (substring (symbol-name feature-name) 2)))
209 (defun www-format-feature-name-as-CCS (feature-name)
212 (symbol-name feature-name)
214 (dest (upcase (pop rest))))
218 (setq dest (concat dest " " (upcase (pop rest)))))
219 (if (string-match "^[0-9]+$" (car rest))
220 (concat dest "-" (car rest))
221 (concat dest " " (upcase (car rest))))
225 (defun www-format-feature-name (feature-name &optional lang)
227 (www-format-encode-string
230 (char-feature-property
232 (intern (format "name@%s" lang))))
233 (char-feature-property
234 feature-name 'name)))
235 ((find-charset feature-name)
236 (www-format-feature-name-as-CCS feature-name))
237 ((and (setq name (symbol-name feature-name))
238 (string-match "^\\(->\\)" name))
239 (www-format-feature-name-as-rel-to feature-name))
240 ((string-match "^\\(<-\\)" name)
241 (www-format-feature-name-as-rel-from feature-name))
243 (www-format-feature-name-default feature-name))))))
246 ;;; @ Feature value presentation
249 (defun www-format-value-as-kuten (value)
251 (- (lsh value -8) 32)
252 (- (logand value 255) 32)))
254 (defun www-format-value-as-char-list (value &optional without-tags)
259 (www-format-encode-string
260 (format (if (characterp unit)
266 (if (characterp unit)
267 (format "<a href=\"%s?char=%s\">%s</a>"
269 (www-uri-encode-char unit)
270 (www-format-encode-string (char-to-string unit)))
271 (www-format-encode-string (format "%s" unit)))))
273 (www-format-encode-string (format "%s" value) without-tags)))
275 (defun www-format-value-as-S-exp (value &optional without-tags)
276 (www-format-encode-string (format "%S" value) without-tags))
278 (defun www-format-value-as-HEX (value)
281 (www-format-value-as-S-exp value)))
283 (defun www-format-value-as-CCS-default (value)
286 (www-format-value-as-HEX value)
288 (www-format-value-as-S-exp value)))
290 (defun www-format-value-as-CCS-94x94 (value)
292 (format "0x%s [%s] (%d)"
293 (www-format-value-as-HEX value)
294 (www-format-value-as-kuten value)
296 (www-format-value-as-S-exp value)))
298 (defun www-format-value (value &optional feature-name format without-tags)
300 ;; ((find-charset feature-name)
302 ;; ((and (= (charset-chars feature-name) 94)
303 ;; (= (charset-dimension feature-name) 2))
304 ;; (www-format-value-as-CCS-94x94 value))
306 ;; (www-format-value-as-CCS-default value)))
309 ;; (www-format-value-as-S-exp value)))
310 (www-format-apply-value format nil value nil nil without-tags)
314 ;;; @ format evaluator
317 (defun www-format-encode-string (string &optional without-tags)
321 (goto-char (point-min))
322 (while (search-forward "<" nil t)
323 (replace-match "<" nil t))
324 (goto-char (point-min))
325 (while (search-forward ">" nil t)
326 (replace-match ">" nil t))
328 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
329 (let ((coded-charset-entity-reference-alist
331 '(=cns11643-1 "C1-" 4 X)
332 '(=cns11643-2 "C2-" 4 X)
333 '(=cns11643-3 "C3-" 4 X)
334 '(=cns11643-4 "C4-" 4 X)
335 '(=cns11643-5 "C5-" 4 X)
336 '(=cns11643-6 "C6-" 4 X)
337 '(=cns11643-7 "C7-" 4 X)
339 '(=gb12345 "G1-" 4 X)
340 '(=jis-x0208@1990 "J90-" 4 X)
341 '(=jis-x0212 "JSP-" 4 X)
343 '(=jef-china3 "JC3-" 4 X)
344 '(=jis-x0208@1997 "J97-" 4 X)
345 '(=jis-x0208@1978 "J78-" 4 X)
346 '(=jis-x0208@1983 "J83-" 4 X)
347 '(=zinbun-oracle "ZOB-" 4 d)
348 '(=daikanwa "M-" 5 d)
349 coded-charset-entity-reference-alist)))
350 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
352 (goto-char (point-min))
353 (while (re-search-forward "&CB\\([0-9]+\\);" nil t)
354 (setq code (string-to-int (match-string 1)))
356 (format "<img alt=\"CB%05d\" src=\"%s/cb-gaiji/%02d/CB%05d.gif\">"
358 chise-wiki-glyphs-url
362 (goto-char (point-min))
363 (while (re-search-forward "&J\\(78\\|83\\|90\\|97\\|SP\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
364 (setq plane (match-string 1)
365 code (string-to-int (match-string 2) 16))
367 (format "<img alt=\"J%s-%04X\" src=\"%s/JIS-%s/%02d-%02d.gif\">"
369 chise-wiki-glyphs-url
372 (- (logand code 255) 32))
375 (goto-char (point-min))
376 (while (re-search-forward "&G\\([01]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
377 (setq plane (string-to-int (match-string 1))
378 code (string-to-int (match-string 2) 16))
380 (format "<img alt=\"GB%d-%04X\" src=\"%s/GB%d/%02d-%02d.gif\">"
382 chise-wiki-glyphs-url
385 (- (logand code 255) 32))
388 (goto-char (point-min))
389 (while (re-search-forward "&C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\);" nil t)
390 (setq plane (string-to-int (match-string 1))
391 code (string-to-int (match-string 2) 16))
393 (format "<img alt=\"CNS%d-%04X\" src=\"%s/CNS%d/%04X.gif\">"
395 chise-wiki-glyphs-url
399 (goto-char (point-min))
400 (while (search-forward ">-" nil t)
401 (replace-match "&GT-" t 'literal))
405 (defun www-format-props-to-string (props &optional format)
407 (setq format (plist-get props :format)))
409 (plist-get props :flag)
410 (if (plist-get props :zero-padding)
412 (if (plist-get props :len)
413 (format "%d" (plist-get props :len)))
415 ((eq format 'decimal) "d")
416 ((eq format 'hex) "x")
417 ((eq format 'HEX) "X")
418 ((eq format 'S-exp) "S")
421 (defun www-format-apply-value (format props value
422 &optional uri-char uri-feature
427 ((memq format '(decimal hex HEX))
429 (format (www-format-props-to-string props format)
431 (www-format-encode-string
436 (www-format-encode-string
437 (format (www-format-props-to-string props format)
441 (www-format-value-as-kuten value))
442 ((eq format 'space-separated-char-list)
443 (www-format-value-as-char-list value without-tags))
445 (setq format 'default)
446 (www-format-encode-string
447 (format (www-format-props-to-string props 'default)
450 (if (or without-tags (eq (plist-get props :mode) 'peek))
452 (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
453 ><input type=\"submit\" value=\"edit\" /></a>"
456 uri-char uri-feature format))))
458 (defun www-format-eval-feature-value (char
460 &optional format lang uri-char value)
462 (setq value (char-feature char feature-name)))
464 (setq format (www-feature-value-format feature-name)))
467 (www-format-apply-value
469 uri-char (www-uri-encode-feature-name feature-name))
472 (cond ((null (cdr format))
473 (setq format (car format))
474 (www-format-apply-value
475 (car format) (nth 1 format) value
476 uri-char (www-uri-encode-feature-name feature-name))
479 (www-format-eval-list format char feature-name lang uri-char)
482 (defun www-format-eval-unit (exp char feature-name
483 &optional lang uri-char value)
485 (setq value (char-feature char feature-name)))
487 (setq uri-char (www-uri-encode-char char)))
489 ((stringp exp) (www-format-encode-string exp))
493 ((memq (car exp) '(value decimal hex HEX ku-ten S-exp default))
494 (if (eq (car exp) 'value)
495 (www-format-eval-feature-value char feature-name
496 (plist-get (nth 1 exp) :format)
498 (www-format-apply-value
499 (car exp) (nth 1 exp) value
500 uri-char (www-uri-encode-feature-name feature-name)))
502 ((eq (car exp) 'name)
503 (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
505 (www-uri-encode-feature-name feature-name)
507 (www-format-feature-name feature-name lang))
509 ((eq (car exp) 'link)
514 (www-format-eval-list (plist-get (nth 1 exp) :ref)
515 char feature-name lang uri-char)
516 (www-format-eval-list (nthcdr 2 exp)
517 char feature-name lang uri-char)))
523 (www-format-eval-list (nthcdr 2 exp) char feature-name
527 (defun www-format-eval-list (format-list char feature-name
528 &optional lang uri-char)
529 (if (consp format-list)
532 (www-format-eval-unit exp char feature-name lang uri-char))
534 (www-format-eval-unit format-list char feature-name lang uri-char)))
540 (defun www-html-display-text (text)
544 (goto-char (point-min))
545 (while (search-forward "<" nil t)
546 (replace-match "<" nil t))
547 (goto-char (point-min))
548 (while (search-forward ">" nil t)
549 (replace-match ">" nil t))
550 (goto-char (point-min))
551 (while (re-search-forward "\\[\\[\\([^]|[]+\\)|\\([^][]+\\)\\]\\]" nil t)
553 (format "<a href=\"%s\">%s</a>"
557 (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
558 (goto-char (point-min))
559 (while (search-forward "&" nil t)
560 (replace-match "&" nil t))
563 (defun www-html-display-paragraph (text)
565 (www-html-display-text text)
568 (provide 'cwiki-common)