Skip to content

Commit

Permalink
Fully render shortcode templates
Browse files Browse the repository at this point in the history
Render shortcode templates with full access to `database` and `site` variables, like other templates.

Also increase LaTeX generation timeout, because Windows is slow.
  • Loading branch information
samwilson authored May 28, 2023
1 parent d669610 commit 71034e0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Database;
use App\Page;
use App\Template;
use App\Util;
use LunrPHP\BuildLunrIndex;
use PDO;
Expand Down Expand Up @@ -83,7 +84,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
foreach ($pages as $page) {
self::writeln('<info>Page: ' . $page->getId() . '</info>');
$site->getTemplate($page->getTemplateName())->render($page, $db);
$template = new Template($db, $site, $page->getTemplateName());
$template->render($page);
}

// Build Lunr index as search.json.
Expand Down
9 changes: 2 additions & 7 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
22 changes: 15 additions & 7 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

final class Twig extends AbstractExtension
{
/** @var Database */
protected $db;

/** @var Site */
protected $site;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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 (
Expand Down
22 changes: 13 additions & 9 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -72,6 +75,8 @@ public function testShortcodes(): void
\\end{center}
\\end{figure}
There are 4 pages.
Expand All @@ -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());
}
}
15 changes: 12 additions & 3 deletions tests/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test;

use App\Database;
use App\Page;
use App\Site;
use App\Twig;
Expand All @@ -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()
Expand All @@ -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));
Expand All @@ -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);
}
Expand All @@ -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));
}

Expand Down
4 changes: 3 additions & 1 deletion tests/test_site/templates/shortcodes/commons.tex.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
\end{center}
\end{figure}

{% endautoescape %}
There are {{ database.query('SELECT COUNT(*) AS t FROM pages').fetch.t }} pages.

{% endautoescape %}
1 change: 1 addition & 0 deletions tests/test_site/templates/test.tex.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% autoescape 'tex' %}

\documentclass{article}
\usepackage{graphicx}

\begin{document}

Expand Down

0 comments on commit 71034e0

Please sign in to comment.