1 Introduction
2 Static Pages
static-page
static-page?
static-page-path
3 Sites
build-site
site?
save-site
Version: 4.1.5.3

Static Page: Developing Static Web Pages

by Dave Herman <dherman at ccs dot neu dot edu>

This library provides a simple abstraction for building static web content that can be rapidly developed and tested, and then exported to raw HTML.

 (require (planet dherman/static-page:1:0))

    1 Introduction

    2 Static Pages

    3 Sites

1 Introduction

This package is intended to ease the development of web sites that contain only static pages. This is useful for generating sites with multiple pages that share common structure. In this case, while the full power of dynamic servlets may not be necessary, the benefits of building templates and abstractions are still important.

Moreover, PLT Scheme and the PLT Web Server are fantastic for rapidly developing and debugging a web site – X-expressions, quasiquote and unquote, and of course data and functional abstraction are all simple and powerful tools for building web pages. Even better, with serve/servlet it’s easy to test a web site from within DrScheme with a push of the Run button.

This library provides the last piece: converting that dynamically generated servlet into a static collection of HTML files.

Consider a simple example of a static web site with three pages:

  (define index
    (static-page "index.html"
      (lambda (embed-url)
        `(html (head (title "My Web Site"))
               (body (p "Check out my "
                        (a ([href ,(embed-url cv)]) "CV.")))))))
  (define cv
    (static-page "cv.html"
      (lambda (embed-url)
        `(html (head (title "My CV"))
               (body (p (a ([href ,(embed-url index)]) "Dave Herman"))
                     ,@(for/list ([job job-history])
                         (job->xexpr job)))))))
  (define (job->xexpr job) ...)

Notice how the pages are created with static-page instead of as explicit functions from request? to response/c. Pages built with static-page can behave like procedures that call send/suspend/dispatch, so we can actually treat this web site as a servlet and test it in DrScheme:

  (serve/servlet index)

But we can extract the HTML source of the site and save it to disk as well:

  (define my-site (build-site index))
  (save-site my-site #:root (build-path 'same "saved"))

2 Static Pages

(static-page path build-response)  static-page?
  path : string?
  build-response : ((static-page? -> string?) -> (or/c response/full? xexpr?))

Constructs a static page structure. The path is the address by which other pages in the site can link to this page. The build-response procedure generates a response, similar to regular servlets. Note, however, that incremental responses are unsupported. The argument to build-response is a procedure that produces the address of static pages in the site.

(static-page? x)  boolean?
  x : any

Determines whether x is a static page.

(static-page-path page)  string?
  page : static-page?

Returns the local address of page.

3 Sites

(build-site front-page)  site?
  front-page : static-page?

Builds all pages in a site, recursively following all local links and adding the generated pages to the site.

(site? x)  boolean?
  x : any

Determines whether x is a site.

(save-site site    
  [#:root root    
  #:exists exists-flag])  any
  site : site?
  root : path-string? = (current-directory)
  exists-flag : 
(or/c 'error 'append 'update
      'replace 'truncate 'trunace/replace)
   = 'error

Saves all the pages in site to the filesystem, based in the root directory. The optional exists-flag determines the behavior of call-with-output-file* in case any of the files already exist.