r11290: add _final.lml processing
[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 (defun lml-file-name (f &optional (type :source))
27   (when (and (consp f) (eql (car f) 'cl:quote))
28     (setq f (cadr f)))
29   (when (symbolp f)
30     (setq f (string-downcase (symbol-name f))))
31   (when (stringp f)
32     (unless (position #\. f)
33       (setq f (concatenate 'string f ".html"))))
34   (if *sources-dir*
35       (make-pathname :defaults (ecase type
36                                  (:source *sources-dir*)
37                                  (:output *output-dir*))
38                      :name (pathname-name f)
39                      :type (pathname-type f))
40       (if (stringp f)
41           (nth-value 0 (parse-namestring f))
42           f)))
43
44 (defmacro with-dir ((output &key sources) &body body)
45   (let ((output-dir (gensym))
46         (sources-dir (gensym)))
47   `(let ((,output-dir ,output)
48          (,sources-dir ,sources))
49     (when (stringp ,output-dir)
50       (setq ,output-dir (parse-namestring ,output-dir)))
51     (when (stringp ,sources-dir)
52       (setq ,sources-dir (parse-namestring ,sources-dir)))
53     (unless ,sources-dir
54       (setq ,sources-dir ,output-dir))
55     (let ((*output-dir* ,output-dir)
56           (*sources-dir* ,sources-dir))
57       ,@body))))
58
59 (defun lml-load-path (file)
60   (if (probe-file file)
61       (with-open-file (in file :direction :input)
62         (do ((form (read in nil 'eof) (read in nil 'eof)))
63             ((eq form 'eof))
64           (eval form)))
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)
78   (lml-load-path (eval `(lml-file-name ,file :source))))
79
80 (defun include-file (file)
81   (print-file-contents file *html-output*))