1 [[!meta author="spalax"]]
2 [[!template id=plugin name=compile author="[[Louis|spalax]]"]]
6 The compile plugin provides the `compile` directive, used to on-the-fly compile
9 For instance, if you want to publish files together with their sources (like
10 `.tex` and `.pdf` files), you can have the `.tex` file in your source wiki
11 directory, and command `\[[!compile files="foo.tex"]]` (or wikilink
12 `\[[foo.tex]]`, if the right option is set) will compile file and render as a
13 link to the `.pdf` file.
19 Some important security notice.
21 - This plugins allows user to execute arbitrary commands when compiling the
22 wiki. Use at your own risk. If you use Ikiwiki as a static web site compiler
23 (and not a wiki), and you are the only one to compile the wiki, there is no
24 risk. If you *do* allow untrusted users to edit or comment on the wiki, they
25 can use the `compile` directives to execute completely arbitrary code, regardless
26 of configuration safeguards you may put.
28 - Source files are published, wheter option `source` is true or not. If
29 `source` is false, source may not be *advertised*, but it is still available
30 somewhere on your website (most likely by replacing in the compiled file URL
31 the extension of the compiled file by the extension of the source file). So,
32 do not use this plugin if you do not want to publish your source files
33 (sorry: I designed this plugin to publish free stuff).
35 The plugin could be modified to only allow commands to be modified from the
36 configuration and it would be safer to use. However, it would still be vulnerable
37 to command injection attacks because it uses `qx()` command expansion, which
38 runs commands through `/bin/sh -c`. A thorough security review would be in order
39 before this should be considered secure running on untrusted input.
41 A simpler implementation, that only runs a predefined set of commands, may be
42 simpler to implement than auditing this whole plugin. For example, the
43 [[bibtex2html]] module performs a similar task than the compile module, but
44 hardcodes the command used and doesn't call it with `/bin/sh -c`. It could be
45 expanded to cover more commands. See this
46 [[plugins/contrib/bibtex2html/discussion/]] for a followup on this idea.
50 I want to publish some latex files, both source (`.tex`) and compiled (`.pdf`)
51 version, but I do not want to maintain two versions of the same file.
53 Using this plugin, I only have to maintain the `.tex` files, and thoses files
54 are compiled on the fly, so that the `pdf` is published.
58 Strings (destination name, template name and build command) accept python-like
59 syntax ``%{name}s``, which is replaced by the value of variable ``name``. The
60 following variables are abailable.
62 - `srcname`: Source name.
63 - `srcextension`: Extension of the source name.
64 - `filetype`: File type (extension of the source name, otherwise specified by directive).
65 - `dirname`: Directory of the source file.
66 - `wikiname`: Name of source file, relative to source wiki directory.
67 - `srcfullname`: Name of source file, relative to file system root.
68 - `basename`: Source name, without directory nor extension.
69 - `destname`: Destination name (without directory).
70 - `destextension`: Extension of the destination name.
71 - `targetname`: Destination name, relative to the destination directory.
72 - `destfullname`: Destination name, relative to file system root.
78 Basic usage of this plugin is:
80 \[[!compile files="foo.ext"]]
82 It renders file `foo.ext` according to rules defined in the setup file, and
83 publish the compiled version.
87 All the arguments (but `source` and `filetype`) are string which are processed
88 using python-like string formatting, and described in the setup options section.
90 - `files`: List of files used in compilation, as space separated string. For
91 instance, to compile some tex file including a png image, you will have:
92 `files="foo.tex image.png"`. It is not possible to have filenames containing
93 spaces (unless you provide me a patch to recognize escaped spaces).
94 - `filetype`: By default, the source file extension is used to determine build
95 command and other configuration. If the same extension refer to different
96 type of files, you can enforce the filetype using this argument. For
97 instance, if some your LaTeX files have to be compiled with `pdflatex`, while
98 the other require `latex`, your `compile_filetypes` can contains two keys
99 `tex` and `texdvi`. By default, LaTeX files will be compiled using
100 configuration associated to `tex`, unless directive has argument
101 `filetype=texdvi`, in which case the latter configuration is used.
102 - `destname`: Name of the compiled file name.
103 - `build`: Build command.
104 - `source`: Boolean to choose whether to publish source file or not. The only
105 effect is the template choice: source is always published (but not always
107 - `template`: Name of the template to use (if set, the `source` option is
109 - `var_*`: Any argument with a name starting with ``var_`` is transmitted to the command and template. For instance, if directive has argument ``var_foo=bar``, then string ``%{foo}s`` in the command will be replaced by ``bar``, and the template will have a variable named ``foo``, and ``<TMPL_VAR FOO>`` will be replaced by ``bar``.
113 Note: This directive does not work if source file name does not have an
114 extension (i.e. does not contain a dot). This should not be too hard to
115 implement, but I do not need it. Patches welcome.
119 Here are the setup options (most of them can be overloaded on a per-extension
120 basis by setup option `compile_filetypes`, or by directive arguments):
122 - `compile_source` (boolean): should sources be published with compiled file
123 (this only affect template choice; see warning)? Default is true.
124 - `compile_template_source` (string): name of the template to use for compiled
125 files when option `source` is true. Default is `compile_source.tmpl`.
126 - `compile_template_nosource` (string): name of the template to use for
127 compiled files when option `source` is false. Default is
128 `compile_nosource.tmpl`.
129 - `compile_filetypes` (string): Per extension configuration (see paragraph
131 - `compile_tmpdir` (string): Path of a directory to use to compile files:
132 source file (and dependency) are copied to this directory before being
133 compiled (to avoid messing the ikiwiki directory with compiled version or
134 auxiliary files). Default is `SOURCE_WIKI/.ikwiki/tmp/compile`.
135 - `compile_bindir` (string): Directory containing binaries to use to compile
136 files. Default is undefined.
137 - `compile_depends` (string): List of files all compiled files will depend on
138 (see *Compilation* section below).
139 - `compile_build` (string): Command to use to compile files. Default
141 - `compile_inline` (boolean): If true, wikilinks pointing to files with an
142 extension specified in `compile_filetypes` are treated as a directive
143 \[[!compile files="LINK"]]. For instance, if this is set globally (or just
144 for tex), a wikilink \[[foo.tex]] will compile file `foo.tex`, and publish
145 the compiled `foo.pdf` file.
147 ### The `compile_filetypes` option
149 This variable is a json string, representing a dictionary. Keys are source file
150 extensions, values are dictionary of options applying only to files with this
153 Keys of these new directory are `source`, `template_nosource`,
154 `template_source`, `build`, `depends`, `inline`, and overrides generic options
155 defined above. They are themselves overriden by directive arguments (excepted
160 compile_filetypes => '{
162 "build": "pdflatex %{basename}s",
163 "destname": "%{basename}s.pdf",
164 "depends": ["logo.png"],
168 "build": "latex %{basename}s",
169 "destname": "%{basename}s.pdf",
170 "depends": ["logo.eps"]
178 Before compilation, the source file and all dependencies are copied to the
179 temporary directory defined by option `compile_tmpdir`. For instance, if all
180 you LaTeX files are compiled using a custom class `foo.sty`, and a particular
181 file `bar.tex` uses the `logo.png` file, your setup option will contain
182 `foo.sty` as `depends`, and `compile` directive will be called using
183 `\[[!compile files="bar.tex logo.png"]]`. Then, before compilation, files
184 `foo.sty`, `bar.tex` and `logo.png` will be copied in the same temporary
187 Note that path are *flattened* when copied: before performing compilation of
188 directive `\[[!compile files="sub1/foo sub2/bar"]]`, files `foo` and `bar` will
189 be copied in the same directory: this temporary directory will contain failes
190 `foo` and `bar`, but not `sub1/foo` and `sub2/bar`.
194 The build command used is (if defined, by priority order):
196 - defined by argument `build` of directive;
197 - setup command ``compile_filetypes{TYPE}{build}``;
198 - setup command ``compile_build`` (if you have a generic build command);
199 - command ``$config{compile_bindir}/${extension}s %{srcname}s`` (if setup variable ``compile_bindir``is defined, is a directory, and contains an executable file matching the extension, it will be used);
200 - command ``make -f $config{compile_bindir}/make.${extension}s %{destname}s`` (if setup variable ``compile_bindir`` is defined, is a directory, and contains a readable makefile ``make.EXTENSION``, it will be used).
204 The way links are rendered is defined in a template, which is (by order of
205 priority, some of them depends on whether ``source`` is true):
207 - argument `template` of directive;
208 - setup variable ``compile_filetypes{TYPE}{template_source}`` or ``compile_filetypes{TYPE}{template_nosource}``;
209 - setup variable ``compile_source`` or ``compile_nosource``;
210 - `compile_source.mdwn` or `compile_nosource.mdwn`.
212 It is passed the following variables:
214 - `DESTURL`: URL to the compiled file.
215 - `DESTNAME`: Name of the compiled file.
216 - `SRCURL`: URL to the source file.
217 - `SRCNAME`: Name of the source file (without directory).
218 - `ORIGNAME`: Name of the source file (with directory).
220 Note that templates can be used to display images (instead of a link to them).
221 For instance, if you have a `.tiff` file you want to convert to png before
222 displaying it on your website, you can use as a template:
224 <img src="<TMPL_VAR DESTURL>">
228 Code and documentation can be found here : [[https://atelier.gresille.org/projects/gresille-ikiwiki/wiki/Compile]].