Documents are hardly ever self-contained, and references to external resources are necessary and useful for the following reasons:
- We don't want to have to load images to memory just to represent the document, and then have to write those images to base-64 when writing the document to an output format.
- External resources can reduce errors. For instance, if we're writing an
article detailing the internals of a piece of code, we can keep the code in an
external file (along with a script to test it), and simply include bits of it
using the very flexible
includecontrib. Then, changes to the file will be automatically reflected in the document, reducing duplication and removing the need to manually synchronize contents.
- Just about every markup format supports at least referencing images both on the local filesystem and on the web, so we have to support that if we are to support that markup.
CommonDoc has simple support for doing operations with external files. There's a
*base-directory*, which basically the directory where all
relative pathnames referenced in the document begin. Once that variable is
bound, a couple of functions can be used to manipulate pathnames.
letby specific input formats.
*base-directory*, return a relative pathname. Otherwise, return the pathname unchanged.
(in-package :common-doc.file)(let ((*base-directory* (user-homedir-pathname))) (absolute-path #p"directory/file.txt")) ;; => #p"/home/eudoxia/directory/file.txt"