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