r5294: *** empty log message ***
[lml2.git] / files.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          files.lisp
6 ;;;; Purpose:       File and directory functions for LML
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Aug 2002
9 ;;;;
10 ;;;; This file, part of LML2, is Copyright (c) 2000-2003 by Kevin Rosenberg.
11 ;;;; Rights of modification and redistribution are in the LICENSE file.
12 ;;;;
13 ;;;; *************************************************************************
14
15 (in-package #:lml2)
16
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18   (defvar *output-dir* nil)
19   (defvar *sources-dir* nil)
20   )
21
22 (defmacro lml-file-name (file &optional (type :source))
23   (let ((f file))
24     (when (and (consp f) (eql (car f) 'cl:quote))
25       (setq f (cadr f)))
26     (when (symbolp f)
27       (setq f (string-downcase (symbol-name f))))
28     (when (stringp f)
29       (unless (position #\. f)
30         (setq f (concatenate 'string f ".html"))))
31     (if *sources-dir*
32         (make-pathname :defaults (ecase type
33                                    (:source *sources-dir*)
34                                    (:output *output-dir*))
35                        :name `,(pathname-name f)
36                        :type `,(pathname-type f))
37       (if (stringp f)
38           (parse-namestring f)
39         f))))
40
41 (defmacro with-dir ((output &key sources) &body body)
42   (let ((output-dir (gensym))
43         (sources-dir (gensym)))
44   `(let ((,output-dir ,output)
45          (,sources-dir ,sources))
46     (when (stringp ,output-dir)
47       (setq ,output-dir (parse-namestring ,output-dir)))
48     (when (stringp ,sources-dir)
49       (setq ,sources-dir (parse-namestring ,sources-dir)))
50     (unless ,sources-dir
51       (setq ,sources-dir ,output-dir))
52     (let ((*output-dir* ,output-dir)
53           (*sources-dir* ,sources-dir))
54       ,@body))))
55
56 (defun lml-load-path (file)
57   (if (probe-file file)
58       (with-open-file (in file :direction :input)
59         (do ((form (read in nil 'eof) (read in nil 'eof)))
60             ((eq form 'eof))
61           (eval form)))
62     (format *trace-output* "Warning: unable to load LML file ~S" file)))
63
64 (defun process-dir (dir &key sources)
65   (with-dir (dir :sources sources)
66     (let ((lml-files (directory
67                       (make-pathname :defaults *sources-dir*
68                                      :name :wild
69                                      :type "lml"))))
70       (dolist (file lml-files)
71         (format *trace-output* "~&; Processing ~A~%" file)
72         (lml-load-path file)))))
73
74 (defun lml-load (file)
75   (lml-load-path (eval `(lml-file-name ,file :source))))
76
77 (defun include-file (file)
78   (print-file-contents file *html-stream*))