Debian Codegraph @

doc/man.cgi

raw
1 #!/home/shiro/bin/gosh
2 ;; -*- coding:utf-8 -*-
3
4 ;;; This script is used in practical-scheme.net/gauche/man/ to redirect
5 ;;; GET request with p=<topic> query to appropriate page.
6 ;;; <topic> can be a procedure/syntax/macro/variable name or a node name.
7
8 (use file.util)
9 (use gauche.charconv)
10 (use gauche.lazy)
11 (use srfi.13)
12 (use text.html-lite)
13 (use util.match)
14 (use www.cgi)
15
16 (define (pick-from-file rx file)
17 (call-with-input-file file
18 (^p (any (^l (and-let1 m (rx l) (m 1))) (port->string-lseq p)))))
19
20 (define (multi-pick-from-file rx file)
21 (call-with-input-file file
22 (^p (filter-map (^l (and-let1 m (rx l) (m 1))) (port->string-lseq p)))))
23
24 (define (pick-initial initial file)
25 (pick-from-file
26 (string->regexp #`"<A class=\"(?:.*?)\" HREF=\"(.*?)\"><b>,(regexp-quote initial)</b></A>" :case-fold #t)
27 file))
28
29 (define (pick-index-item item file)
30 (pick-from-file
31 (string->regexp #`"<A HREF=\"(.*?)\">(?:<CODE>)+,(regexp-quote item)(?:</CODE>)+</A>" :case-fold #t)
32 file))
33
34 (define (file-part path)
35 (cond [(string-scan path "#" 'before)]
36 [else path]))
37
38 (define (file-path lang filename)
39 (build-path (if (memq lang '(ja jp)) "gauche-refj" "gauche-refe") filename))
40
41 (define (get-indexed-uri lang index-file name)
42 (and-let* ([index-page (pick-initial (string-upcase (string-take name 1))
43 (file-path lang index-file))]
44 [target-file (let1 p (file-part index-page)
45 (if (string-null? p)
46 (file-part index-file)
47 p))])
48 (pick-index-item name (file-path lang target-file))))
49
50 (define (get-index-pages lang)
51 (let1 rx (if (eq? lang 'en)
52 #/<A HREF=\"([^\"]*)\">.*(?:Function and Syntax Index|Module Index|Class Index|Variable Index)<\/A>/i
53 #/<A HREF=\"([^\"]*)\">.*(?:Index - 手続きと構文索引|Index - モジュール索引|Index - クラス索引|Index - 変数索引)<\/A>/i)
54 ($ map file-part
55 $ multi-pick-from-file rx (file-path lang "index.html"))))
56
57 (define (search-from-index lang name)
58 (match-let1 (fn md cl va) (get-index-pages lang)
59 (or (get-indexed-uri lang fn name) ;; from function
60 (get-indexed-uri lang md name) ;; from module
61 (and-let1 m (#/^&lt\;(.*)&gt\;$/ name)
62 (get-indexed-uri lang cl (m 1))) ;; from class
63 (get-indexed-uri lang va name) ;; from variable
64 )))
65
66 (define (search-from-toc lang name)
67 (let1 picker #/<A NAME=\"toc-[^\"]*\" HREF=\"([^\"]*)\">\d+\.[.\d]*\s+(.*?)<\/A>/i
68 (call-with-input-file (file-path lang "index.html")
69 (^p (any (^l (and-let1 m (picker l)
70 (let1 sectitle (regexp-replace-all #/<\/?CODE>/i (m 2) "")
71 (and (equal? name sectitle)
72 (m 1)))))
73 (port->string-lseq p))))))
74
75 (define (main args)
76 (cgi-main
77 (^[params]
78 (let* ([lang (cgi-get-parameter "l" params
79 :default 'en :convert string->symbol)]
80 [enc (cgi-get-parameter "en" params)]
81 [raw-name (cgi-get-parameter "p" params)]
82 [name (html-escape-string
83 (cond [(and enc (ces-conversion-supported? enc #f))
84 (ces-convert raw-name enc)]
85 [(eq? lang 'jp)
86 (ces-convert raw-name "*JP")]
87 [else raw-name]))]
88 [uri (file-path lang
89 (or (and name
90 (positive? (string-length name))
91 (if (string-every #[\x21-\x7e] name)
92 (or (search-from-index lang name)
93 (search-from-toc lang name))
94 (search-from-toc lang name)))
95 "index.html"))])
96 `("Status: 302 Moved\n" ,(cgi-header :location uri))))))
97
98 ;; Local variables:
99 ;; mode: scheme
100 ;; end: