diff --git a/src/Command/BuildCommand.php b/src/Command/BuildCommand.php index 06ab2da..edc5abf 100644 --- a/src/Command/BuildCommand.php +++ b/src/Command/BuildCommand.php @@ -6,6 +6,7 @@ use App\Database; use App\Page; +use App\Template; use App\Util; use LunrPHP\BuildLunrIndex; use PDO; @@ -83,7 +84,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int } foreach ($pages as $page) { self::writeln('Page: ' . $page->getId() . ''); - $site->getTemplate($page->getTemplateName())->render($page, $db); + $template = new Template($db, $site, $page->getTemplateName()); + $template->render($page); } // Build Lunr index as search.json. diff --git a/src/Site.php b/src/Site.php index 3e841ce..1fce726 100644 --- a/src/Site.php +++ b/src/Site.php @@ -74,15 +74,10 @@ public function getPages(): array return $this->pages; } - public function getTemplate(string $name): Template - { - return new Template($this, $name); - } - /** * @return Template[] */ - public function getTemplates(string $prefix = ''): array + public function getTemplates(Database $db, string $prefix = ''): array { $templatesDir = $this->getDir() . '/templates'; if (!is_dir($templatesDir)) { @@ -97,7 +92,7 @@ public function getTemplates(string $prefix = ''): array foreach ($finder as $file) { preg_match('/(.*)\.(.*)\.twig/', $file->getFilename(), $nameParts); $tplName = $file->getRelativePath() . '/' . $nameParts[1]; - $templates[] = new Template($this, $tplName); + $templates[] = new Template($db, $this, $tplName); } return $templates; } diff --git a/src/Template.php b/src/Template.php index 02fb99a..bcd8d00 100644 --- a/src/Template.php +++ b/src/Template.php @@ -25,8 +25,12 @@ final class Template /** @var string The filesystem name of this template. */ protected $name; - public function __construct(Site $site, string $name) + /** @var Database */ + private $db; + + public function __construct(Database $db, Site $site, string $name) { + $this->db = $db; $this->site = $site; $this->name = $name; } @@ -72,15 +76,19 @@ public function getFormats(): array */ public function renderSimple(string $format, Page $page, ?array $params = null): string { - $params['page'] = $page; - return $this->getTwig($page)->render($this->name . ".$format.twig", $params); + $allParams = array_merge([ + 'database' => $this->db, + 'site' => $page->getSite(), + 'page' => $page, + ], $params ?? []); + return $this->getTwig($page)->render($this->name . ".$format.twig", $allParams); } - public function render(Page $page, Database $db): void + public function render(Page $page): void { foreach ($this->getFormats() as $format) { $renderedTemplate = $this->getTwig($page)->render($this->name . ".$format.twig", [ - 'database' => $db, + 'database' => $this->db, 'site' => $page->getSite(), 'page' => $page, ]); @@ -97,7 +105,7 @@ public function render(Page $page, Database $db): void CommandBase::writeln('Compiling PDF for: ' . $page->getId()); $args = ['latexmk', '-lualatex', "-auxdir=$pdfDir", "-outdir=$pdfDir", $texOutFile]; $process = new Process($args, $pdfDir); - $process->setTimeout(5 * 60); + $process->setTimeout(10 * 60); $process->mustRun(); // Copy PDF to output directory. Util::mkdir(dirname($outFileBase)); @@ -127,7 +135,7 @@ protected function getTwig(Page $page): Environment ]); $twig->addExtension(new IntlExtension()); $twig->addExtension(new DebugExtension()); - $twigExtension = new Twig($this->site, $page); + $twigExtension = new Twig($this->db, $this->site, $page); $twig->addExtension($twigExtension); $escaper = $twig->getExtension(EscaperExtension::class); if ($escaper instanceof EscaperExtension) { diff --git a/src/Twig.php b/src/Twig.php index bf1a9d4..039c0fa 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -36,6 +36,9 @@ final class Twig extends AbstractExtension { + /** @var Database */ + protected $db; + /** @var Site */ protected $site; @@ -49,8 +52,9 @@ final class Twig extends AbstractExtension 'flickr' => [], ]; - public function __construct(Site $site, Page $page) + public function __construct(Database $db, Site $site, Page $page) { + $this->db = $db; $this->site = $site; $this->page = $page; } @@ -393,7 +397,7 @@ public function escapeCsv(Environment $env, ?string $string = '', string $charse private function getCommonMarkEnvironment(string $format): CommonMarkEnvironment { $shortcodes = []; - foreach ($this->site->getTemplates('shortcodes') as $shortcodeTemplate) { + foreach ($this->site->getTemplates($this->db, 'shortcodes') as $shortcodeTemplate) { $shortcodeName = substr($shortcodeTemplate->getName(), strlen('shortcodes/')); $page = $this->page; $shortcodes[$shortcodeName] = static function ( diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index b99ceaf..ad76c4b 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -29,12 +29,12 @@ public function setUp(): void */ public function testRenderSimple(): void { - $site = new Site(__DIR__ . '/test_site'); - $tpl = new Template($site, 'test'); - $page = $site->getPages()['/simple']; + $tpl = new Template($this->db, $this->site, 'test'); + $page = $this->site->getPages()['/simple']; $out = $tpl->renderSimple('tex', $page); self::assertSame(' \documentclass{article} +\usepackage{graphicx} \begin{document} @@ -52,12 +52,15 @@ public function testRenderSimple(): void */ public function testShortcodes(): void { - $site = new Site(__DIR__ . '/test_site'); - $tpl = new Template($site, 'test'); - $page = $site->getPages()['/shortcodes']; - $out = $tpl->renderSimple('tex', $page); + $tpl = new Template($this->db, $this->site, 'test'); + $page = $this->site->getPages()['/shortcodes']; + $tpl->render($page); + $texFile = __DIR__ . '/test_site/cache/tex/shortcodes.tex'; + self::assertFileExists($texFile); + $out = file_get_contents($texFile); self::assertSame(" \documentclass{article} +\usepackage{graphicx} \begin{document} @@ -72,6 +75,8 @@ public function testShortcodes(): void \\end{center} \\end{figure} +There are 4 pages. + @@ -85,8 +90,7 @@ public function testShortcodes(): void */ public function testGetFormats(): void { - $site = new Site(__DIR__ . '/test_site'); - $tpl = new Template($site, 'test'); + $tpl = new Template($this->db, $this->site, 'test'); $this->assertSame(['tex'], $tpl->getFormats()); } } diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 97c1f11..cb98da0 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -4,6 +4,7 @@ namespace Test; +use App\Database; use App\Page; use App\Site; use App\Twig; @@ -13,6 +14,14 @@ final class TwigTest extends TestCase { + /** @var Database */ + private $db; + + public function setUp(): void + { + $this->db = new Database(); + } + /** * @covers \App\Twig::escapeCsv() * @covers \App\Twig::escapeTex() @@ -21,7 +30,7 @@ final class TwigTest extends TestCase public function testEscape(string $strategy, ?string $in, string $out): void { $site = new Site(__DIR__ . '/test_site'); - $twig = new Twig($site, new Page($site, '/simple')); + $twig = new Twig($this->db, $site, new Page($site, '/simple')); $env = new Environment(new ArrayLoader()); $escapeMethod = 'escape' . ucfirst($strategy); self::assertSame($out, $twig->$escapeMethod($env, $in)); @@ -48,7 +57,7 @@ public function provideEscape(): array public function testQrCode(): void { $site = new Site(__DIR__ . '/test_site'); - $twig = new Twig($site, new Page($site, '/simple')); + $twig = new Twig($this->db, $site, new Page($site, '/simple')); $out = $twig->functionQrCode('Lorem'); self::assertSame('/assets/qrcodes/db6ff2ffe2df7b8cfc0d9542bdce27dc.svg', $out); } @@ -60,7 +69,7 @@ public function testQrCode(): void public function testImageUrlsToLatex(string $pageId, string $markdown, string $latex): void { $site = new Site(__DIR__ . '/test_site'); - $twig = new Twig($site, new Page($site, $pageId)); + $twig = new Twig($this->db, $site, new Page($site, $pageId)); self::assertSame($latex, $twig->filterMarkdownToLatex($markdown)); } diff --git a/tests/test_site/templates/shortcodes/commons.tex.twig b/tests/test_site/templates/shortcodes/commons.tex.twig index d4c845e..5aa44d2 100644 --- a/tests/test_site/templates/shortcodes/commons.tex.twig +++ b/tests/test_site/templates/shortcodes/commons.tex.twig @@ -9,4 +9,6 @@ \end{center} \end{figure} -{% endautoescape %} \ No newline at end of file +There are {{ database.query('SELECT COUNT(*) AS t FROM pages').fetch.t }} pages. + +{% endautoescape %} diff --git a/tests/test_site/templates/test.tex.twig b/tests/test_site/templates/test.tex.twig index 6aba2a8..81f4d80 100644 --- a/tests/test_site/templates/test.tex.twig +++ b/tests/test_site/templates/test.tex.twig @@ -1,6 +1,7 @@ {% autoescape 'tex' %} \documentclass{article} +\usepackage{graphicx} \begin{document}