expand binary paths
[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 (or (and (eq type :source) *sources-dir*)
31           (and (eq type :output) *output-dir*))
32       (merge-pathnames
33        (make-pathname :name (pathname-name f)
34                       :type (pathname-type f)
35                       :directory (pathname-directory f))
36        (ecase type
37          (:source *sources-dir*)
38          (:output *output-dir*)))
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 &key optional)
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     (unless optional
65       (format *trace-output* "Warning: unable to load LML file ~S" file))))
66
67 (defun process-dir (dir &key sources)
68   (with-dir (dir :sources sources)
69     (let ((lml-files (directory
70                       (make-pathname :defaults *sources-dir*
71                                      :name :wild
72                                      :type "lml"))))
73       (dolist (file lml-files)
74         (format *trace-output* "~&; Processing ~A~%" file)
75         (lml-load-path file)))))
76
77 (defun lml-load (file &key optional)
78   (lml-load-path (eval `(lml-file-name ,file :source)) :optional optional))
79
80 (defun insert-file (file)
81   (print-file-contents file *html-stream*))