From 72829e1d1d2d7fa1cf8ebc91336231c2b86ed81c Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Wed, 30 Jan 2008 18:20:04 +0000 Subject: [PATCH] searchable index pages svn: r8477 original commit: b012e7dbe7cc0291ba2af8d22dd05e88f626c274 --- collects/scribble/basic.ss | 110 +++++++++++++++---------------- collects/scribble/html-render.ss | 87 +++++++++++++++++++++++- 2 files changed, 139 insertions(+), 58 deletions(-) diff --git a/collects/scribble/basic.ss b/collects/scribble/basic.ss index 2c37884e..cae8025e 100644 --- a/collects/scribble/basic.ss +++ b/collects/scribble/basic.ss @@ -208,64 +208,60 @@ (stringlist "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) + (define (rows . rows) + (make-table 'index (map (lambda (row) + (list (make-flow (list (make-paragraph row))))) + rows))) + (define line-break (make-element 'newline '("/n"))) (define contents - (make-delayed-flow-element - (lambda (renderer sec ri) - (define l null) - (define alpha-starts (make-hash-table)) - (define (rows . rows) - (make-table 'index - (map (lambda (row) - (list (make-flow (list (make-paragraph row))))) - rows))) - (hash-table-for-each - (let ([parent (collected-info-parent (part-collected-info sec ri))]) - (if parent - (collected-info-info (part-collected-info parent ri)) - (collect-info-ext-ht (resolve-info-ci ri)))) - (lambda (k v) - (when (and (pair? k) (eq? 'index-entry (car k))) - (set! l (cons (cons (cadr k) v) l))))) - (set! l (sort l cadr-string-lists? letter (car alpha)) - (add-letter (car alpha) (loop i (cdr alpha)))] - [(char-ci=? letter (car alpha)) - (hash-table-put! alpha-starts (car i) letter) - (list* (make-element - (make-target-url (format "#alpha:~a" letter) - #f) - (list (string (car alpha)))) - " " - (loop (cdr i) (cdr alpha)))] - [else (loop (cdr i) alpha)]))])) - (list 'nbsp) - ((if (null? l) values cdr) ; drop one 'newline - (apply append - (map (lambda (i) - (define e (make-link-element - "indexlink" (commas (caddr i)) (car i))) - (list (make-element 'newline '("/n")) - (cond [(hash-table-get alpha-starts i #f) - => (lambda (let) - (make-element - (make-url-anchor - (format "alpha:~a" (char-upcase let))) - (list e)))] - [else e]))) - l))))))) - (list contents)) + (lambda (renderer sec ri) + (define l null) + (define alpha-starts (make-hash-table)) + (hash-table-for-each + (let ([parent (collected-info-parent (part-collected-info sec ri))]) + (if parent + (collected-info-info (part-collected-info parent ri)) + (collect-info-ext-ht (resolve-info-ci ri)))) + (lambda (k v) + (when (and (pair? k) (eq? 'index-entry (car k))) + (set! l (cons (cons (cadr k) v) l))))) + (set! l (sort l cadr-string-lists? letter (car alpha)) + (add-letter (car alpha) (loop i (cdr alpha)))] + [(char-ci=? letter (car alpha)) + (hash-table-put! alpha-starts (car i) letter) + (list* (make-element + (make-target-url (format "#alpha:~a" letter) #f) + (list (string (car alpha)))) + " " + (loop (cdr i) (cdr alpha)))] + [else (loop (cdr i) alpha)]))])) + (list 'nbsp) + (map (lambda (i) + (define e + (make-link-element "indexlink" + `(,@(commas (caddr i)) ,line-break) + (car i))) + (cond [(hash-table-get alpha-starts i #f) + => (lambda (let) + (make-element (make-url-anchor + (format "alpha:~a" (char-upcase let))) + (list e)))] + [else e])) + l)))) + (list (make-delayed-flow-element contents))) ;; ---------------------------------------- diff --git a/collects/scribble/html-render.ss b/collects/scribble/html-render.ss index c7ff729c..68eefc47 100644 --- a/collects/scribble/html-render.ss +++ b/collects/scribble/html-render.ss @@ -66,6 +66,89 @@ (loop (substring s (cdar m)))))] [else s]))) + (define literal + (let ([loc (xml:make-location 0 0 0)]) + (lambda strings (xml:make-cdata loc loc (apply string-append strings))))) + (define (script . body) + `(script ((type "text/javascript")) + ,(apply literal + `("\n" + ,@(map (lambda (x) (if (string? x) x (format "~a" x))) body) + "\n")))) + + #reader scribble/reader + (define search-script + @script{ + var search_nodes = null; + var last_search_terms = null; + function node_to_text(node) { + if (node.nodeType == 3) return node.nodeValue; + var r = ""; + var children = node.childNodes; + for (var i=0@";" i