update.
[chise/est.git] / cwiki-common.el
index f6aa4ad..fa6a4e8 100644 (file)
@@ -2,7 +2,7 @@
 (require 'char-db-util)
 
 (defvar chise-wiki-view-url "view.cgi")
-(defvar chise-wiki-edit-url "edit/edit.cgi")
+(defvar chise-wiki-edit-url "edit.cgi")
 
 (defvar chise-wiki-bitmap-glyphs-url
   "http://chise.zinbun.kyoto-u.ac.jp/glyphs")
                      =cbeta =jef-china3
                      =jis-x0213-1@2000 =jis-x0213-1@2004
                      =jis-x0208@1983 =jis-x0208@1978
-                     =zinbun-oracle
+                     =zinbun-oracle =>zinbun-oracle
                      =daikanwa
                      =gt =gt-k
                      =>>jis-x0208 =>>jis-x0213-1
                      =>jis-x0208 =>jis-x0213-1
                      =>>gt
+                     =ruimoku-v6
                      =big5
                      =big5-cdp))
          ccs ret)
          (- (lsh value -8) 32)
          (- (logand value 255) 32)))
 
+(defun www-format-value-default (value &optional without-tags)
+  (if (listp value)
+      (mapconcat
+       (lambda (unit)
+        (www-format-encode-string
+         (format "%S" unit)
+         without-tags))
+       value " ")
+    (www-format-encode-string (format "%S" value) without-tags)))
+  
 (defun www-format-value-as-char-list (value &optional without-tags)
   (if (listp value)
       (mapconcat
        value " ")
     (www-format-encode-string (format "%s" value) without-tags)))
 
+(defun www-format-value-as-domain-list (value &optional without-tags)
+  (let (name source0 source num dest rest unit start end ddest)
+    (if (listp value)
+       (if without-tags
+           (mapconcat
+            (lambda (unit)
+              (format "%s" unit))
+            value " ")
+         (setq rest value)
+         (while rest
+           (setq unit (pop rest))
+           (if (symbolp unit)
+               (setq name (symbol-name unit)))
+           (setq dest
+                 (concat
+                  dest
+                  (cond
+                   ((string-match "^zob1968=" name)
+                    (setq source (intern (substring name 0 (match-end 0)))
+                          num (substring name (match-end 0)))
+                    (if (string-match "^\\([0-9]+\\)-\\([0-9]+\\)$" num)
+                        (setq start (string-to-number
+                                     (match-string 1 num))
+                              end (string-to-number
+                                   (match-string 2 num)))
+                      (setq start (string-to-number num)
+                            end start))
+                    (setq ddest
+                          (if (eq source source0)
+                              (format
+                               ", <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
+                               start start)
+                            (setq source0 source)
+                            (format
+                             " <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/\">%s</a>=<a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
+                             (www-format-encode-string "\u4EAC大人\u6587研甲\u9AA8")
+                             start start)))
+                    (setq start (1+ start))
+                    (while (<= start end)
+                      (setq ddest
+                            (concat
+                             ddest
+                             (format
+                              ", <a href=\"http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/%04d\">%04d</a>"
+                              start start)))
+                      (setq start (1+ start)))
+                    ddest)
+                   (t
+                    (setq source unit)
+                    (if (eq source source0)
+                        ""
+                      (setq source0 source)
+                      (concat " " name))
+                    )))))
+         dest)
+      (www-format-encode-string (format "%s" value) without-tags))))
+
 (defun www-format-value-as-ids (value &optional without-tags)
   (if (listp value)
       (mapconcat
              value)
     (www-format-value-as-S-exp value)))
 
