simmone
simmone

Reputation: 379

Racket Scribble, How to add a searchbox?

I used scribble to generate my package's document.

And raco will integate my douments with racket self's documents, and add a searchbox on the topleft of the page.

You can see that when you use "raco docs".

Now I want use scribble as a document tool to generate my independent documents.

But when I use scribble --htmls demo.scribble, it can't have a searchbox ont the topleft of the page.

How to add the searchbox to let my document have the capability of search?

Upvotes: 4

Views: 470

Answers (1)

Leif Andersen
Leif Andersen

Reputation: 22332

There is a way to do this, but sadly, it's not currently particularly pleasant. If you want us to make it better, please submit an issue (or I guess pull request if you're feeling ambitious) on how you would like to be able to do this. (Also if it does get improved, someone please update this answer.)

Sadly, you cannot render a scribble page with a search box using the scribble command line app (nor using raco scribble). Rather, you need to use scribble's render function, with the html-render-mixin to render your document. Once you get over the hump of writing your own script to render your document though, it's fairly straightforward.

The class produced by html-render-mixin has an (undocumented as of Racket 6.5, I will add documentation for Racket 6.6) field: search-box?, that defaults to #f. You could build your own renderer mixin that extends the html one, except changes the search box to #t. You can do that like so:

#lang racket

(require (prefix-in html: scribble/html-render))

(define (search:render-mixin %)
  (class (html:render-mixin %)
    (init [search-box? #t])
    (super-new [search-box? search-box?])))

From there, you can pass this into the render function directly with the render-mixin keyword argument:

(require "webpage.scrbl")

(render (list doc)
        (list "webpage.html")
        #:render-mixin search:render-mixin)

Here, webpage.scrbl is the source for your file and webpage.html is your target location. They are lists so you can render multiple files at the same time.

The doc variable is coming from the webpage.scrbl. (For reference, scribble files, when compiled, define a variable called doc, which contains the content of your document.)

Also, make sure that webpage.scrbl is written in the scribble/manual language: #lang scribble/manual, otherwise you may not get a search box there.

When you run this file, webpage.html is generated with a search box. When you type into it and hit enter, it will go to search/index.html in that same folder, passing in your search query as an http parameter. As far as I can tell, Racket currently doesn't export how it builds the internal search index, but you can find how it does it in: pkgs/racket-index/scribblings/main/search.scrbl in the source code of the repo. If you would like that search page to come with scribble, please open an issue on github.

Upvotes: 2

Related Questions