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