From 87b82b5822ca54228cfa6df29be3ad9d4bc47d16 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 20 Feb 2017 00:05:28 -0800 Subject: Import Upstream version 2d2 --- htmlform.scm | 1128 +++++++++++++++++++--------------------------------------- 1 file changed, 364 insertions(+), 764 deletions(-) (limited to 'htmlform.scm') diff --git a/htmlform.scm b/htmlform.scm index c7ce1dc..935e006 100644 --- a/htmlform.scm +++ b/htmlform.scm @@ -1,9 +1,9 @@ -;;; "htmlform.scm" Generate HTML 2.0 forms; service CGI requests. -*-scheme-*- -; Copyright 1997, 1998 Aubrey Jaffer +;;; "htmlform.scm" Generate HTML 2.0 forms. -*-scheme-*- +; Copyright 1997, 1998, 2000, 2001 Aubrey Jaffer ; -;Permission to copy this software, to redistribute it, and to use it -;for any purpose is granted, subject to the following restrictions and -;understandings. +;Permission to copy this software, to modify it, to redistribute it, +;to distribute modified versions, and to use it for any purpose is +;granted, subject to the following restrictions and understandings. ; ;1. Any copy made of this software must include this copyright notice ;in full. @@ -18,31 +18,20 @@ ;each case. (require 'sort) -(require 'scanf) (require 'printf) -(require 'line-i/o) (require 'parameters) -(require 'fluid-let) -(require 'dynamic-wind) -(require 'pretty-print) (require 'object->string) -(require 'string-case) -(require 'string-port) (require 'string-search) (require 'database-utilities) (require 'common-list-functions) ;;;;@code{(require 'html-form)} - -;;@body Procedure names starting with @samp{html:} send their output -;;to the port @0. @0 is initially the current output port. -(define *html:output-port* (current-output-port)) - -(define (html:printf . args) (apply fprintf *html:output-port* args)) +;;@ftindex html-form +(define html:blank (string->symbol "")) ;;@body Returns a string with character substitutions appropriate to ;;send @1 as an @dfn{attribute-value}. -(define (make-atval txt) ; attribute-value +(define (html:atval txt) ; attribute-value (if (symbol? txt) (set! txt (symbol->string txt))) (if (number? txt) (number->string txt) @@ -54,256 +43,164 @@ ;;@body Returns a string with character substitutions appropriate to ;;send @1 as an @dfn{plain-text}. -(define (make-plain txt) ; plain-text `Data Characters' - (if (symbol? txt) (set! txt (symbol->string txt))) - (if (number? txt) - (number->string txt) - (string-subst (if (string? txt) txt (object->string txt)) - "&" "&" - "<" "<" - ">" ">"))) +(define (html:plain txt) ; plain-text `Data Characters' + (cond ((eq? html:blank txt) " ") + (else + (if (symbol? txt) (set! txt (symbol->string txt))) + (if (number? txt) + (number->string txt) + (string-subst (if (string? txt) txt (object->string txt)) + "&" "&" + "<" "<" + ">" ">"))))) + +;;@body Returns a tag of meta-information suitable for passing as the +;;third argument to @code{html:head}. The tag produced is @samp{}. The string or symbol @1 can be +;;@samp{author}, @samp{copyright}, @samp{keywords}, @samp{description}, +;;@samp{date}, @samp{robots}, @dots{}. +(define (html:meta name content) + (sprintf #f "\n" name (html:atval content))) + +;;@body Returns a tag of HTTP information suitable for passing as the +;;third argument to @code{html:head}. The tag produced is @samp{}. The string or symbol @1 can be +;;@samp{Expires}, @samp{PICS-Label}, @samp{Content-Type}, +;;@samp{Refresh}, @dots{}. +(define (html:http-equiv name content) + (sprintf #f "\n" + name (html:atval content))) + +;;@args delay uri +;;@args delay +;; +;;Returns a tag suitable for passing as the third argument to +;;@code{html:head}. If @2 argument is supplied, then @1 seconds after +;;displaying the page with this tag, Netscape or IE browsers will fetch +;;and display @2. Otherwise, @1 seconds after displaying the page with +;;this tag, Netscape or IE browsers will fetch and redisplay this page. +(define (html:meta-refresh delay . uri) + (if (null? uri) + (sprintf #f "\n" delay) + (sprintf #f "\n" + delay (car uri)))) ;;@args title backlink tags ... ;;@args title backlink ;;@args title ;; -;;Outputs headers for an HTML page named @1. If string arguments @2 -;;... are supplied they are printed verbatim within the @t{} -;;section. -(define (html:start-page title . args) +;;Returns header string for an HTML page named @1. If @2 is a string, +;;it is used verbatim between the @samp{H1} tags; otherwise @1 is +;;used. If string arguments @3 ... are supplied, then they are +;;included verbatim within the @t{} section. +(define (html:head title . args) (define backlink (if (null? args) #f (car args))) (if (not (null? args)) (set! args (cdr args))) - (html:printf "\\n") - (html:printf "\\n") - (html:comment "HTML by SLIB" - "http://swissnet.ai.mit.edu/~jaffer/SLIB.html") - (html:printf "%s%s\\n" - (apply string-append args) (make-plain title)) - (html:printf "

%s

\\n" - (or backlink (make-plain title)))) - -;;@body Outputs HTML codes to end a page. -(define (html:end-page) - (html:printf "\\n") - (html:printf "\\n")) - -;;@body Writes (using @code{html:printf}) the strings @1, @2 as -;;@dfn{PRE}formmated plain text (rendered in fixed-width font). -;;Newlines are inserted between @1, @2. HTML tags (@samp{}) -;;within @2 will be visible verbatim. + (string-append + (sprintf #f "\\n") + (sprintf #f "\\n") + (sprintf #f "%s" + (html:comment "HTML by SLIB" + "http://swissnet.ai.mit.edu/~jaffer/SLIB.html")) + (sprintf #f " \\n %s\\n %s\\n \\n" + (html:plain title) (apply string-append args)) + (sprintf #f "

%s

\\n" (or backlink (html:plain title))))) + +;;@body Returns HTML string to end a page. +(define (html:body . body) + (apply string-append + (append body (list (sprintf #f "\\n\\n"))))) + +;;@body Returns the strings @1, @2 as @dfn{PRE}formmated plain text +;;(rendered in fixed-width font). Newlines are inserted between @1, +;;@2. HTML tags (@samp{}) within @2 will be visible verbatim. (define (html:pre line1 . lines) - (html:printf "
\\n%s" (make-plain line1))
-  (for-each (lambda (line) (html:printf "\\n%s" (make-plain line))) lines)
-  (html:printf "
\\n")) - -;;@body Writes (using @code{html:printf}) the strings @1 as HTML -;;comments. + (sprintf #f "
\\n%s%s
" + (html:plain line1) + (string-append + (apply string-append + (map (lambda (line) (sprintf #f "\\n%s" (html:plain line))) + lines))))) + +;;@body Returns the strings @1 as HTML comments. (define (html:comment line1 . lines) - (html:printf "\\n")) - -;;@section HTML Tables - -;;@body -(define (html:start-table caption) - (html:printf "\\n") - (html:printf "\\n" (make-plain caption))) - -;;@body -(define (html:end-table) - (html:printf "
%s
\\n")) - -;;@body Outputs a heading row for the currently-started table. -(define (html:heading columns) - (html:printf "\\n") - (for-each (lambda (datum) (html:printf "%s\\n" (or datum ""))) columns)) - -;;@body Outputs a heading row with column-names @1 linked to URLs @2. -(define (html:href-heading columns urls) - (html:heading - (map (lambda (column url) - (if url - (sprintf #f "%s" url column) - column)) - columns urls))) - -;;@args k foreigns -;; -;;The positive integer @1 is the primary-key-limit (number of -;;primary-keys) of the table. @2 is a list of the filenames of -;;foreign-key field pages and #f for non foreign-key fields. -;; -;;@0 returns a procedure taking a row for its single argument. This -;;returned procedure prints the table row to @var{*html:output-port*}. -(define (make-row-converter pkl foreigns) - (lambda (data-row) - (define anchored? #f) - (define (present datum) - (cond ((or (string? datum) (symbol? datum)) - (html:printf "%s" (make-plain datum))) - (else - (html:printf - "
\\n%s
\\n" - (make-plain (call-with-output-string - (lambda (port) - (pretty-print datum port)))))))) - (html:printf "") - (for-each (lambda (datum foreign) - (html:printf "") - (cond ((not datum)) - ((null? datum)) - ((not anchored?) - (html:printf "= idx pkl)) - (html:printf - " %s" (make-atval (car contents)))))) - (html:printf "\">") - (set! anchored? (not (zero? pkl))))) - (cond ((not datum)) ((null? datum)) - ((not foreign) (present datum)) - ((zero? pkl) - (html:printf "" foreign) - (present datum) - (html:printf "")) - (else - (html:printf "" - foreign (make-atval datum)) - (present datum) - (html:printf "")))) - data-row foreigns) - (html:printf "\\n"))) - -;;@body -;;Returns the symbol @1 converted to a filename. -(define (table-name->filename table-name) - (and table-name (string-append - (string-subst (symbol->string table-name) "*" "" ":" "_") - ".html"))) - -(define (table-name->column-table-name db table-name) - ((((db 'open-table) '*catalog-data* #f) 'get 'coltab-name) - table-name)) - -;;@args caption db table-name match-key1 @dots{} -;;Writes HTML for @2 table @3 to @var{*html:output-port*}. -;; -;;The optional @4 @dots{} arguments restrict actions to a subset of -;;the table. @xref{Table Operations, match-key}. -(define (table->html caption db table-name . args) - (let* ((table ((db 'open-table) table-name #f)) - (foreigns (table 'column-foreigns)) - (tags (map table-name->filename foreigns)) - (names (table 'column-names)) - (primlim (table 'primary-limit))) - (html:start-table caption) - (html:href-heading - names - (append (make-list primlim (table-name->filename - (table-name->column-table-name db table-name))) - (make-list (- (length names) primlim) #f))) - (html:heading (table 'column-domains)) - (html:href-heading foreigns tags) - (html:heading (table 'column-types)) - (apply (table 'for-each-row) (make-row-converter primlim tags) args) - (html:end-table))) - -;;@body -;;Writes a complete HTML page to @var{*html:output-port*}. The string -;;@3 names the page which refers to this one. -(define (table->page db table-name index-filename) - (dynamic-wind - (lambda () - (if index-filename - (html:start-page - table-name - (sprintf #f "%s" - index-filename - (make-atval table-name) - (make-plain table-name))) - (html:start-page table-name))) - (lambda () (table->html table-name db table-name)) - html:end-page)) - -;;@body -;;Writes HTML for the catalog table of @1 to @var{*html:output-port*}. -(define (catalog->html db caption) - (html:start-table caption) - (html:heading '(table columns)) - ((((db 'open-table) '*catalog-data* #f) 'for-each-row) - (lambda (row) - (cond ((and (eq? '*columns* (caddr row)) - (not (eq? '*columns* (car row))))) - (else ((make-row-converter - 0 (list (table-name->filename (car row)) - (table-name->filename (caddr row)))) - (list (car row) (caddr row)))))))) - -;;@body -;;Writes a complete HTML page for the catalog of @1 to -;;@var{*html:output-port*}. -(define (catalog->page db caption) - (dynamic-wind - (lambda () (html:start-page caption)) - (lambda () - (catalog->html db caption) - (html:end-table)) - html:end-page)) + (string-append + (apply string-append + (if (substring? "--" line1) + (slib:error 'html:comment "line contains --" line1) + (sprintf #f "