r5219: *** empty log message ***
[lml2.git] / files.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          files.cl
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) 2002 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; LML2 users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the GNU General Public License v2
14 ;;;; (http://www.gnu.org/licenses/gpl.html)
15 ;;;; *************************************************************************
16
17 (in-package #:lml2)
18
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20   (defvar *output-dir* nil)
21   (defvar *sources-dir* nil)
22   )
23
24 (defmacro lml-file-name (file &optional (type :source))
25   (let ((f file))
26     (when (and (consp f) (eql (car f) 'cl:quote))
27       (setq f (cadr f)))
28     (when (symbolp f)
29       (setq f (string-downcase (symbol-name f))))
30     (when (stringp f)
31       (unless (position #\. f)
32         (setq f (concatenate 'string f ".html"))))
33     (if *sources-dir*
34         (make-pathname :defaults (ecase type
35                                    (:source *sources-dir*)
36                                    (:output *output-dir*))
37                        :name `,(pathname-name f)
38                        :type `,(pathname-type f))
39       (if (stringp f)
40           (parse-namestring f)
41         f))))
42
43 (defmacro with-dir ((output &key sources) &body body)
44   (let ((output-dir (gensym))
45         (sources-dir (gensym)))
46   `(let ((,output-dir ,output)
47          (,sources-dir ,sources))
48     (when (stringp ,output-dir)
49       (setq ,output-dir (parse-namestring ,output-dir)))
50     (when (stringp ,sources-dir)
51       (setq ,sources-dir (parse-namestring ,sources-dir)))
52     (unless ,sources-dir
53       (setq ,sources-dir ,output-dir))
54     (let ((*output-dir* ,output-dir)
55           (*sources-dir* ,sources-dir))
56       ,@body))))
57
58 (defun lml-load-path (file)
59   (if (probe-file file)
60       (with-open-file (in file :direction :input)
61         (do ((form (read in nil 'eof) (read in nil 'eof)))
62             ((eq form 'eof))
63           (eval form)))
64     (format *trace-output* "Warning: unable to load LML file ~S" file)))
65
66 (defun process-dir (dir &key sources)
67   (with-dir (dir :sources sources)
68     (let ((lml-files (directory
69                       (make-pathname :defaults *sources-dir*
70                                      :name :wild
71                                      :type "lml"))))
72       (dolist (file lml-files)
73         (format *trace-output* "~&; Processing ~A~%" file)
74         (lml-load-path file)))))
75
76 (defun lml-load (file)
77   (lml-load-path (eval `(lml-file-name ,file :source))))
78
79 (defun include-file (file)
80   (print-file-contents file *html-stream*))