;;; File:
;;;   greeting.ss
;;; Version:
;;;   1.2 of February 2002
;;; Author:
;;;   Samuel A. Rebelsky
;;; Contents:
;;;   A simple example of some Scheme procedures for CGI
;;; Organization:
;;;   Preparation - Stuff I need to do to get started
;;;   Sam's HTML Helpers - Procedures for generating HTML
;;;   Page Generators - Procedures for generating specific kinds of
;;;     pages (when given particular values)
;;;   Interface - Stuff to extract important values and then
;;;     call the page generators.  You'll find (page) here.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Preparation

;;; Look!  We're going to use Sam's Web Utilities!
(load "/home/rebelsky/Web/Scheme/webutils.ss")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Page Generators

;;; Procedure
;;;   greeting-page
;;; Parameters:
;;;   name, a string that represents the name of a user
;;; Purpose:
;;;   Generates an HTML page that can greet the named person.
;;; Produces:
;;;   page-code, a string that corresponds to that page.
;;; Preconditions:
;;;   name must be nonempty.
;;; Postconditions:
;;;   page-code is valid HTML.
(define greeting-page
  (lambda (user-name)
    (make-page 
      (head "Greetings")
      (body (string-append
              (heading 1 "Welcome")
              (string #\newline)
              (paragraph (string-append "Hi " user-name "!")))))))

;;; Procedure
;;;   hate-page
;;; Parameters:
;;;   bozo, a string that represents the name of a user
;;; Purpose:
;;;   Generates an HTML page that rants about the named person.
;;; Produces:
;;;   page-code, a string that corresponds to that page.
;;; Preconditions:
;;;   bozo must be nonempty.
;;; Postconditions:
;;;   page-code is valid HTML.
(define hate-page
  (lambda (bozo)
    (make-page 
      (head (string-append "I Hate " bozo))
      (body (string-append
              (heading 1 "I Hate ...")
              (string #\newline)
              (paragraph (string-append
                          "I hate many things.  Most of all, I hate "
                          bozo ".  "
                          bozo " is one of the most annoying people "
                          "that I have ever met.")))))))

;;; Procedure
;;;   nice-page
;;; Parameters:
;;;   friend, a string that represents the name of a user
;;; Purpose:
;;;   Generates an HTML page that rants about the named person.
;;; Produces:
;;;   page-code, a string that corresponds to that page.
;;; Preconditions:
;;;   friend must be nonempty.
;;; Postconditions:
;;;   page-code is valid HTML.
(define nice-page
  (lambda (friend)
    (make-page 
      (head (string-append "I Like " friend))
      (body (string-append
              (heading 1 (string-append "My Friend " friend))
              (string #\newline)
              (paragraph (string-append
                          "I like " friend ".  "
                          friend 
                          " is one of the nicest people you will ever meet.")))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interface

;;; Procedure:
;;;   page
;;; Parameters:
;;;   None
;;; Purpose:
;;;   Builds an HTML page according to specifications.
;;; Produces:
;;;   A string that corresponds to the HTML page.
;;; Preconditions:
;;;   CGI-variable user must be defined.
;;;   greeting-page must be defined, take one parameter, and generate a page.
;;; Postconditions:
;;;   The returned page is as valid as anything greeting-page produces.
(define page
  (lambda ()
    (greeting-page (get-cgi-variable 'user "SamR"))))
