1<?php
2
3require_once(dirname(__FILE__).'/../../lib/plugins.php');
4
5class projects_plugin_pdflatex extends MakeRule {
6	/**
7	 * The name of the rule, a human readable string, a unique identifier
8	 */
9	public function name() { return "PDFLatex"; }
10
11	/**
12	 * whether this rule can make a given target
13	 */
14	public function can_handle($project, $file) {
15		if (!has_extension($file->name(), ".pdf")) return false;
16		$tex = $this->replace_extension($file->name(), ".pdf", ".tex");
17		return $project->file($tex) != NULL;
18	}
19
20	/**
21	 * The dependent files needed by this rule
22	 */
23	protected function dependency($project, $file) {
24		$tex = $this->replace_extension($file->name(), ".pdf", ".tex");
25		$deps = $file->dependency();
26		if (!is_array($deps)) $deps = array();
27		if (!in_array($tex, $deps))
28	 		$deps[] = $tex;
29		return $deps;
30	}
31
32	/**
33	 * The default recipe
34	 */
35	protected function recipe($project, $file) {
36		$tex = $this->replace_extension($file->name(), ".pdf", ".tex");
37		$base = substr($tex, 0, -4);
38		$bibtex = "";
39		$pdflatex = "pdflatex -interaction=nonstopmode $tex\n";
40		$texfile = $project->file($tex);
41		if (!$texfile) return '';
42		foreach ($texfile->dependency() as $dep)
43			if (has_extension($dep, ".bib")) {
44				$bibtex = "bibtex $base\n";
45				break;
46			}
47		return "rm -f $base.aux" . "\n" .
48			$pdflatex . $bibtex . $pdflatex;
49	}
50}
51
52?>