refactor blueboxes support code to avoid dependency on racket-index pkg

This commit is contained in:
Robby Findler 2015-04-12 16:34:32 -05:00
parent 1b4b474785
commit 9b026739ed
3 changed files with 35 additions and 21 deletions

View File

@ -2,7 +2,8 @@
@(require scribble/manual "utils.rkt" @(require scribble/manual "utils.rkt"
(for-label scribble/core (for-label scribble/core
scribble/blueboxes scribble/blueboxes
racket/contract)) racket/contract
setup/xref))
@title[#:tag "blueboxes"]{Blue Boxes Utilities} @title[#:tag "blueboxes"]{Blue Boxes Utilities}
@ -14,7 +15,7 @@
@defproc[(fetch-blueboxes-strs [tag tag?] @defproc[(fetch-blueboxes-strs [tag tag?]
[#:blueboxes-cache blueboxes-cache [#:blueboxes-cache blueboxes-cache
blueboxes-cache? blueboxes-cache?
(make-blueboxes-cache)]) (make-blueboxes-cache #t)])
(or/c #f (non-empty-listof string?))]{ (or/c #f (non-empty-listof string?))]{
Returns a list of strings that show the content of the blue box Returns a list of strings that show the content of the blue box
(without any styling information) for the documentation referenced (without any styling information) for the documentation referenced
@ -25,13 +26,21 @@
was used to document the export). was used to document the export).
} }
@defproc[(make-blueboxes-cache [populate? boolean?]) blueboxes-cache?]{ @defproc[(make-blueboxes-cache
[populate? boolean?]
[#:blueboxes-dirs blueboxes-dirs (listof path?) (get-doc-search-dirs)])
blueboxes-cache?]{
Constructs a new (mutable) blueboxes cache. Constructs a new (mutable) blueboxes cache.
If @racket[populate?] is @racket[#f], the cache is initially If @racket[populate?] is @racket[#f], the cache is initially
unpopulated, in which case it is filled in the first time the cache unpopulated, in which case it is filled in the first time the cache
is passed to @racket[fetch-bluebxoes-strs]. Otherwise, the cache is is passed to @racket[fetch-bluebxoes-strs]. Otherwise, the cache is
initially populated. populated immediately.
The @racket[blueboxes-dirs] argument is a list of directories that are
looked inside for @filepath{blueboxes.rktd} files. The default value
is only an approximation for where those files usually reside. See
also @racket[get-rendered-doc-directories].
} }
@defproc[(blueboxes-cache? [v any/c]) boolean?]{ @defproc[(blueboxes-cache? [v any/c]) boolean?]{

View File

@ -12,8 +12,7 @@
"at-exp-lib" "at-exp-lib"
"draw-lib" "draw-lib"
"syntax-color-lib" "syntax-color-lib"
"sandbox-lib" "sandbox-lib"))
"racket-index"))
(define build-deps '("rackunit-lib" (define build-deps '("rackunit-lib"
"eli-tester")) "eli-tester"))
@ -23,4 +22,4 @@
(define pkg-authors '(mflatt eli)) (define pkg-authors '(mflatt eli))
(define version "1.9") (define version "1.10")

View File

@ -1,5 +1,5 @@
#lang racket/base #lang racket/base
(require setup/xref (require setup/dirs
racket/serialize racket/serialize
racket/contract racket/contract
scribble/core) scribble/core)
@ -8,12 +8,14 @@
(contract-out (contract-out
[fetch-blueboxes-strs (->* (tag?) (#:blueboxes-cache blueboxes-cache?) [fetch-blueboxes-strs (->* (tag?) (#:blueboxes-cache blueboxes-cache?)
(or/c #f (non-empty-listof string?)))] (or/c #f (non-empty-listof string?)))]
[make-blueboxes-cache (-> boolean? blueboxes-cache?)] [make-blueboxes-cache (->* (boolean?) (#:blueboxes-dirs (listof path?)) blueboxes-cache?)]
[blueboxes-cache? (-> any/c boolean?)])) [blueboxes-cache? (-> any/c boolean?)]))
(struct blueboxes-cache (info) #:mutable) (struct blueboxes-cache (info-or-paths) #:mutable)
(define (make-blueboxes-cache populate?) (define (make-blueboxes-cache populate? #:blueboxes-dirs [blueboxes-dirs (get-doc-search-dirs)])
(blueboxes-cache (and populate? (build-blueboxes-cache)))) (define cache (blueboxes-cache blueboxes-dirs))
(when populate? (populate-cache! cache))
cache)
(define (fetch-blueboxes-strs tag #:blueboxes-cache [cache (make-blueboxes-cache #f)]) (define (fetch-blueboxes-strs tag #:blueboxes-cache [cache (make-blueboxes-cache #f)])
(define plain-strs (fetch-strs-for-single-tag tag cache)) (define plain-strs (fetch-strs-for-single-tag tag cache))
@ -33,9 +35,8 @@
plain-strs])) plain-strs]))
(define (fetch-strs-for-single-tag tag cache) (define (fetch-strs-for-single-tag tag cache)
(unless (blueboxes-cache-info cache) (populate-cache! cache)
(set-blueboxes-cache-info! cache (build-blueboxes-cache))) (for/or ([ent (in-list (blueboxes-cache-info-or-paths cache))])
(for/or ([ent (in-list (blueboxes-cache-info cache))])
(define offset+lens (hash-ref (list-ref ent 2) tag #f)) (define offset+lens (hash-ref (list-ref ent 2) tag #f))
(cond (cond
[offset+lens [offset+lens
@ -52,14 +53,19 @@
(read-line port))))))] (read-line port))))))]
[else #f]))) [else #f])))
(define (populate-cache! cache)
(define cache-content (blueboxes-cache-info-or-paths cache))
(when ((listof path?) cache-content)
(set-blueboxes-cache-info-or-paths! cache (build-blueboxes-cache cache-content))))
;; build-blueboxes-cache : (listof (list file-path int hash[tag -o> (cons int int)])) ;; build-blueboxes-cache : (listof (list file-path int hash[tag -o> (cons int int)]))
(define (build-blueboxes-cache) (define (build-blueboxes-cache blueboxes-dirs)
(filter (filter
values values
(for*/list ([doc-dir-name (in-list (get-rendered-doc-directories #f #f))]) (for*/list ([doc-dir-name (in-list blueboxes-dirs)])
(define x (build-path doc-dir-name "blueboxes.rktd")) (define blueboxes.rktd (build-path doc-dir-name "blueboxes.rktd"))
(and (file-exists? x) (and (file-exists? blueboxes.rktd)
(call-with-input-file x (call-with-input-file blueboxes.rktd
(λ (port) (λ (port)
(port-count-lines! port) (port-count-lines! port)
(define first-line (read-line port)) (define first-line (read-line port))
@ -72,6 +78,6 @@
#f)]) #f)])
(deserialize (read port)))) (deserialize (read port))))
(and desed (and desed
(list x (list blueboxes.rktd
(+ (string->number first-line) pos) (+ (string->number first-line) pos)
desed)))))))) desed))))))))