r10296: Automated commit for Debian build of lml2 upstream-version-1.5.2
[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 (defun lml-file-name (f &optional (type :source))
23   (when (and (consp f) (eql (car f) 'cl:quote))
24     (setq f (cadr f)))
25   (when (symbolp f)
26     (setq f (string-downcase (symbol-name f))))
27   (when (stringp f)
28     (unless (position #\. f)
29       (setq f (concatenate 'string f ".html"))))
30   (if *sources-dir*
31       (merge-pathnames
32        (make-pathname :name (pathname-name f)
33                       :type (pathname-type f)
34                       :directory (pathname-directory f))
35        (ecase type
36          (:source *sources-dir*)
37          (:output *output-dir*)))
38       (if (stringp f)
39           (parse-namestring f)
40           f)))
41
42 (defmacro with-dir ((output &key sources) &body body)
43   (let ((output-dir (gensym))
44         (sources-dir (gensym)))
45   `(let ((,output-dir ,output)
46          (,sources-dir ,sources))
47     (when (stringp ,output-dir)
48       (setq ,output-dir (parse-namestring ,output-dir)))
49     (when (stringp ,sources-dir)
50       (setq ,sources-dir (parse-namestring ,sources-dir)))
51     (unless ,sources-dir
52       (setq ,sources-dir ,output-dir))
53     (let ((*output-dir* ,output-dir)
54           (*sources-dir* ,sources-dir))
55       ,@body))))
56
57 (defun lml-load-path (file)
58   (if (probe-file file)
59       (with-open-file (in file :direction :input)
60         (do ((form (read in nil 'eof) (read in nil 'eof)))
61             ((eq form 'eof))
62           (eval form)))
63     (format *trace-output* "Warning: unable to load LML file ~S" file)))
64
65 (defun process-dir (dir &key sources)
66   (with-dir (dir :sources sources)
67     (let ((lml-files (directory
68                       (make-pathname :defaults *sources-dir*
69                                      :name :wild
70                                      :type "lml"))))
71       (dolist (file lml-files)
72         (format *trace-output* "~&; Processing ~A~%" file)
73         (lml-load-path file)))))
74
75 (defun lml-load (file)
76   (lml-load-path (eval `(lml-file-name ,file :source))))
77
78 (defun insert-file (file)
79   (print-file-contents file *html-stream*))