+(defun www-format-value-as-kangxi-radical (value)
+  (if (and (integerp value)
+          (<= 0 value)
+          (<= value 214))
+      (www-format-encode-string
+       (format "%c" (ideographic-radical value)))
+    (www-format-value-as-S-exp value)))
+
 (defun www-format-value (object feature-name
-                               &optional value format without-tags)
+                               &optional value format
+                               without-tags without-edit)
   (unless value
     (setq value (www-char-feature object feature-name)))
   (www-format-apply-value object feature-name
-                         format nil value nil nil without-tags)
+                         format nil value nil nil
+                         without-tags without-edit)
   )
 
 
 (defun www-format-encode-string (string &optional without-tags)
   (with-temp-buffer
     (insert string)
-    (let (plane code start end char variants ret)
+    (let (plane code start end char variants ret rret)
       (goto-char (point-min))
       (while (search-forward "<" nil t)
        (replace-match "&lt;" nil t))
          (encode-coding-region (point-min)(point-max) 'utf-8-mcs-er)
        (let ((coded-charset-entity-reference-alist
               (list*
+               '(=gt                   "GT-" 5 d)
                '(=cns11643-1           "C1-" 4 X)
                '(=cns11643-2           "C2-" 4 X)
                '(=cns11643-3           "C3-" 4 X)
                '(=jis-x0208@1997       "J97-" 4 X)
                '(=jis-x0208@1978       "J78-" 4 X)
                '(=jis-x0208@1983       "J83-" 4 X)
-               '(=gt                   "GT-" 5 d)
+               '(=ruimoku-v6           "RUI6-" 4 X)
                '(=zinbun-oracle        "ZOB-" 4 d)
                '(=jef-china3           "JC3-" 4 X)
                '(=daikanwa             "M-" 5 d)
             t 'literal))
 
          (goto-char (point-min))
-         (while (re-search-forward "&ZOB-\\([0-9]+\\);" nil t)
-           (setq code (string-to-int (match-string 1)))
+         (while (re-search-forward "&\\(A-\\)?ZOB-\\([0-9]+\\);" nil t)
+           (setq code (string-to-int (match-string 2)))
            (replace-match
             (format "<img alt=\"ZOB-%04d\" src=\"%s/ZOB-1968/%04d.png\">"
                     code
             t 'literal))
 
          (goto-char (point-min))
+         (while (re-search-forward "&\\(G-\\)?GT-K\\([0-9]+\\);" nil t)
+           (setq code (string-to-int (match-string 2)))
+           (replace-match
+            (format "<img alt=\"GT-K%05d\" src=\"%s?char=GT-K%05d\">"
+                    code
+                    chise-wiki-glyph-cgi-url
+                    code)
+            t 'literal))
+
+         (goto-char (point-min))
          (while (re-search-forward "&B-\\([0-9A-F]+\\);" nil t)
            (setq code (string-to-int (match-string 1) 16))
            (replace-match
                     chise-wiki-glyph-cgi-url
                     code)
             t 'literal))
-         
+
+         (goto-char (point-min))
+         (while (re-search-forward "&RUI6-\\([0-9A-F]+\\);" nil t)
+           (setq code (string-to-int (match-string 1) 16))
+           (replace-match
+            (format "<img alt=\"RUI6-%04X\" src=\"%s?char=RUI6-%04X\">"
+                    code
+                    chise-wiki-glyph-cgi-url
+                    code)
+            t 'literal))
+
          (goto-char (point-min))
          (while (re-search-forward "&\\(UU\\+\\|U-\\)\\([0-9A-F]+\\);" nil t)
            (setq code (string-to-int (match-string 2) 16))
            (setq start (match-beginning 0)
                  end (match-end 0))
            (setq char (decode-char 'system-char-id code))
-           (setq variants (or (www-char-feature char '->subsumptive)
-                              (www-char-feature char '->denotational)))
-           (while (and variants
-                       (setq ret (www-format-encode-string
-                                  (char-to-string (car variants))))
-                       (string-match "&MCS-\\([0-9A-F]+\\);" ret))
-             (setq variants (cdr variants)))
-           (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
-             (goto-char start)
-             (delete-region start end)
-             (insert ret)))
+           (cond
+            ((and (setq variants
+                        (or (www-char-feature char '->subsumptive)
+                            (www-char-feature char '->denotational)))
+                  (progn
+                    (while (and variants
+                                (setq ret (www-format-encode-string
+                                           (char-to-string (car variants))))
+                                (string-match "&MCS-\\([0-9A-F]+\\);" ret))
+                      (setq variants (cdr variants)))
+                    ret))
+             (unless (string-match "&MCS-\\([0-9A-F]+\\);" ret)
+               (goto-char start)
+               (delete-region start end)
+               (insert ret))
+             )
+            ((setq ret (or (www-char-feature char 'ideographic-combination)
+                           (www-char-feature char 'ideographic-structure)))
+             (setq ret
+                   (mapconcat
+                    (lambda (ch)
+                      (if (listp ch)
+                          (if (characterp (setq rret (find-char ch)))
+                              (setq ch rret)))
+                      (if (characterp ch)
+                          (www-format-encode-string
+                           (char-to-string ch) without-tags)
+                        (www-format-encode-string
+                         (format "%S" ch) without-tags)))
+                    ret ""))
+             (when ret
+               (goto-char start)
+               (delete-region start end)
+               (insert ret))
+             )))
          ))
       ;; (goto-char (point-min))
       ;; (while (search-forward "&GT-" nil t)
     (setq format (plist-get props :format)))
   (concat "%"
          (plist-get props :flag)
-         (if (plist-get props :zero-padding)
-             "0")
+          ;; (if (plist-get props :zero-padding)
+          ;;     "0")
          (if (plist-get props :len)
-             (format "%d" (plist-get props :len)))
+             (format "0%d" (plist-get props :len)))
          (cond
           ((eq format 'decimal) "d")
           ((eq format 'hex) "x")
 (defun www-format-apply-value (object feature-name
                                      format props value
                                      &optional uri-char uri-feature
-                                     without-tags)
+                                     without-tags without-edit)
   (let (ret)
     (setq ret
          (cond
           ((eq format 'wiki-text)
            (if without-tags
                (www-xml-format-list value)
-             (www-format-eval-list value object feature-name nil uri-char))
+             (www-format-eval-list value object feature-name nil uri-char
+                                   without-tags without-edit))
            )
           ((eq format 'S-exp)
            (www-format-encode-string
             without-tags))
           ((eq format 'ku-ten)
            (www-format-value-as-kuten value))
+          ((eq format 'kangxi-radical)
+           (www-format-value-as-kangxi-radical value))
           ((eq format 'space-separated-char-list)
            (www-format-value-as-char-list value without-tags))
           ((eq format 'space-separated-ids)
            (www-format-value-as-ids value without-tags))
+          ((eq format 'space-separated-domain-list)
+           (www-format-value-as-domain-list value without-tags))
+          ((eq format 'string)
+           (www-format-encode-string (format "%s" value) without-tags)
+           )
           (t
-            ;; (setq format 'default)
-           (www-format-encode-string
-            (format (www-format-props-to-string props 'default)
-                    value)
-            without-tags))))
-    (if (or without-tags (eq (plist-get props :mode) 'peek))
+           (www-format-value-default value without-tags)
+            ))
+         )
+    (if (or without-tags
+           without-edit
+           (eq (plist-get props :mode) 'peek))
        ret
       (format "%s <a href=\"%s?char=%s&feature=%s&format=%s\"
 ><input type=\"submit\" value=\"edit\" /></a>"
 
 (defun www-format-eval-feature-value (char
                                      feature-name
-                                     &optional format lang uri-char value)
+                                     &optional format lang uri-char value
+                                     without-tags without-edit)
   (unless value
     (setq value (www-char-feature char feature-name)))
   (unless format
     (www-format-apply-value
      char feature-name
      format nil value
-     uri-char (www-uri-encode-feature-name feature-name))
+     uri-char (www-uri-encode-feature-name feature-name)
+     without-tags without-edit)
     )
    ((consp format)
     (cond ((null (cdr format))
           (www-format-apply-value
            char feature-name
            (car format) (nth 1 format) value
-           uri-char (www-uri-encode-feature-name feature-name))
+           uri-char (www-uri-encode-feature-name feature-name)
+           without-tags without-edit)
           )
          (t
-          (www-format-eval-list format char feature-name lang uri-char)
+          (www-format-eval-list format char feature-name lang uri-char
+                                without-tags without-edit)
           )))))
 
 (defun www-format-eval-unit (exp char feature-name
-                                &optional lang uri-char value)
+                                &optional lang uri-char value
+                                without-tags without-edit)
   (unless value
     (setq value (www-char-feature char feature-name)))
   (unless uri-char
    ((null exp) "")
    ((consp exp)
     (cond
-     ((memq (car exp) '(value decimal hex HEX ku-ten S-exp default))
+     ((memq (car exp) '(value decimal hex HEX ku-ten kangxi-radical
+                             S-exp string default))
       (if (eq (car exp) 'value)
          (www-format-eval-feature-value char feature-name
                                         (plist-get (nth 1 exp) :format)
-                                        lang uri-char value)
+                                        lang uri-char value
+                                        without-tags without-edit)
        (www-format-apply-value
         char feature-name
         (car exp) (nth 1 exp) value
-        uri-char (www-uri-encode-feature-name feature-name)))
+        uri-char (www-uri-encode-feature-name feature-name)
+        without-tags without-edit))
       )
      ((eq (car exp) 'name)
       (format "<a href=\"%s?feature=%s&char=%s\">%s</a>"
 >%s</a
 >"
              (www-format-eval-list (plist-get (nth 1 exp) :ref)
-                                   char feature-name lang uri-char)
+                                   char feature-name lang uri-char
+                                   'without-tags 'without-edit)
              (www-format-eval-list (nthcdr 2 exp)
-                                   char feature-name lang uri-char)))
+                                   char feature-name lang uri-char
+                                   without-tags without-edit)))
      (t
       (format "<%s
 >%s</%s
 >"
              (car exp)
              (www-format-eval-list (nthcdr 2 exp) char feature-name
-                                   lang uri-char)
+                                   lang uri-char
+                                   without-tags without-edit)
              (car exp)))))))
 
 (defun www-format-eval-list (format-list char feature-name
-                                        &optional lang uri-char)
+                                        &optional lang uri-char
+                                        without-tags without-edit)
   (if (consp format-list)
       (mapconcat
        (lambda (exp)
-        (www-format-eval-unit exp char feature-name lang uri-char))
+        (www-format-eval-unit exp char feature-name lang uri-char
+                              nil without-tags without-edit))
        format-list "")
-    (www-format-eval-unit format-list char feature-name lang uri-char)))
+    (www-format-eval-unit format-list char feature-name lang uri-char
+                         nil without-tags without-edit)))
 
 
 ;;; @ XML generator
   (let (name props children ret)
     (cond
      ((stringp format-unit)
-      (www-format-encode-string format-unit 'without-tags)
-      ;; (mapconcat (lambda (c)
-      ;;              (cond
-      ;;               ((eq c ?<) "&amp;lt;")
-      ;;               ((eq c ?>) "&amp;gt;")
-      ;;               (t
-      ;;                (char-to-string c))))
-      ;;            format-unit "")
+      (mapconcat (lambda (c)
+                  (cond
+                   ((eq c ?&) "&amp;")
+                    ;; ((eq c ?<) "&amp;lt;")
+                    ;; ((eq c ?>) "&amp;gt;")
+                   (t
+                    (char-to-string c))))
+                (www-format-encode-string format-unit 'without-tags)
+                "")
       )
      ((consp format-unit)
       (setq name (car format-unit)
   (www-html-display-text text)
   (princ "</p>\n"))
 
+
+;;; @ for GlyphWiki
+;;;
+
+(defvar coded-charset-GlyphWiki-id-alist
+  '((=ucs              "u"     4 x nil)
+    (=ucs@JP           "u"     4 x nil)
+    (=ucs@jis          "u"     4 x nil)
+    (=ucs@gb           "u"     4 x "-g")
+    (=ucs@cns          "u"     4 x "-t")
+    (=ucs@ks           "u"     4 x "-k")
+    (=ucs@iso          "u"     4 x "-u")
+    (=ucs@unicode      "u"     4 x "-us")
+    (=adobe-japan1-6   "aj1-"  5 d nil)
+    (=gt               "gt-"   5 d nil)
+    (=big5-cdp         "cdp-"  4 x nil)
+    (=cbeta            "cb"    5 d nil)
+    (=jis-x0208@1978/1pr "j78-"        4 x nil)
+    (=jis-x0208@1978/-4pr "j78-" 4 x nil)
+    (=jis-x0208@1978   "j78-"  4 x nil)
+    (=jis-x0208@1983   "j83-"  4 x nil)
+    (=jis-x0208@1990   "j90-"  4 x nil)
+    (=jis-x0212                "jsp-"  4 x nil)
+    (=jis-x0213-1@2000 "jx1-2000-" 4 x nil)
+    (=jis-x0213-1@2004 "jx1-2004-" 4 x nil)
+    (=jis-x0213-2      "jx2-"  4 x nil)
+    (=cns11643-1       "c1-"   4 x nil)
+    (=cns11643-2       "c2-"   4 x nil)
+    (=cns11643-3       "c3-"   4 x nil)
+    (=cns11643-4       "c4-"   4 x nil)
+    (=cns11643-5       "c5-"   4 x nil)
+    (=cns11643-6       "c6-"   4 x nil)
+    (=cns11643-7       "c7-"   4 x nil)
+    (=daikanwa         "dkw-"  5 d nil)
+    (=gt-k             "gt-k"  5 d nil)
+    (=jef-china3       "jc3-"  4 x nil)
+    (=big5             "b-"    4 x nil)
+    (=ks-x1001         "k0-"   4 x nil)
+    ))
+
+(defun char-GlyphWiki-id (char)
+  (let ((rest coded-charset-GlyphWiki-id-alist)
+       spec ret code)
+    (while (and rest
+               (setq spec (pop rest))
+               (null (setq ret (char-feature char (car spec))))))
+    (when ret
+      (or
+       (and (memq (car spec) '(=ucs@unicode '=ucs@iso))
+           (cond
+            ((and (or (encode-char char '=jis-x0208@1990)
+                      (encode-char char '=jis-x0212)
+                      (encode-char char '=jis-x0213-1))
+                  (setq code (encode-char char '=ucs@jis)))
+             (format "u%04x" code)
+             )
+            ((and (or (encode-char char '=gb2312)
+                      (encode-char char '=gb12345))
+                  (setq code (encode-char char '=ucs@gb)))
+             (format "u%04x-g" code)
+             )
+            ((and (or (encode-char char '=cns11643-1)
+                      (encode-char char '=cns11643-2)
+                      (encode-char char '=cns11643-3)
+                      (encode-char char '=cns11643-4)
+                      (encode-char char '=cns11643-5)
+                      (encode-char char '=cns11643-6)
+                      (encode-char char '=cns11643-7))
+                  (setq code (encode-char char '=ucs@cns)))
+             (format "u%04x-t" code)
+             )
+            ((and (encode-char char '=ks-x1001)
+                  (setq code (encode-char char '=ucs@ks)))
+             (format "u%04x-k" code)
+             )))
+       (format (format "%s%%0%d%s%s"
+                      (nth 1 spec)
+                      (nth 2 spec)
+                      (nth 3 spec)
+                      (or (nth 4 spec) ""))
+              ret)))))
+
+
+;;; @ End.
+;;;
+
 (provide 'cwiki-common)
+
+;;; cwiki-common.el ends here