Suppose we want a contiguous sequence of blank lines
to be come out as the paragraph separator,
This will cause newline to read up all the following
whitespace, and then check to see how many further
newlines it picked up. If there was at least one,
it outputs the paragraph separator, viz.,
Before a control sequence can be used, we must fix the
escape character. The following sets it to backslash:
We can now invoke the 1 Writing Mistie formats
A typical intent of a format file is to cause
certain characters in the input document to
trigger non-trivial changes in the output document.
E.g., if the output is to be HTML, we'd like the
characters <, >, &, and " in the
input to come out as <, >, &,
and ", respectively. The Mistie
procedure mistie-def-char can be used for this:
(mistie-def-char #\<
(lambda ()
(display "<")))
(mistie-def-char #\>
(lambda ()
(display ">")))
(mistie-def-char #\&
(lambda ()
(display "&")))
(mistie-def-char #\"
(lambda ()
(display """)))
mistie-def-char takes two arguments: The first is
the character that is defined, and the second is
the procedure associated with it. Here, the procedure
writes the HTML encoded version of the character.
<p>. We could mistie-def-char the newline
character as follows:
(mistie-def-char #\newline
(lambda ()
(newline)
(let* ((s (h-read-whitespace))
(n (h-number-of-newlines s)))
(if (> n 0)
(begin (display "<p>")
(newline) (newline))
(display s)))))
<p> followed by two newlines (added for human
readability). Otherwise, it merely prints the
picked up whitespace as is. The help procedures
h-read-whitespace and h-number-of-newlines
are ordinary Scheme procedures:
(define h-read-whitespace
(lambda ()
(let loop ((r '()))
(let ((c (peek-char)))
(if (or (eof-object? c) (not (char-whitespace? c)))
(list->string (reverse r))
(loop (cons (read-char) r)))))))
(define h-number-of-newlines
(lambda (ws)
(let ((n (string-length ws)))
(let loop ((i 0) (k 0))
(if (>= i n) k
(loop (+ i 1)
(if (char=? (string-ref ws i) #\newline)
(+ k 1) k)))))))
1.1 Control sequences
The Mistie procedure mistie-def-ctl-seq defines
control sequences. A control sequence is a
sequence of letters (alphabetic characters), and
is invoked in the input document by prefixing the
sequence with an escape character. (The case of
the letters is insignificant.) mistie-def-ctl-seq
associates a procedure with a control sequence --
when the control sequence occurs in the input
document, it causes the procedure to be applied.
The following defines the control sequence br,
which emits the HTML tag <br>:
(mistie-def-ctl-seq 'br
(lambda ()
(display "<br>")))
(set! mistie-escape-char #\\)
br control sequence as \br.