r5177: *** empty log message ***
[lml.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 LML, is Copyright (c) 2002 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; LML 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 #:lml)
18
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20   (defvar *output-dir* nil)
21   (defvar *sources-dir* nil)
22   )
23
24 (defvar *html-output* *standard-output*)
25
26 (defmacro lml-file-name (file &optional (type :source))
27   (let ((f file))
28     (when (and (consp f) (eql (car f) 'cl:quote))
29       (setq f (cadr f)))
30     (when (symbolp f)
31       (setq f (string-downcase (symbol-name f))))
32     (when (stringp f)
33       (unless (position #\. f)
34         (setq f (concatenate 'string f ".html"))))
35     (if *sources-dir*
36         (make-pathname :defaults (ecase type
37                                    (:source *sources-dir*)
38                                    (:output *output-dir*))
39                        :name `,(pathname-name f)
40                        :type `,(pathname-type f))
41       (if (stringp f)
42           (parse-namestring f)
43         f))))
44
45 (defmacro with-dir ((output &key sources) &body body)
46   (let ((output-dir (gensym))
47         (sources-dir (gensym)))
48   `(let ((,output-dir ,output)
49          (,sources-dir ,sources))
50     (when (stringp ,output-dir)
51       (setq ,output-dir (parse-namestring ,output-dir)))
52     (when (stringp ,sources-dir)
53       (setq ,sources-dir (parse-namestring ,sources-dir)))
54     (unless ,sources-dir
55       (setq ,sources-dir ,output-dir))
56     (let ((*output-dir* ,output-dir)
57           (*sources-dir* ,sources-dir))
58       ,@body))))
59
60 (defun lml-load-path (file)
61   (if (probe-file file)
62       (with-open-file (in file :direction :input)
63         (do ((form (read in nil 'eof) (read in nil 'eof)))
64             ((eq form 'eof))
65           (eval form)))
66     (format *trace-output* "Warning: unable to load LML file ~S" file)))
67
68 (defun process-dir (dir &key sources)
69   (with-dir (dir :sources sources)
70     (let ((lml-files (directory
71                       (make-pathname :defaults *sources-dir*
72                                      :name :wild
73                                      :type "lml"))))
74       (dolist (file lml-files)
75         (format *trace-output* "~&; Processing ~A~%" file)
76         (lml-load-path file)))))
77
78 (defun lml-load (file)
79   (lml-load-path (eval `(lml-file-name ,file :source))))
80
81 (defun include-file (file)
82   (print-file-contents file *html-output*))