New file.
[chise/est.git] / est-rdf-view.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'cwiki-format)
3
4 (defvar chise-wiki-view-url "view.cgi")
5 (defvar chise-wiki-edit-url "edit.cgi")
6 (defvar chise-wiki-add-url "add.cgi")
7
8 (defun est-rdf-encode-feature-name (feature-name)
9   (let ((str (symbol-name feature-name))
10         base domain
11         ret)
12     (if (string-match "@" str)
13         (setq base (substring str 0 (match-beginning 0))
14               domain (substring str (match-end 0)))
15       (setq base str))
16     (setq ret (mapconcat (lambda (c)
17                            (cond ((eq c ?*)
18                                   ".")
19                                  ((eq c ?/)
20                                   "-")
21                                  (t (char-to-string c))))
22                          base ""))
23     (if (eq (aref ret 0) ?.)
24         (setq ret (concat "meta" ret)))
25     (cons (if domain
26               (concat "est."
27                       (mapconcat #'identity
28                                  (split-string domain "/")
29                                  "."))
30             "est")
31           (www-uri-encode-feature-name (intern ret)))))
32
33 (defun est-rdf-format-object (obj)
34   (if (or (characterp obj)
35           (concord-object-p obj))
36       (let ((genre (est-object-genre obj))
37             (url-object (www-uri-encode-object obj)))
38         (format "
39     <rdf:Description
40      rdf:about=\"http://www.chise.org/est/rdf.cgi?%s=%s\">
41     </rdf:Description>"
42                 genre url-object))
43     (encode-coding-string (format "
44     %s" obj)
45                           'utf-8-mcs-er)))
46
47 (defun est-rdf-format-object-list (obj-list &optional with-li)
48   ;; (concat (mapconcat #'est-rdf-format-object
49   ;;                    obj-list
50   ;;                    "")
51   ;;         "\n")
52   (let ((rest obj-list)
53         dest obj)
54     (while (consp rest)
55       (setq obj (pop rest))
56       (if with-li
57           (setq dest (concat dest
58                              "
59     <rdf:li>"
60                              (est-rdf-format-object obj)
61                              "
62     </rdf:li>"))
63         (setq dest (concat dest (est-rdf-format-object obj)))))
64     (if rest
65         (setq dest (concat dest (est-rdf-format-object rest))))
66     (concat dest "\n  ")))
67
68 (defun est-rdf-display-object-desc (genre uri-object &optional lang level)
69   (unless level
70     (setq level 0))
71   (let ((object (www-uri-decode-object genre uri-object))
72         logical-feature chise-wiki-displayed-features
73         object-spec
74         rdf-feature-name rdf-feature-name-space
75         feature-type rdf-container
76         value ret)
77     (if (eq level 0)
78         (setq level 1))
79     (when object
80       (when (and (eq genre 'character)
81                  (= (length uri-object) 1))
82         (setq uri-object (www-uri-encode-object object)))
83       (when (eq genre 'character)
84         (dolist (feature (char-feature-property '$object 'additional-features))
85           (mount-char-attribute-table
86            (char-feature-name-at-domain feature '$rev=latest))))
87       (setq object-spec
88             (if (eq genre 'character)
89                 (char-attribute-alist object)
90               (concord-object-spec object)))
91       (princ
92        (format "<rdf:Description
93  rdf:about=\"http://www.chise.org/est/rdf.cgi/%s=%s\">\n"
94                genre uri-object))
95       (dolist (cell (sort object-spec
96                           (lambda (a b)
97                             (char-attribute-name<
98                              (char-feature-name-sans-versions (car a))
99                              (char-feature-name-sans-versions (car b))))))
100         (setq logical-feature (char-feature-name-sans-versions (car cell)))
101         (unless (memq logical-feature chise-wiki-displayed-features)
102           (push logical-feature chise-wiki-displayed-features)
103           (setq value (www-get-feature-value object logical-feature))
104           (setq ret (est-rdf-encode-feature-name logical-feature))
105           (setq rdf-feature-name (format "%s:%s" (car ret)(cdr ret)))
106           (setq rdf-feature-name-space
107                 (format "xmlns:%s=\"http://www.chise.org/est/rdf.cgi?domain=%s/\""
108                         (car ret)
109                         (car ret)))
110           (setq feature-type (www-feature-type logical-feature))
111           (if (and (consp value)
112                    (cdr value))
113               (cond
114                ((eq feature-type 'structure)
115                 (setq rdf-container "rdf:Seq")
116                 )
117                ;; ((eq feature-type 'relation)
118                ;;  (setq rdf-container "rdf:Bag")
119                ;;  )
120                (t
121                 (setq rdf-container "rdf:Bag")
122                 ))
123             (setq rdf-container nil))
124           (princ
125            (format "  <%s\n   %s>%s%s%s</%s>\n"
126                    rdf-feature-name
127                    rdf-feature-name-space
128                    (if rdf-container
129                        (format "\n  <%s>" rdf-container)
130                      "")
131                    (est-rdf-format-object-list value rdf-container)
132                    (if rdf-container
133                        (format "</%s>\n  " rdf-container)
134                      "")
135                    rdf-feature-name))
136           ))
137       (princ "</rdf:Description>")
138       )))
139
140 (defun est-rdf-display-feature-desc (uri-feature-name genre uri-object
141                                                   &optional lang simple)
142   (let ((feature-name (www-uri-decode-feature-name uri-feature-name))
143         (name@lang (intern (format "name@%s" lang))))
144     (princ
145      (encode-coding-string
146       (format "<head>
147 <title>EsT feature: %s</title>
148 </head>\n"
149               feature-name)
150       'utf-8-mcs-er))
151     (princ "<body>\n")
152     (princ
153      (format
154       (if simple
155           "<div style=\"text-align:right;\">
156 <a href=\"edit/view.cgi?feature=%s&%s=%s\">
157 <input type=\"submit\" value=\"Edit\" />
158 </a>
159 <input type=\"submit\" value=\"New Account\" />
160 </div>
161 <hr />\n"
162           "<div style=\"text-align:right;\">
163 <a href=\"../view.cgi?feature=%s&%s=%s\">
164 <input type=\"submit\" value=\"Simple\" />
165 </a>
166 </div>
167 <hr />\n")
168       uri-feature-name genre uri-object))
169     (princ
170      (format "<h1>%s</h1>\n"
171              (www-format-encode-string
172               (symbol-name feature-name))))
173     (princ (format "<p>name : %s "
174                    (or (www-format-feature-name feature-name) "")))
175     (unless simple
176       (princ
177        (format
178         " <a href=\"%s?feature=%s&property=name&format=string&%s=%s\">"
179         chise-wiki-edit-url
180         uri-feature-name
181         genre
182         uri-object))
183       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
184     (princ "</p>\n")
185     (when lang
186       (princ "<p>")
187       (princ
188        (www-format-encode-string
189         (format "%s : %s"
190                 name@lang
191                 (or (char-feature-property feature-name name@lang) ""))))
192       (unless simple
193         (princ
194          (format
195           " <a href=\"%s?feature=%s&property=%s&format=string&%s=%s\">"
196           chise-wiki-edit-url
197           uri-feature-name
198           name@lang
199           genre
200           uri-object))
201         (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
202       (princ "</p>\n"))
203     (www-html-display-paragraph
204      (format "type : %s"
205              (or (www-feature-type feature-name)
206                  ;; (char-feature-property feature-name 'type)
207                  'generic)))
208     (princ (format "<p>value-format : %s "
209                    (www-format-value
210                     nil 'value-format 
211                     (or (www-feature-value-format feature-name)
212                         'default)
213                     'default
214                     'without-tags)
215                    ))
216     (unless simple
217       (princ
218        (format
219         " <a href=\"%s?feature=%s&property=value-format&format=wiki-text&%s=%s\"
220 >"
221         chise-wiki-edit-url
222         uri-feature-name
223         genre
224         uri-object))
225       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
226     (princ "</p>\n")
227
228     (princ "<p>format : ")
229     (www-html-display-text
230      (decode-coding-string
231       (www-xml-format-list
232        (www-feature-format feature-name))
233       'utf-8-mcs-er))
234     (unless simple
235       (princ
236        (format
237         " <a href=\"%s?feature=%s&property=format&format=wiki-text&%s=%s\"
238 >"
239         chise-wiki-edit-url
240         uri-feature-name
241         genre
242         uri-object))
243       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
244     (princ "</p>\n")
245     
246     (www-html-display-paragraph
247      (format "description : %s"
248              (or (char-feature-property feature-name 'description)
249                  "")))
250     (when lang
251       (www-html-display-paragraph
252        (format "description@%s : %s"
253                lang
254                (or (char-feature-property
255                     feature-name
256                     (intern (format "description@%s" lang)))
257                    ""))))
258     ))
259   
260 (defun est-rdf-batch-view ()
261   (setq terminal-coding-system 'binary)
262   (condition-case err
263       (let* ((target (pop command-line-args-left))
264              (user (pop command-line-args-left))
265              (accept-language (pop command-line-args-left))
266              (mode (intern (pop command-line-args-left)))
267              (lang
268               (intern
269                (car (split-string
270                      (car (split-string
271                            (car (split-string accept-language ","))
272                            ";"))
273                      "-"))))
274              ret genre)
275         (princ "Content-Type: application/xml
276
277 <?xml version=\"1.0\" encoding=\"UTF-8\" ?>
278 <rdf:RDF
279   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
280   xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
281 ")
282         (cond
283          ((stringp target)
284           (when (string-match "^char=\\(&[^&;]+;\\)" target)
285             (setq ret (match-end 0))
286             (setq target
287                   (concat "char="
288                           (www-uri-encode-object
289                            (www-uri-decode-object
290                             'character (match-string 1 target)))
291                           (substring target ret))))
292           (setq target
293                 (mapcar (lambda (cell)
294                           (if (string-match "=" cell)
295                               (progn
296                                 (setq genre (substring cell 0 (match-beginning 0))
297                                       ret (substring cell (match-end 0)))
298                                 (cons
299                                  (intern
300                                   (decode-uri-string genre 'utf-8-mcs-er))
301                                  ret))
302                             (list (decode-uri-string cell 'utf-8-mcs-er))))
303                         (split-string target "&")))
304           (setq ret (car target))
305           (cond ((eq (car ret) 'char)
306                  (est-rdf-display-object-desc
307                   'character
308                   (cdr ret)
309                   lang nil)
310                  )
311                 ((eq (car ret) 'feature)
312                  (est-rdf-display-feature-desc
313                   (decode-uri-string (cdr ret) 'utf-8-mcs-er)
314                   (car (nth 1 target))
315                   (cdr (nth 1 target))
316                   lang
317                   (eq mode 'simple))
318                  )
319                 (t
320                  (est-rdf-display-object-desc
321                   (car ret)
322                   (cdr ret)
323                   lang nil)
324                  ))
325           ))
326         (princ "
327 </rdf:RDF>")
328         )
329     (error nil
330            (princ (format "%S" err)))
331     ))
332
333 (provide 'cwiki-view)