(www-display-object-desc): Check return value of
[chise/est.git] / cwiki-view.el
1 ;; -*- coding: utf-8-mcs-er -*-
2 (require 'cwiki-format)
3 (require 'char-db-json)
4
5 (defvar chise-wiki-view-url "view.cgi")
6 (defvar chise-wiki-edit-url "edit.cgi")
7 (defvar chise-wiki-add-url "add.cgi")
8
9 (defun www-edit-display-feature-input-box (char feature-name
10                                                 &optional format value)
11   (if (symbolp char)
12       (setq char (or (concord-decode-object '=id char 'feature)
13                      (concord-make-object 'feature char))))
14   (unless format
15     (setq format 'default))
16   (unless value
17     (setq value (www-get-feature-value char feature-name)))
18   (if (and (symbolp value)
19            (eq format 'wiki-text))
20       (setq value (list (list value))))
21   (princ
22    (format "<p><input type=\"text\" name=\"feature-name\"
23 size=\"32\" maxlength=\"256\" value=\"%s\">"
24            feature-name))
25   (princ (encode-coding-string " \u2190 " 'utf-8-mcs-er))
26   (princ
27    (format "%s<input type=\"text\" name=\"%s\"
28 size=\"64\" maxlength=\"256\" value=\"%s\">
29 <input type=\"submit\" value=\"set\" /></p>
30 "
31            (if (or (eq format 'HEX)(eq format 'hex))
32                "0x"
33              "")
34            format
35            (mapconcat (lambda (c)
36                         (cond
37                          ;; ((eq c ?<) "&amp;lt;")
38                          ;; ((eq c ?>) "&amp;gt;")
39                          ((eq c ?\u0022) "&quot;")
40                          (t
41                           (char-to-string c))))
42                       (est-format-list value format nil nil " ")
43                       "")))
44   )
45
46 (defun www-display-object-desc (genre uri-object &optional uri-feature-name
47                                       image-selection
48                                       lang level simple
49                                       uri-feature-name-to-edit editing-format)
50   (unless level
51     (setq level 0))
52   (let ((object (www-uri-decode-object genre uri-object))
53         (est-eval-list-feature-items-limit est-eval-list-feature-items-limit)
54         (est-view-url-prefix (if uri-feature-name
55                                  "../.."
56                                ".."))
57         (rdf-uri-object (if est-hide-cgi-mode
58                             (if (string-match "=" uri-object)
59                                 (concat
60                                  (est-uri-decode-feature-name-body
61                                   (substring uri-object 0 (match-beginning 0)))
62                                  ":"
63                                  (est-uri-decode-feature-name-body
64                                   (substring uri-object (match-end 0))))
65                               uri-object)))
66         feature-name-to-display feature-name-to-edit
67         base-name-to-edit metadata-name-to-edit
68         without-header
69         logical-feature chise-wiki-displayed-features
70         parents
71         GlyphWiki-id HNG-card HNG-card-id HNG-card-cobj ret object-spec
72         width height image-cobj base-image x y w h)
73     (if (eq level 0)
74         (setq level 1
75               without-header nil)
76       (setq without-header t))
77     (when object
78       (when uri-feature-name-to-edit
79         (setq feature-name-to-edit
80               (www-uri-decode-feature-name uri-feature-name-to-edit))
81         (setq ret (symbol-name feature-name-to-edit))
82         (if (string-match "\\*" ret)
83             (setq base-name-to-edit (intern
84                                      (substring ret 0 (match-beginning 0)))
85                   metadata-name-to-edit (intern
86                                          (substring ret (match-end 0))))
87           (setq base-name-to-edit feature-name-to-edit))
88         (when (stringp editing-format)
89           (setq editing-format (intern editing-format))))
90       (when (and (eq genre 'character)
91                  (= (length uri-object) 1))
92         (setq uri-object (www-uri-encode-object object)))
93       (when (= level 1)
94         (princ
95          (encode-coding-string
96           (format "<head>
97 <style type=\"text/css\">
98 <!--
99 hr { color: green; }
100 .ids { vertical-align: middle; font-size: 40px; line-height: 100%%; }
101 a { text-decoration: none; }
102 .ids a { color: black; }
103 ul { margin: 0 0; color: black; }
104 li { margin: 0 0 0 2em; }
105 .feature-name { font-family: sans-serif; font-weight: bold; }
106 .feature-name a { color: black; }
107
108 .tooltip {
109     position: relative;
110     display: inline-block;
111     border-bottom: 1px dotted black;
112 }
113
114 .tooltip .tooltiptext {
115     visibility: hidden;
116     width: 120px;
117     top: 100%%;
118     left: 50%%;
119     margin-left: -60px;
120     background-color: black;
121     color: #fff;
122     text-align: center;
123     padding: 5px 0;
124     border-radius: 6px;
125  
126     position: absolute;
127     z-index: 1;
128 }
129
130 .value .image-list img { height: 320px; vertical-align: top; }
131
132 .list .tooltip:hover .tooltiptext {
133     visibility: visible;
134 }
135 -->
136 </style>
137
138 <script
139  src=\"http://hng.chise.org/openseadragon/openseadragon.min.js\"></script>
140
141 <title>EsT %s = %s</title>
142 </head>\n"
143                   genre
144                   (decode-uri-string uri-object 'utf-8-mcs-er))
145           'utf-8-mcs-er))
146         (princ "<body>\n"))
147       (when (eq genre 'character)
148         (dolist (feature (char-feature-property '$object 'additional-features))
149           (mount-char-attribute-table
150            (char-feature-name-at-domain feature '$rev=latest))))
151       (princ
152        (if simple
153            (format
154             (if est-hide-cgi-mode
155                 "<div style=\"text-align:right;\">
156 <a href=\"../../edit/view.cgi?%s=%s\">
157 <input type=\"submit\" value=\"Edit\" />
158 </a>\n"
159               "<div style=\"text-align:right;\">
160 <a href=\"edit/view.cgi?%s=%s\">
161 <input type=\"submit\" value=\"Edit\" />
162 </a>\n")
163             genre rdf-uri-object)
164          (format
165           "<div style=\"text-align:right;\">
166 <a href=\"../view/%s/%s\">
167 <input type=\"submit\" value=\"Simple\" />
168 </a>\n"
169           genre
170           (if (string-match ":" uri-object)
171               (concat
172                (est-uri-encode-feature-name-body
173                 (substring uri-object 0 (match-beginning 0)))
174                "="
175                (est-uri-encode-feature-name-body
176                 (substring uri-object (match-end 0))))))))
177       (princ
178        (format "<input type=\"submit\" value=\"New Account\" />
179 <a href=\"http://www.chise.org/est/rdf.cgi?%s=%s\">
180 <input type=\"submit\" value=\"RDF\" />
181 </a>%s
182 </div>
183 <hr />\n"
184                genre rdf-uri-object
185                (if (eq genre 'character)
186                    (format "
187 <a href=\"/est/view/%s/%s/data.json\">
188 <input type=\"submit\" value=\"JSON\" />
189 </a>\n"
190                            genre rdf-uri-object)
191                  "")))
192       (when (setq parents (www-get-feature-value object '<-denotational))
193         (if (characterp parents)
194             (setq parents (list parents)))
195         (princ (format "<p>%s %s</p>\n<hr>\n"
196                        (www-format-value-as-char-list parents)
197                        (www-format-feature-name '->denotational lang))))
198       (when (setq parents (www-get-feature-value object '<-subsumptive))
199         (if (characterp parents)
200             (setq parents (list parents)))
201         (princ (format "<p>%s %s</p>\n<hr>\n"
202                        (www-format-value-as-char-list parents)
203                        (www-format-feature-name '->subsumptive lang))))
204       (when (eq genre 'character)
205         (setq GlyphWiki-id (char-GlyphWiki-id object)))
206       (cond
207        ((eq genre 'image-resource)
208         (princ
209          (if (setq ret (concord-object-get object '=location@iiif))
210              (if (setq base-image
211                        (car (concord-object-get object '<-image-segment)))
212                  (format "<a href=\"%s...$.zoom-xywh=%d,%d,%d,%d\"
213 ><img alt=\"%s\" src=\"%s\" /></a>"
214                          (www-uri-encode-object base-image)
215                          (concord-object-get object 'image-offset-x)
216                          (concord-object-get object 'image-offset-y)
217                          (concord-object-get object 'image-width)
218                          (concord-object-get object 'image-height)
219                          ret ret)
220                (if (and image-selection
221                         (string-match "\\([0-9]+\\),\\([0-9]+\\),\\([0-9]+\\),\\([0-9]+\\)" image-selection))
222                    (progn
223                      (setq x (string-to-int (match-string 1 image-selection))
224                            y (string-to-int (match-string 2 image-selection))
225                            w (string-to-int (match-string 3 image-selection))
226                            h (string-to-int (match-string 4 image-selection)))
227                      (setq width (float
228                                   (concord-object-get object 'image-width)))
229                      ;; (setq height (float
230                      ;;               (concord-object-get object 'image-height)))
231                      (format "<div id=\"openseadragon1\"
232  style=\"width: 800px; height: 600px;\"></div>
233
234 <script type=\"text/javascript\">
235     var viewer;
236     viewer = OpenSeadragon({
237         id: \"openseadragon1\",
238         prefixUrl: \"http://hng.chise.org/openseadragon/images/\",
239         preserveViewport:   true,
240         visibilityRatio:    1,
241         minZoomLevel:       1,
242         defaultZoomLevel:   1,
243         sequenceMode:       true,
244         tileSources:   [ \"%s/info.json\" ]
245     });
246     viewer.addHandler('open', function () {
247       var newBounds = new OpenSeadragon.Rect( %f, %f, %f, %f, 0 );
248       viewer.viewport.fitBounds(newBounds, true);
249       viewer.viewport.applyConstraints();
250     });
251 </script>
252 "
253                            ret
254                            (/ x width)
255                            (/ y width)
256                            (/ w width)
257                            (/ h width)
258                            ))
259                  (format "<div id=\"openseadragon1\"
260  style=\"width: 800px; height: 600px;\"></div>
261
262 <script type=\"text/javascript\">
263     OpenSeadragon({
264         id: \"openseadragon1\",
265         prefixUrl: \"http://hng.chise.org/openseadragon/images/\",
266         preserveViewport:   true,
267         visibilityRatio:    1,
268         minZoomLevel:       1,
269         defaultZoomLevel:   1,
270         sequenceMode:       true,
271         tileSources:   [ \"%s/info.json\" ]
272     });
273 </script>
274 "
275                          ret)))
276            (setq ret (concord-object-get object '=location))
277            (format "<img alt=\"%s\" src=\"%s\" />"
278                    ret ret)))
279         )
280        ((and (eq genre 'glyph-image)
281              (setq image-cobj
282                    (car (concord-object-get object '->image-resource))))
283         (if (and (setq ret (concord-object-get image-cobj '=location@iiif))
284                  (setq base-image
285                        (car (concord-object-get image-cobj '<-image-segment))))
286             (princ
287              (format "<a href=\"../image-resource/%s...$.zoom-xywh=%d,%d,%d,%d\"
288 ><img alt=\"%s\" src=\"%s\" /></a>"
289                      (www-uri-encode-object base-image)
290                      (concord-object-get image-cobj 'image-offset-x)
291                      (concord-object-get image-cobj 'image-offset-y)
292                      (concord-object-get image-cobj 'image-width)
293                      (concord-object-get image-cobj 'image-height)
294                      ret ret))
295           (setq ret (concord-object-get image-cobj '=location))
296           (princ (format "<img alt=\"%s\" src=\"%s\" />"
297                          ret ret)))
298         )
299        ((and (eq genre 'hng-card)
300              (setq ret (concord-object-get object '=hng-card))
301              (setq ret (symbol-name ret))
302              (string-match "\\([0-9]+\\)-\\([0-9]+\\)" ret))
303         (princ
304          (format
305           "<img alt=\"HNG-card:%s\"
306 src=\"http://hng.chise.org/images/HNG/%03d/card/%04d.jpg\"
307 style=\"height: 480px;\" />"
308           ret
309           (string-to-int (match-string 1 ret))
310           (string-to-int (match-string 2 ret))))
311         )
312        (t
313         (setq ret (www-format-encode-string
314                    (est-format-object object 'readable)))
315         (when (string-match
316                "<img alt=\"HNG\\([0-9]+\\)-\\([0-9]+\\)[a-z]?\"" ret)
317           (setq HNG-card-id
318                 (intern
319                  (format "%d-%d"
320                          (string-to-int (match-string 1 ret))
321                          (string-to-int (match-string 2 ret)))))
322           (setq HNG-card
323                 (format "hng-card/rep.id=%s" HNG-card-id)))
324         (princ
325          (format "<h%d>%s%s</h%d>\n"
326                  level
327                  (cond
328                   (uri-feature-name
329                    (format "<a href=\"%s\">%s</a>"
330                            ;; (if est-hide-cgi-mode
331                            ;;     "<a href=\"../%s\">%s</a>"
332                            ;;   "<a href=\"%s\">%s</a>")
333                            (www-uri-make-object-url object uri-object)
334                            ret)
335                    )
336                   (HNG-card
337                    (format
338                     "<a href=\"../%s\">%s</a>"
339                     HNG-card ret)
340                    )
341                   (t ret))
342                  (if GlyphWiki-id
343                      (format
344                       " <a href=\"http://glyphwiki.org/wiki/%s\"><img alt=\"%s\" src=\"http://glyphwiki.org/glyph/%s.50px.png\" /></a>"
345                       GlyphWiki-id
346                       GlyphWiki-id GlyphWiki-id)
347                    "")
348                  level))
349         ))
350       (if (> level 1)
351           (princ "<ul>"))
352       (when feature-name-to-edit
353         (princ "<form action=\"set.cgi\" method=\"GET\">\n")
354         (princ
355          (encode-coding-string
356           (format "<p>(%s : <input type=\"text\" name=\"%s\"
357 size=\"30\" maxlength=\"30\" value=\"%s\">)</p>
358 "
359                   genre genre
360                   (decode-uri-string uri-object 'utf-8-mcs-er))
361           'utf-8-mcs-er)))
362       (when (and HNG-card-id
363                  (eq genre 'character)
364                  (setq HNG-card-cobj
365                        (concord-decode-object
366                         '=id HNG-card-id
367                         'hng-card)))
368         (put-char-attribute object 'sources@HNG/card (list HNG-card-cobj))
369         (if (setq ret (concord-object-get
370                        HNG-card-cobj
371                        '->glyph-image@zinbun/takuhon))
372             (put-char-attribute object 'sources@zinbun/takuhon ret))
373         )
374       (setq object-spec
375             (cond
376              (uri-feature-name
377               (setq feature-name-to-display
378                     (www-uri-decode-feature-name uri-feature-name))
379               (setq est-eval-list-feature-items-limit nil)
380               (list
381                (cons feature-name-to-display
382                      (if (eq genre 'character)
383                          (get-char-attribute object feature-name-to-display)
384                        (concord-object-get object feature-name-to-display)))))
385              (t
386               (if (eq genre 'character)
387                   (char-attribute-alist object)
388                 (concord-object-spec object)))))
389       (when feature-name-to-edit
390         (unless (assq base-name-to-edit object-spec)
391           (setq object-spec (cons (cons base-name-to-edit nil)
392                                   object-spec))))
393       (dolist (cell (sort object-spec
394                           (lambda (a b)
395                             (char-attribute-name<
396                              (char-feature-name-sans-versions (car a))
397                              (char-feature-name-sans-versions (car b))))))
398         (setq logical-feature (char-feature-name-sans-versions (car cell)))
399         (unless (memq logical-feature chise-wiki-displayed-features)
400           (push logical-feature chise-wiki-displayed-features)
401           (cond
402            ((and feature-name-to-edit
403                  (eq (car cell) feature-name-to-edit))
404             (www-edit-display-feature-input-box
405              object feature-name-to-edit editing-format)
406             )
407            (t
408             (princ
409              (if (= level 1)
410                  "<div class=\"feature\" style=\"line-height:150%\">\n"
411                "<li>\n"))
412             (princ
413              (www-format-eval-list
414               (www-feature-format logical-feature)
415               object
416               logical-feature ; (car cell)
417               lang uri-object
418               nil simple))
419             (unless simple
420               (princ
421                (format " <a href=\"%s?%s=%s&feature=%s&format=wiki-text\"
422 ><input type=\"submit\" value=\"note\" /></a>"
423                        chise-wiki-edit-url
424                        genre
425                        (www-format-encode-string uri-object)
426                        (www-format-encode-string
427                         (www-uri-encode-feature-name
428                          (intern (format "%s*note"
429                                          logical-feature ; (car cell)
430                                          )))))))
431             (when (and feature-name-to-edit
432                        (eq base-name-to-edit (car cell)) metadata-name-to-edit)
433               (princ "<ul>\n")
434               (princ "<li>")
435               (www-edit-display-feature-input-box
436                object feature-name-to-edit editing-format)
437               (princ "</li>")
438               (princ "</ul>"))
439             (princ
440              (if (= level 1)
441                  "</div>\n"
442                "<li>\n"))
443             ))
444           ))
445       (princ
446        (if (= level 1)
447            "<p>\n"
448          "<li>\n"))
449       (when feature-name-to-edit
450         (princ "</form>\n"))
451       (unless simple
452         (princ
453          (format "<a href=\"%s?%s=%s\"
454 ><input type=\"submit\" value=\"add feature\" /></a>
455 "
456                  chise-wiki-add-url
457                  genre
458                  (www-format-encode-string uri-object))))
459       (princ
460        (if (= level 1)
461            "<p>\n"
462          "<li>\n"))
463       (when (eq genre 'character)
464         (princ
465          "<form action=\"http://www.chise.org/ids-find\">\n")
466         (princ
467          (www-format-encode-string
468           (est-format-object object)
469           ;; (if (eq genre 'character)
470           ;;     (format "%c" object)
471           ;;   (format "%s" (concord-object-id object)))
472           ))
473         (princ
474          (format
475           " <input type=\"text\" name=\"components\"
476 size=\"30\" maxlength=\"30\" value=\"%s\" />"
477           (encode-coding-string
478            (est-format-object object)
479            ;; (if (eq genre 'character)
480            ;;     (char-to-string object)
481            ;;   (format "%s" (concord-object-id object)))
482            'utf-8-jp-er)))
483         (princ
484          (www-format-encode-string
485           "を\u542Bむ\u6F22\u5B57を\u63A2す"))
486         (princ " <input type=\"submit\" value=\"search\" />\n")
487         (princ "</form>\n")
488
489         (princ
490          "<form action=\"http://www.chise.org/hng-ids-find\">\n")
491         (princ
492          (www-format-encode-string
493           (est-format-object object)
494           ;; (if (eq genre 'character)
495           ;;     (format "%c" object)
496           ;;   (format "%s" (concord-object-id object)))
497           ))
498         (princ
499          (format
500           " <input type=\"text\" name=\"components\"
501 size=\"30\" maxlength=\"30\" value=\"%s\" />"
502           (encode-coding-string
503            (est-format-object object)
504            ;; (if (eq genre 'character)
505            ;;     (char-to-string object)
506            ;;   (format "%s" (concord-object-id object)))
507            'utf-8-jp-er)))
508         (princ
509          (www-format-encode-string
510           "を\u542Bむ HNG の\u6F22\u5B57を\u63A2す"))
511         (princ " <input type=\"submit\" value=\"search\" />\n")
512         (princ "</form>\n")
513         )
514       (princ
515        (if (= level 1)
516            "</p>\n"
517          "<li>\n"))
518       )))
519
520 (defun www-display-feature-desc (uri-feature-name genre uri-object
521                                                   &optional lang simple)
522   (let ((rdf-uri-object (if est-hide-cgi-mode
523                             (if (string-match "=" uri-object)
524                                 (concat
525                                  (est-uri-decode-feature-name-body
526                                   (substring uri-object 0 (match-beginning 0)))
527                                  ":"
528                                  (est-uri-decode-feature-name-body
529                                   (substring uri-object (match-end 0))))
530                               uri-object)))
531         (feature-name (www-uri-decode-feature-name uri-feature-name))
532         (name@lang (intern (format "name@%s" lang))))
533     (princ
534      (encode-coding-string
535       (format "<head>
536 <title>EsT feature: %s</title>
537 </head>\n"
538               feature-name)
539       'utf-8-mcs-er))
540     (princ "<body>\n")
541     (princ
542      (if simple
543          (format
544           (if est-hide-cgi-mode
545               "<div style=\"text-align:right;\">
546 <a href=\"../../../edit/view.cgi?feature=%s&%s=%s\">
547 <input type=\"submit\" value=\"Edit\" />
548 </a>
549 <input type=\"submit\" value=\"New Account\" />
550 </div>
551 <hr />\n"
552             "<div style=\"text-align:right;\">
553 <a href=\"edit/view.cgi?feature=%s&%s=%s\">
554 <input type=\"submit\" value=\"Edit\" />
555 </a>
556 <input type=\"submit\" value=\"New Account\" />
557 </div>
558 <hr />\n")
559           uri-feature-name genre rdf-uri-object)
560        (format
561         "<div style=\"text-align:right;\">
562 <a href=\"../view/feature/%s&%s/%s\">
563 <input type=\"submit\" value=\"Simple\" />
564 </a>
565 </div>
566 <hr />\n"
567         uri-feature-name genre uri-object)))
568     (princ
569      (format "<h1>%s</h1>\n"
570              (www-format-encode-string
571               (symbol-name feature-name))))
572     (princ (format "<p>name : %s "
573                    (or (www-format-feature-name feature-name) "")))
574     (unless simple
575       (princ
576        (format
577         " <a href=\"%s?feature=%s&property=name&format=string&%s=%s\">"
578         chise-wiki-edit-url
579         uri-feature-name
580         genre
581         uri-object))
582       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
583     (princ "</p>\n")
584     (when lang
585       (princ "<p>")
586       (princ
587        (www-format-encode-string
588         (format "%s : %s"
589                 name@lang
590                 (or (char-feature-property feature-name name@lang) ""))))
591       (unless simple
592         (princ
593          (format
594           " <a href=\"%s?feature=%s&property=%s&format=string&%s=%s\">"
595           chise-wiki-edit-url
596           uri-feature-name
597           name@lang
598           genre
599           uri-object))
600         (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
601       (princ "</p>\n"))
602     (www-html-display-paragraph
603      (format "type : %s"
604              (or (www-feature-type feature-name)
605                  ;; (char-feature-property feature-name 'type)
606                  'generic)))
607     (princ (format "<p>value-format : %s "
608                    (www-format-value
609                     nil 'value-format 
610                     (or (www-feature-value-format feature-name)
611                         'default)
612                     'default
613                     'without-tags)
614                    ))
615     (unless simple
616       (princ
617        (format
618         " <a href=\"%s?feature=%s&property=value-format&format=wiki-text&%s=%s\"
619 >"
620         chise-wiki-edit-url
621         uri-feature-name
622         genre
623         uri-object))
624       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
625     (princ "</p>\n")
626
627     (princ (format "<p>value-presentation-format : %s "
628                    (www-format-value
629                     nil 'value-presentation-format 
630                     (or (www-feature-value-format feature-name)
631                         'default)
632                     'default
633                     'without-tags)
634                    ))
635     (unless simple
636       (princ
637        (format
638         " <a href=\"%s?feature=%s&property=value-presentation-format&format=wiki-text&%s=%s\"
639 >"
640         chise-wiki-edit-url
641         uri-feature-name
642         genre
643         uri-object))
644       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
645     (princ "</p>\n")
646
647     (princ "<p>format : ")
648     (www-html-display-text
649      (decode-coding-string
650       (www-xml-format-list
651        (www-feature-format feature-name))
652       'utf-8-mcs-er))
653     (unless simple
654       (princ
655        (format
656         " <a href=\"%s?feature=%s&property=format&format=wiki-text&%s=%s\"
657 >"
658         chise-wiki-edit-url
659         uri-feature-name
660         genre
661         uri-object))
662       (princ "<input type=\"submit\" value=\"edit\" /></a>\n"))
663     (princ "</p>\n")
664     
665     (www-html-display-paragraph
666      (format "description : %s"
667              (or (decode-coding-string
668                   (char-feature-property feature-name 'description)
669                   'utf-8-mcs-er)
670                  "")))
671     (when lang
672       (www-html-display-paragraph
673        (format "description@%s : %s"
674                lang
675                (or (char-feature-property
676                     feature-name
677                     (intern (format "description@%s" lang)))
678                    ""))))
679     ))
680   
681 (defun www-batch-view ()
682   (setq terminal-coding-system 'binary)
683   (condition-case err
684       (let* ((target (pop command-line-args-left))
685              (user (pop command-line-args-left))
686              (accept-language (pop command-line-args-left))
687              (mode (intern (pop command-line-args-left)))
688              (lang
689               (intern
690                (car (split-string
691                      (car (split-string
692                            (car (split-string accept-language ","))
693                            ";"))
694                      "-"))))
695              ret genre)
696         (princ "Content-Type: text/html; charset=UTF-8
697
698 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
699             \"http://www.w3.org/TR/html4/loose.dtd\">
700 <html lang=\"ja\">
701 ")
702         (cond
703          ((stringp target)
704           (when (string-match "^char=\\(&[^&;]+;\\)" target)
705             (setq ret (match-end 0))
706             (setq target
707                   (concat "char="
708                           (www-uri-encode-object
709                            (www-uri-decode-object
710                             'character (match-string 1 target)))
711                           (substring target ret))))
712           (setq target
713                 (mapcar (lambda (cell)
714                           (if (string-match "=" cell)
715                               (progn
716                                 (setq genre (substring cell 0 (match-beginning 0))
717                                       ret (substring cell (match-end 0)))
718                                 (cons
719                                  (intern
720                                   (decode-uri-string genre 'utf-8-mcs-er))
721                                  ret))
722                             (list (decode-uri-string cell 'utf-8-mcs-er))))
723                         (split-string target "&")))
724           (setq ret (car target))
725           (cond ((eq (car ret) 'char)
726                  (www-display-object-desc
727                   'character (cdr ret) (cdr (assq 'feature target))
728                   nil
729                   lang nil
730                   (eq mode 'simple))
731                  )
732                 ((eq (car ret) 'feature)
733                  (www-display-feature-desc
734                   (decode-uri-string (cdr ret) 'utf-8-mcs-er)
735                   (car (nth 1 target))
736                   (cdr (nth 1 target))
737                   lang
738                   (eq mode 'simple))
739                  )
740                 (t
741                  (www-display-object-desc
742                   (car ret) (cdr ret) (cdr (assq 'feature target))
743                   nil
744                   lang nil
745                   (eq mode 'simple))
746                  ))
747           ))
748         (princ "\n<hr>\n")
749         (princ (format "mode=%S\n" mode))
750         (princ (format "user=%s\n" user))
751         ;; (princ (format "local user=%s\n" (user-login-name)))
752         (princ (format "lang=%S\n" lang))
753         (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
754         ;; (princ " CHISE ")
755         ;; (princ xemacs-chise-version)
756         (princ "
757 </body>
758 </html>")
759         )
760     (error nil
761            (princ (format "%S" err)))
762     ))
763
764 (defun www-batch-view-smart ()
765   (setq debug-on-error t)
766   (setq terminal-coding-system 'binary)
767   (condition-case err
768       (let* ((est-hide-cgi-mode t)
769              (target (pop command-line-args-left))
770              (user (pop command-line-args-left))
771              (accept-language (pop command-line-args-left))
772              (mode (intern (pop command-line-args-left)))
773              (lang
774               (intern
775                (car (split-string
776                      (car (split-string
777                            (car (split-string accept-language ","))
778                            ";"))
779                      "-"))))
780              ret genre feature obj-url json obj)
781         (cond
782          ((stringp target)
783           (when (string-match "/data\\.json$" target)
784             (setq json t
785                   target (substring target 0 (match-beginning 0))))
786           (when (string-match "^char/\\(&[^&;]+;\\)" target)
787             (setq ret (match-end 0))
788             (setq target
789                   (concat "char/"
790                           (www-uri-encode-object
791                            (www-uri-decode-object
792                             'character (match-string 1 target)))
793                           (substring target ret))))
794           (setq target
795                 (mapcar
796                  (lambda (cell)
797                    (if (string-match "/" cell)
798                        (progn
799                          (setq genre (substring cell 0 (match-beginning 0))
800                                ret (substring cell (match-end 0)))
801                          (cons
802                           (intern (decode-uri-string genre 'utf-8-mcs-er))
803                           (cond
804                            ((string-match "/feature=" ret)
805                             (list (substring ret 0 (match-beginning 0))
806                                   (substring ret (match-end 0)))
807                             )
808                            ((string-match "...$.zoom-xywh=" ret)
809                             (list (substring ret 0 (match-beginning 0))
810                                   nil
811                                   (substring ret (match-end 0)))
812                             )
813                            (t
814                             (list ret)))))
815                      (list (decode-uri-string cell 'utf-8-mcs-er)))
816                    ;; (setq ret (split-string cell "/"))
817                    ;; (cons (intern
818                    ;;        (decode-uri-string (car ret) 'utf-8-mcs-er))
819                    ;;       (cdr ret))
820                    )
821                  (split-string target "&")))
822           (setq ret (car target))
823           (if json
824               (princ "Content-Type: application/json; charset=UTF-8
825
826 ")
827             (princ "Content-Type: text/html; charset=UTF-8
828
829 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
830             \"http://www.w3.org/TR/html4/loose.dtd\">
831 <html lang=\"ja\">
832 ")
833             )
834           ;; (princ (format "<p>%S, %S, %S</p>"
835           ;;                (car ret)(nth 1 ret)(nth 2 ret)))
836           ;; (princ (format "// %S %S\n" ret json))
837           (cond ((or (eq (car ret) 'char)
838                      (eq (car ret) 'character))
839                  (if (and json
840                           (setq obj (www-uri-decode-object
841                                      (car ret)(nth 1 ret)))
842                           (characterp obj))
843                      (with-temp-buffer
844                        ;; (princ (encode-coding-string
845                        ;;         (format "// %S\n" obj)
846                        ;;         char-db-file-coding-system))
847                        (char-db-json-char-data-with-variant obj 'printable)
848                        (encode-coding-region
849                         (point-min)(point-max)
850                         char-db-file-coding-system)
851                        (princ (buffer-string))
852                        )
853                    (www-display-object-desc
854                     'character (nth 1 ret) (nth 2 ret)
855                     nil
856                     lang nil
857                     (eq mode 'simple)))
858                  )
859                 ((eq (car ret) 'feature)
860                  (www-display-feature-desc
861                   (decode-uri-string (nth 1 ret) 'utf-8-mcs-er)
862                   (car (nth 1 target))
863                   (nth 1 (nth 1 target))
864                   lang
865                   (eq mode 'simple))
866                  )
867                 ;; ((eq (car ret) 'image-resource)
868                 ;; ;;  (cond
869                 ;; ;;   ((string-match "^\\.iiif=" (nth 1 ret))
870                 ;; ;;    (setq obj-url (decode-uri-string
871                 ;; ;;                   (substring (nth 1 ret) (match-end 0))
872                 ;; ;;                   'utf-8-mcs-er))
873                 ;; ;;    (setq obj (concord-images-add-iiif obj-url))
874                 ;; ;;    (www-display-object-desc
875                 ;; ;;     'image-resource
876                 ;; ;;     (www-uri-encode-object obj)
877                 ;; ;;     (nth 2 ret)
878                 ;; ;;     lang nil
879                 ;; ;;     (eq mode 'simple))
880                 ;; ;;    )
881                 ;; ;;   (t
882                 ;;  (princ (nth 1 ret))
883                 ;;  (www-display-object-desc
884                 ;;   'image-resource (nth 1 ret) (nth 2 ret)
885                 ;;   lang nil
886                 ;;   (eq mode 'simple))
887                 ;; ;;    ))
888                 ;;  )
889                 (t
890                  (www-display-object-desc
891                   (car ret) (nth 1 ret) (nth 2 ret)
892                   (nth 3 ret)
893                   lang nil
894                   (eq mode 'simple))
895                  ))
896           ))
897         (unless json
898           (princ "\n<hr>\n")
899           (princ (format "mode=%S\n" mode))
900           (princ (format "user=%s\n" user))
901           ;; (princ (format "local user=%s\n" (user-login-name)))
902           (princ (format "lang=%S\n" lang))
903           (princ (encode-coding-string (emacs-version) 'utf-8-jp-er))
904           ;; (princ " CHISE ")
905           ;; (princ xemacs-chise-version)
906           (princ "
907 </body>
908 </html>")
909           )
910         )
911     (error nil
912            (princ (format "%S" err)))
913     ))
914
915 (provide 'cwiki-view)