CS 2135: Lab 3


Lab Motivation and Goals

This lab is designed to help you practice

We'll do this by having you develop data definitions for a subset of HTML, then writing two kinds of programs: some to generate your HTML structures from a database of information for a record store, and some to generate actual HTML files from your definitions.

This lab uses some helper functions that I've provided in library lab3-lib.ss. Download the library to your machine (if you are in lab after 10am, it may be there already), then use the "Add Teachpack" option under the "Language" menu to load the library. After doing "Add Teachpack", you need to hit "Execute" to make the library functions available to you.

Warning: This is a long lab handout, and I don't expect most students will finish it (or even most of it). I left the page long so you can see the kinds of programs that you now know how to write (given more time). Don't get discouraged by the length -- if you make it to the "everybody should get here" point, you're doing fine.


Exercises: Modeling HTML

Consider a restricted subset of HTML expressions containing strings, numbers, lists, and links:

To see a sample HTML file with these constructs, view the source of the sample HTML file.

  1. Develop a data definition for this subset of HTML expressions.

  2. An HTML page consists of a title (string), a background color (string), and a list of HTML expressions that make up the page's contents. Develop a data definition for HTML pages.

  3. The following data definitions model the inventory of a music store:

          ;; A music-category is one of
          ;;   - 'classical
          ;;   - 'rock
          ;;   - 'blues
    
          ;; An album is a structure
          ;;  (make-album string string music-category)
          (define-struct album (artist title category))
    
          ;; A stock is a structure
          ;;  (make-stock album number number)
          (define-struct stock (album copies price))
    
          ;; An inventory is a list of stock
    

    We want to generate web pages for the music store using the data definitions you developed in the first two questions.

    Develop a program gen-stock-page which consumes a stock and produces an html-page structure. The page should contain the title followed by an itemized list containing the album artist, category, availability and price. For example, given the stock (make-stock (make-album "Beatles" "Abbey Road" 'rock) 4 14.95), the page structure that your program returns should be the same as you'd expect to capture this gen-stock sample page.

    Everybody should be able to finish up to this point

    If you don't care about writing your actual pages out to file so you can view them, you may skip to question 6 and eliminate parts of the latter questions that talk about files.

  4. We want to be able to write HTML pages out to file so that we can view them through a web browser. This proceeds in two stages: first, we need to convert an HTML page into a string, then we need to write that string out to a file.

    Write a function HTML-page-to-string, which consumes an HTML page (in your data definition) and returns a string representing that page. The string should wrap each HTML construct in it's appropriate set of tags. To see the set of tags, view the source of the gen-stock sample page or this other sample HTML file.

    In writing HTML-page-to-string, you'll probably find the following operations useful (all are built in or in the provided library -- you don't need to write them!):

  5. The library for this lab provides a function write-str-to-file, which consumes two strings and returns void. The first input string is the string to write to file. The second input string is the name of the file to write the first string into. Write a program HTML-page-to-file which consumes an HTML-page and a filename (string) and returns void, but writes the page contents to the named file as a string. Use it to write the page of one of your stock examples to file.

    Many students should be able to finish up to this point

  6. Develop a program produce-all-stock-pages which consumes an inventory and returns true. The program writes one web page file for each stock item in the inventory. The library provides functions filename-from-symbol and filename-from-string which take a symbol/string (respectively) and return a string containing the given symbol/string with the ".html" extension appended. filename-from-string converts all spaces in the input string to dashes. Use these functions to create the filename for each stock's page.

  7. Develop a program gen-inventory-index-page which consumes an inventory and returns an html-page. The page should contain a list of links, with one link for each stock item in the inventory. Use the album title as the text in the link and the library function filename-for-string to compute the filename for the stock's web page.

    Very few students will get to this point.

  8. Listing all of Sammy's inventory on one web page makes it harder for customers to browse categories of music. We therefore want to create separate listings for each category. The remaining problems generate such a web site.

    1. Develop a program produce-category-page which consumes a category and an inventory and returns true. The program should write to file a page containing a list of links to the stock items that are in that category.

    2. Develop a program produce-all-category-pages which consumes a list of categories and an inventory and returns true. The program should create the category page for each category in the input list. Note that for this program, you do not need to look at what kind of inventory you have; instead, your template needs only to take the structure of the list of categories into account.

    3. Develop a program produce-category-index which consumes a list of categories and returns true. The program should write to file a page containing a list of links, one to each category page.