Skip to content

Commit

Permalink
Change how Twig templates get site-wide metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
damiendart committed Jul 28, 2024
1 parent a74a837 commit b88ef28
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/ssg/StaticSiteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
new ProcessTwigStep(
$this->inputDirectory,
$siteMetadata,
new TwigEnvironmentFactory($markdownConverterFactory),
new TwigEnvironmentFactory($markdownConverterFactory, $siteMetadata),
),
new MinifyHtmlStep(),
new WriteFilesStep($this->outputDirectory),
Expand Down
9 changes: 3 additions & 6 deletions src/ssg/Steps/ProcessTwigStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,18 @@ public function run(Inputfile ...$inputFiles): array
);
$chainLoader->addLoader($this->filesystemLoader);

$context = $inputFile->metadata;
$environment = $this->twigEnvironmentFactory->make($chainLoader);
$data = array_merge(
$this->globalMetadata->metadata,
$inputFile->metadata,
);

if (\array_key_exists('twigTemplate', $inputFile->metadata)) {
$template = $inputFile->metadata['twigTemplate'];
$data['renderedMarkdown'] = $inputFile->getContent();
$context['renderedMarkdown'] = $inputFile->getContent();
} else {
$template = $inputFile->outputPath;
}

$inputFiles[$key] = $inputFile
->withContent($environment->render($template, $data))
->withContent($environment->render($template, $context))
->withOutputPath($this->processRelativePathname($inputFile->outputPath));
}

Expand Down
4 changes: 4 additions & 0 deletions src/ssg/Support/TwigEnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace StaticSiteGenerator\Support;

use Michelf\SmartyPantsTypographer;
use StaticSiteGenerator\ValueObjects\SiteMetadata;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\TwigFilter;
Expand All @@ -19,6 +20,7 @@
{
public function __construct(
private MarkdownConverterFactory $markdownConverterFactory,
private SiteMetadata $siteMetadata,
) {}

public function make(LoaderInterface $loader): Environment
Expand Down Expand Up @@ -86,6 +88,8 @@ static function (?string $string): string {
),
);

$environment->addGlobal('site', $this->siteMetadata->metadata);

return $environment;
}
}
2 changes: 1 addition & 1 deletion src/ssg/input/.templates/base-note.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@

{% block headCustom %}
{{ parent() }}
<link href="{{ urlBase }}notes/index.atom" rel="alternate" title="Damien Dart’s Notes (www.robotinaponcho.net)" type="application/atom+xml">
<link href="{{ site.urlBase }}notes/index.atom" rel="alternate" title="Damien Dart’s Notes (www.robotinaponcho.net)" type="application/atom+xml">
{% endblock %}

20 changes: 10 additions & 10 deletions src/ssg/input/.templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
<meta content="initial-scale=1, width=device-width" name="viewport">

{% block head %}
<link href="/assets/style.{{ releaseTimestamp }}.css" rel="stylesheet">
<link href="/assets/style.{{ site.releaseTimestamp }}.css" rel="stylesheet">
{% endblock %}

<link href="{{ urlBase ~ slug }}" rel="canonical">
<meta content="{{ author | smartypants }}" name="author">
<link href="{{ site.urlBase ~ slug }}" rel="canonical">
<meta content="{{ site.author | smartypants }}" name="author">

{% if description is defined %}
<meta content="{{ description | smartypants }}" name="description">
<meta content="{{ description | smartypants }}" property="og:description">
{% endif %}

<meta content="{{ metaOpengraphImage }}" property="og:image">
<meta content="{{ metaOpengraphImage | default(site.metaOpengraphImage) }}" property="og:image">
<meta content="{{ metaTitle | smartypants }}" property="og:title">
<meta content="website" property="og:type">
<meta content="{{ urlBase ~ slug }}" property="og:url">
<meta content="{{ metaTwitterAuthor | smartypants }}" property="twitter:creator">
<meta content="{{ metaTwitterSite | smartypants }}" property="twitter:site">
<meta content="{{ site.urlBase ~ slug }}" property="og:url">
<meta content="{{ site.metaTwitterAuthor | smartypants }}" property="twitter:creator">
<meta content="{{ site.metaTwitterSite | smartypants }}" property="twitter:site">
<meta content="summary_large_image" name="twitter:card">

