;escape character -*- Scheme -*- (import (chicken file) srfi-1) (set! mistie-escape-char #\\) (define h-read-and-check-char (lambda (name expect-ch) (let ((ch (read-char))) (if (not (eqv? ch expect-ch)) (error (sprintf "~A: expected ~S but got ~S" name expect-ch ch)))))) (define h-ignore-spaces (lambda () (let loop () (let ((c (peek-char))) (unless (or (eof-object? c) (not (char-whitespace? c))) (read-char) (loop)))))) ; (define h-read-until (lambda (pred?) (let loop ((r '())) (if (pred? (peek-char)) (list->string (reverse! r)) (loop (cons (read-char) r)))))) (define h-read-group (lambda () ;simple group only, no nested braces (h-ignore-spaces) (h-read-and-check-char "h-read-group" #\{) (let ((s (h-read-until (lambda (c) (eqv? c #\}))))) (h-read-and-check-char "h-read-group" #\}) s))) (define h-read-filename (lambda () (h-ignore-spaces) (if (eqv? (peek-char) #\{) (h-read-group) (h-read-until (lambda (c) (or (eof-object? c) (char-whitespace? c))))))) ;evaluating Scheme expressions in the input (mistie-def-ctl-seq 'eval (lambda () (eval (read)) (h-ignore-spaces))) ;encode < > & " (mistie-def-char #\< (lambda () (display "<"))) (mistie-def-char #\> (lambda () (display ">"))) (mistie-def-char #\& (lambda () (display "&"))) (mistie-def-char #\" (lambda () (display """))) ;blank lines become paragraph separators (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))))))) (mistie-def-char #\newline (lambda () (newline) (let* ((ws (h-read-whitespace)) (n (h-number-of-newlines ws))) (if (> n 0) (begin (display "
") (newline) (newline)) (display ws))))) ; verbatim (mistie-def-ctl-seq 'p (lambda () (h-ignore-spaces) (let* ((old-escape-char mistie-escape-char) (c (read-char)) (ws (h-read-whitespace)) (nnl (h-number-of-newlines ws)) (pre? (> nnl 0))) (set! mistie-escape-char #f) (mistie-push-frame) (display (if pre? "
" ""))
(display ws)
(if (char=? c #\{)
(let ((n 0))
(set! mistie-escape-char #\|)
(mistie-def-char #\{
(lambda ()
(display #\{)
(set! n (+ n 1))))
(mistie-def-char #\}
(lambda ()
(if (= n 0)
(begin
(display (if pre? "
" ""))
(mistie-pop-frame)
(set! mistie-escape-char old-escape-char))
(begin
(display #\})
(set! n (- n 1)))))))
(mistie-def-char c
(lambda ()
(display (if pre? "" ""))
(display "")
(mistie-pop-frame)
(set! mistie-escape-char old-escape-char))))
(mistie-def-char #\newline newline))))
(mistie-def-ctl-seq 'q
(mistie-lookup-ctl-seq 'p))
;emphasis, bold, smallprint
(define h-wrap-font
(lambda (fontpre fontpost)
(h-ignore-spaces)
(h-read-and-check-char "h-wrap-font" #\{)
(mistie-push-frame)
(display fontpre)
(mistie-def-char #\}
(lambda ()
(display fontpost)
(mistie-pop-frame)))))
(mistie-def-ctl-seq 'i
(lambda ()
(h-wrap-font "" "")))
(mistie-def-ctl-seq 'b
(lambda ()
(h-wrap-font "" "")))
(mistie-def-ctl-seq 'small
(lambda ()
(h-wrap-font "" "")))
;stylesheet
(define h-stylesheet #f)
(mistie-def-ctl-seq 'stylesheet
(lambda ()
(print "ok")
(write `(set! h-stylesheet ,(h-read-group)) h-aux-port)
(newline h-aux-port)))
;title
(define h-title #f)
(mistie-def-ctl-seq 'title
(lambda ()
(let ((ti (h-read-group)))
(write `(set! h-title ,ti) h-aux-port)
(newline h-aux-port)
(mistie-push-frame)
(display "