{% if metaNoCrawl | default(false) %}
Expand All @@ -56,7 +56,7 @@

<div class="site-header">
<div class="site-header__container">
<p class="site-header__title"><a href="/">{{ author | smartypants }}</a></p>
<p class="site-header__title"><a href="/">{{ site.author | smartypants }}</a></p>
<nav class="site-header__navigation">
<ul class="site-header__navigation__list" role="list">
<li class="site-header__navigation__list__item{% if section is defined and section is same as('projects') %} site-header__navigation__list__item--active{% endif %}"><a href="/projects/">Projects</a></li>
Expand Down Expand Up @@ -90,9 +90,9 @@
</ul>
</nav>
<p class="site-footer__copyright">© {{ 'now' | date('Y') }}
<a href="/">{{ author | smartypants }}</a>.</p>
<a href="/">{{ site.author | smartypants }}</a>.</p>
</footer>

<script src="/assets/app.{{ releaseTimestamp }}.js" type="module"></script>
<script src="/assets/app.{{ site.releaseTimestamp }}.js" type="module"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions src/ssg/input/.templates/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<div class="image-and-text">
<div class="image-and-text__image">
<picture class="robot__robot" id="content">
<source srcset="/assets/homepage-robot.{{ releaseTimestamp }}.avif, /assets/homepage-robot@2x.{{ releaseTimestamp }}.avif 2x" type="image/avif">
<source srcset="/assets/homepage-robot.{{ releaseTimestamp }}.webp, /assets/homepage-robot@2x.{{ releaseTimestamp }}.webp 2x" type="image/webp">
<img alt="A black-and-white drawing of a waving robot in a poncho." height="375" src="/assets/homepage-robot.{{ releaseTimestamp }}.png" srcset="/assets/homepage-robot@2x.{{ releaseTimestamp }}.png 2x" title="Hello!" width="350">
<source srcset="/assets/homepage-robot.{{ site.releaseTimestamp }}.avif, /assets/homepage-robot@2x.{{ site.releaseTimestamp }}.avif 2x" type="image/avif">
<source srcset="/assets/homepage-robot.{{ site.releaseTimestamp }}.webp, /assets/homepage-robot@2x.{{ site.releaseTimestamp }}.webp 2x" type="image/webp">
<img alt="A black-and-white drawing of a waving robot in a poncho." height="375" src="/assets/homepage-robot.{{ site.releaseTimestamp }}.png" srcset="/assets/homepage-robot@2x.{{ site.releaseTimestamp }}.png 2x" title="Hello!" width="350">
</picture>
</div>
<div class="image-and-text__text u-stack">
Expand Down
18 changes: 9 additions & 9 deletions src/ssg/input/notes/index.atom.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
This file is distributed under the MIT licence. For more
information, please refer to the accompanying "LICENCE" file.
#}
{% set items = collections.notes | sort((a, b) => b.metadata.git.createdAt <=> a.metadata.git.createdAt) | slice(0, 11) %}
{% set items = site.collections.notes | sort((a, b) => b.metadata.git.createdAt <=> a.metadata.git.createdAt) | slice(0, 11) %}
{% set lastUpdated = items | map(n => n.metadata.git.updatedAt) | sort | first %}

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Damien Dart’s Notes (www.robotinaponcho.net)</title>
<link href="{{ urlBase }}notes/"/>
<link href="{{ urlBase }}notes/index.atom" rel="self"/>
<link href="{{ site.urlBase }}notes/"/>
<link href="{{ site.urlBase }}notes/index.atom" rel="self"/>
<updated>{{ lastUpdated | date('c') }}</updated>
<author>
<email>{{ authorEmail }}</email>
<uri>{{ urlBase }}</uri>
<name>{{ author | smartypants }}</name>
<email>{{ site.authorEmail }}</email>
<uri>{{ site.urlBase }}</uri>
<name>{{ site.author | smartypants }}</name>
</author>
<id>{{ urlBase }}notes/index.atom</id>
<id>{{ site.urlBase }}notes/index.atom</id>
{% for note in items %}
<entry>
<id>{{ urlBase }}{{ note.metadata.slug }}</id>
<link href="{{ urlBase }}{{ note.metadata.slug }}"/>
<id>{{ site.urlBase }}{{ note.metadata.slug }}</id>
<link href="{{ site.urlBase }}{{ note.metadata.slug }}"/>
<published>{{ note.metadata.git.createdAt | date('c') }}</published>
<summary type="html">
<![CDATA[ {{ note.getContent | markdown | raw }} <hr><p><em><b>Last updated</b>: {{ note.metadata.git.updatedAt | date('c') }}</em></p> ]]>
Expand Down
6 changes: 3 additions & 3 deletions src/ssg/input/notes/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@

{% block content %}
<p>My notes, wiki, digital garden, whatever one of these things are
called nowadays. An <a href="{{ urlBase }}notes/index.atom">Atom
called nowadays. An <a href="{{ site.urlBase }}notes/index.atom">Atom
feed</a> is also available.</p>

<h2>Topics</h2>
<ul class="tag-list" role="list">
{% for category, notes in collections | filter((v, k) => k != 'notes') %}
{% for category, notes in site.collections | filter((v, k) => k != 'notes') %}
<li><a href="#{{ category }}">{{ categoryNames[category] ?? category | smartypants }}</a></li>
{% endfor %}
</ul>


{% for category, notes in collections | filter((v, k) => k != 'notes') %}
{% for category, notes in site.collections | filter((v, k) => k != 'notes') %}
<h2 id="{{ category }}">{{ categoryNames[category] ?? category | smartypants }}</h2>

<ul class="notes-list u-stack" role="list">
Expand Down
8 changes: 4 additions & 4 deletions src/ssg/input/projects/flippywindow/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% extends '.templates/base.html.twig' %}

{% set description = 'Download FlippyWindow, a simple Windows application that non-destructively flips any part of your screen.' %}
{% set metaOpengraphImage = urlBase ~ 'projects/flippywindow/opengraph.' ~ releaseTimestamp ~ '.png' %}
{% set metaOpengraphImage = site.urlBase ~ 'projects/flippywindow/opengraph.' ~ site.releaseTimestamp ~ '.png' %}
{% set section = 'projects' %}

{% block content %}
Expand All @@ -20,7 +20,7 @@
alt="A screenshot of FlippyWindow over Adobe Photoshop."
class="content__container__full-width-image"
height="557"
src="/projects/flippywindow/screenshot.{{ releaseTimestamp }}.png"
src="/projects/flippywindow/screenshot.{{ site.releaseTimestamp }}.png"
title="A screenshot of FlippyWindow over Adobe Photoshop."
width="776"
>
Expand Down Expand Up @@ -118,8 +118,8 @@
{%- endapply %}
{%
include '.templates/partials/sharing-links.html.twig' with {
'link': urlBase ~ slug,
'twitterAuthor': metaTwitterAuthor | replace({ '@': '' }),
'link': site.urlBase ~ slug,
'twitterAuthor': site.metaTwitterAuthor | replace({ '@': '' }),
'twitterText': 'Download FlippyWindow, by @damiendart, a simple Windows application that non-destructively flips any part of your screen.'
}
%}
Expand Down
2 changes: 1 addition & 1 deletion src/ssg/input/sitemap.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% block content %}
<ul>
{%
for entry in sitemapEntries
for entry in site.sitemapEntries
| filter(entry => 'pdf' not in entry.slug and 'sitemap' not in entry.slug and not (entry.slug matches '#notes/.+#'))
| sort((a, b) => a.slug <=> b.slug)
%}
Expand Down
4 changes: 2 additions & 2 deletions src/ssg/input/sitemap.xml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for entry in sitemapEntries | sort((a, b) => a.slug <=> b.slug) %}
<url><loc>{{ urlBase }}{{ entry.slug }}</loc></url>
{% for entry in site.sitemapEntries | sort((a, b) => a.slug <=> b.slug) %}
<url><loc>{{ site.urlBase }}{{ entry.slug }}</loc></url>
{% endfor %}
</urlset>
{% endapply %}

0 comments on commit b88ef28

Please sign in to comment.