Hide .html extensions on all pages by default

This uses the same old page-title/index.html trick that was used before.
This commit is contained in:
Sami Samhuri 2019-12-10 00:27:56 -08:00
parent d184ed06fa
commit 652d192560
30 changed files with 564 additions and 18 deletions

View file

@ -25,7 +25,7 @@ final class PostWriter {
}
func urlPathForPost(date: Date, slug: String) -> String {
urlPath(year: date.year, month: Month(date.month)).appending("/\(slug).html")
urlPath(year: date.year, month: Month(date.month)).appending("/\(slug)")
}
}
@ -47,7 +47,7 @@ extension PostWriter {
}
private func filePath(date: Date, slug: String) -> String {
"/\(date.year)/\(Month(date.month).padded)/\(slug).html"
"/\(date.year)/\(Month(date.month).padded)/\(slug)/index.html"
}
}

View file

@ -19,13 +19,13 @@ private struct Projects: Codable {
final class ProjectsPlugin: Plugin {
let fileManager: FileManager = .default
let path: String
let outputPath: String
var projects: [Project] = []
var sourceURL: URL!
init(path: String = "projects") {
self.path = path
init(outputPath: String = "projects") {
self.outputPath = outputPath
}
func setUp(sourceURL: URL) throws {
@ -33,7 +33,7 @@ final class ProjectsPlugin: Plugin {
let projectsURL = sourceURL.appendingPathComponent("projects.json")
if fileManager.fileExists(atPath: projectsURL.path) {
self.projects = try Projects.decode(from: projectsURL).projects.map { project in
Project(title: project.title, description: project.description, path: "/\(path)/\(project.title).html")
Project(title: project.title, description: project.description, path: "/\(outputPath)/\(project.title)")
}
}
}
@ -43,7 +43,7 @@ final class ProjectsPlugin: Plugin {
return
}
let projectsDir = targetURL.appendingPathComponent(path)
let projectsDir = targetURL.appendingPathComponent(outputPath)
try fileManager.createDirectory(at: projectsDir, withIntermediateDirectories: true, attributes: nil)
let projectsURL = projectsDir.appendingPathComponent("index.html")
let projectsHTML = try templateRenderer.renderTemplate(name: "projects", context: [
@ -53,12 +53,12 @@ final class ProjectsPlugin: Plugin {
try projectsHTML.write(to: projectsURL, atomically: true, encoding: .utf8)
for project in projects {
let filename = "\(project.title).html"
let projectURL = projectsDir.appendingPathComponent(filename)
let projectURL = projectsDir.appendingPathComponent("\(project.title)/index.html")
let projectHTML = try templateRenderer.renderTemplate(name: "project", context: [
"title": "\(project.title)",
"project": project,
])
try fileManager.createDirectory(at: projectURL.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try projectHTML.write(to: projectURL, atomically: true, encoding: .utf8)
}
}

View file

@ -9,6 +9,7 @@ import Foundation
import Ink
public final class MarkdownRenderer: Renderer {
let fileManager: FileManager = .default
let markdownParser = MarkdownParser()
public func canRenderFile(named filename: String, withExtension ext: String) -> Bool {
@ -17,13 +18,21 @@ public final class MarkdownRenderer: Renderer {
/// Parse Markdown and render it as HTML, running it through a Stencil template.
public func render(fileURL: URL, targetDir: URL, templateRenderer: TemplateRenderer) throws {
let mdFilename = fileURL.lastPathComponent
let htmlFilename = mdFilename.replacingOccurrences(of: ".md", with: ".html")
let htmlURL = targetDir.appendingPathComponent(htmlFilename)
let bodyMarkdown = try String(contentsOf: fileURL, encoding: .utf8)
let bodyHTML = markdownParser.html(from: bodyMarkdown).trimmingCharacters(in: .whitespacesAndNewlines)
let metadata = try markdownMetadata(from: fileURL)
let pageHTML = try templateRenderer.renderPage(bodyHTML: bodyHTML, metadata: metadata)
let mdFilename = fileURL.lastPathComponent
let htmlPath: String
if metadata["Hide extension"]?.lowercased() == "no" || mdFilename == "index.md" {
htmlPath = mdFilename.replacingOccurrences(of: ".md", with: ".html")
}
else {
htmlPath = mdFilename.replacingOccurrences(of: ".md", with: "/index.html")
}
let htmlURL = targetDir.appendingPathComponent(htmlPath)
try fileManager.createDirectory(at: htmlURL.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try pageHTML.write(to: htmlURL, atomically: true, encoding: .utf8)
}

View file

@ -0,0 +1,62 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: gonzo journalism</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<div class="container">
<article>
<header>
<h2>&rarr; <a href="https://placekitten.com">Cats</a></h2>
<time>18th December, 2019</time>
<a class="permalink" href="/posts/2019/12/cats">&infin;</a>
</header>
<p>Cats are neat. Look at these kittens!</p>
</article>
<article>
<header>
<h2><a href="/posts/2019/12/fear-and-loathing">Fear and Loathing in Las Vegas</a></h2>
<time>9th December, 2019</time>
<a class="permalink" href="/posts/2019/12/fear-and-loathing">&infin;</a>
</header>
<p>We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.</p>
</article>
</div>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,44 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: Cats</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<article>
<header>
<h2>&rarr; <a href="https://placekitten.com">Cats</a></h2>
<time>18th December, 2019</time>
<a class="permalink" href="/posts/2019/12/cats">&infin;</a>
</header>
<p>Cats are neat. Look at these kittens!</p>
</article>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,44 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: Fear and Loathing in Las Vegas</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<article>
<header>
<h2><a href="/posts/2019/12/fear-and-loathing">Fear and Loathing in Las Vegas</a></h2>
<time>9th December, 2019</time>
<a class="permalink" href="/posts/2019/12/fear-and-loathing">&infin;</a>
</header>
<p>We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.</p>
</article>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,64 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: December 2019</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<div class="month">
<h1>December 2019</h1>
</div>
<article>
<header>
<h2>&rarr; <a href="https://placekitten.com">Cats</a></h2>
<time>18th December, 2019</time>
<a class="permalink" href="/posts/2019/12/cats">&infin;</a>
</header>
<p>Cats are neat. Look at these kittens!</p>
</article>
<article>
<header>
<h2><a href="/posts/2019/12/fear-and-loathing">Fear and Loathing in Las Vegas</a></h2>
<time>9th December, 2019</time>
<a class="permalink" href="/posts/2019/12/fear-and-loathing">&infin;</a>
</header>
<p>We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.</p>
</article>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,67 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: 2019</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<div class="year">
<h1>2019</h1>
<h2>
<a href="/posts/2019/12">December</a>
</h2>
<ul class="archive">
<li>
<a href="https://placekitten.com">&rarr; Cats</a>
<time>18 Dec</time>
<a class="permalink" href="/posts/2019/12/cats">&infin;</a>
</li>
<li>
<a href="/posts/2019/12/fear-and-loathing">Fear and Loathing in Las Vegas</a>
<time>9 Dec</time>
</li>
</ul>
</div>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,71 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>gonzo journalism: Archive</title>
</head>
<body>
<header>
<div class="title">
<h1><a href="/">gonzo journalism</a></h1>
<br>
<h4>By <a href="/about">Hunter S. Thompson</a></h4>
</div>
</header>
<h1>Archive</h1>
<div class="year">
<h2><a href="/posts/2019">2019</a></h2>
<h3>
<a href="/posts/2019/12">December</a>
</h3>
<ul class="archive">
<li>
<a href="https://placekitten.com">&rarr; Cats</a>
<time>18 Dec</time>
<a class="permalink" href="/posts/2019/12/cats">&infin;</a>
</li>
<li>
<a href="/posts/2019/12/fear-and-loathing">Fear and Loathing in Las Vegas</a>
<time>9 Dec</time>
</li>
</ul>
</div>
<footer class="container">
&copy; 2006 - 2019 <a href="/about">Hunter S. Thompson</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,10 @@
---
Title: Cats
Author: Sami Samhuri
Date: 18th December, 2019
Link: https://placekitten.com
Timestamp: 1576656168
Tags: cats
---
Cats are neat. Look at these kittens!

View file

@ -0,0 +1,9 @@
---
Title: Fear and Loathing in Las Vegas
Author: Hunter S. Thompson
Date: 9th December, 2019
Timestamp: 1575964368
Tags: fear, loathing, las vegas
---
We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.

View file

@ -0,0 +1,5 @@
{
"author": "Hunter S. Thompson",
"title": "gonzo journalism",
"url": "http://example.net"
}

View file

@ -0,0 +1,12 @@
<article>
<header>
{% if post.isLink %}
<h2>&rarr; <a href="{{ post.link }}">{{ post.title }}</a></h2>
{% else %}
<h2><a href="{{ post.path }}">{{ post.title }}</a></h2>
{% endif %}
<time>{{ post.formattedDate }}</time>
<a class="permalink" href="{{ post.path }}">&infin;</a>
</header>
{{ post.body }}
</article>

View file

@ -0,0 +1,5 @@
{% extends "site.html" %}
{% block body %}
{% include "partial-post.html" post %}
{% endblock %}

View file

@ -0,0 +1,36 @@
{% extends "site.html" %}
{% block body %}
<h1>{{ title }}</h1>
{% for year in years %}
<div class="year">
<h2><a href="{{ year.path }}">{{ year.title }}</a></h2>
{% for month in year.months %}
<h3>
<a href="{{ month.path }}">{{ month.name }}</a>
</h3>
<ul class="archive">
{% for post in month.posts %}
<li>
{% if post.isLink %}
<a href="{{ post.link }}">&rarr; {{ post.title }}</a>
{% else %}
<a href="{{ post.path }}">{{ post.title }}</a>
{% endif %}
<time>{{ post.day }} {{ month.abbreviation }}</time>
{% if post.isLink %}
<a class="permalink" href="{{ post.path }}">&infin;</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endfor %}
</div>
{% endfor %}
{% endblock %}

View file

@ -0,0 +1,13 @@
{% extends "site.html" %}
{% block body %}
<div class="month">
<h1>{{ title }}</h1>
</div>
{% for post in posts %}
{% include "partial-post.html" post %}
{% endfor %}
{% endblock %}

View file

@ -0,0 +1,32 @@
{% extends "site.html" %}
{% block body %}
<div class="year">
<h1>{{ title }}</h1>
{% for month in months %}
<h2>
<a href="{{ month.path }}">{{ month.name }}</a>
</h2>
<ul class="archive">
{% for post in month.posts %}
<li>
{% if post.isLink %}
<a href="{{ post.link }}">&rarr; {{ post.title }}</a>
{% else %}
<a href="{{ post.path }}">{{ post.title }}</a>
{% endif %}
<time>{{ post.day }} {{ month.abbreviation }}</time>
{% if post.isLink %}
<a class="permalink" href="{{ post.path }}">&infin;</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endfor %}
</div>
{% endblock %}

View file

@ -0,0 +1,11 @@
{% extends "site.html" %}
{% block body %}
<div class="container">
{% for post in recentPosts %}
{% include "partial-post.html" post %}
{% endfor %}
</div>
{% endblock %}

View file

@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
{% if title %}
<title>{{ site.title }}: {{ title }}</title>
{% elif page.title %}
<title>{{ site.title }}: {{ page.title }}</title>
{% else %}
<title>{{ site.title }}</title>
{% endif %}
{% for style in styles %}
<link rel="stylesheet" href="{{ style }}">
{% endfor %}
</head>
<body>
<header>
<div class="title">
<h1><a href="/">{{ site.title }}</a></h1>
<br>
<h4>By <a href="/about">{{ site.author }}</a></h4>
</div>
</header>
{% block body %}{{ body }}{% endblock %}
<footer class="container">
&copy; 2006 - {{ currentYear }} <a href="/about">{{ site.author }}</a>
</footer>
{% for script in scripts %}
<script defer src="{{ script }}"></script>
{% endfor %}
</body>
</html>

View file

@ -8,7 +8,7 @@
<ul>
<li><a href="/projects/linux.html">linux</a></li>
<li><a href="/projects/linux">linux</a></li>
</ul>
</body>

View file

@ -4,6 +4,6 @@ set -e
for site in Tests/test-*; do
bin/compile "$site/in" "$site/actual" # >/dev/null
diff -ru "$site/expected" "$site/actual"
diff -bru "$site/expected" "$site/actual"
rm -r "$site/actual"
done

View file

@ -1 +1,5 @@
---
Hide extension: no
---
<p align="center">four oh four</p>

View file

@ -1,6 +1,7 @@
{% extends "samhuri.net.html" %}
{% block body %}
<div class="container">
<h1>{{ title }}</h1>
</div>
@ -8,4 +9,5 @@
{% for post in posts %}
{% include "partial-post.html" post %}
{% endfor %}
{% endblock %}

View file

@ -14,7 +14,7 @@
{% for post in month.posts %}
<li>
{% if post.isLink %}
<a href="{{ post.path }}">&rarr; {{ post.title }}</a>
<a href="{{ post.link }}">&rarr; {{ post.title }}</a>
{% else %}
<a href="{{ post.path }}">{{ post.title }}</a>
{% endif %}
@ -28,4 +28,5 @@
{% endfor %}
</div>
{% endblock %}

View file

@ -1,5 +1,7 @@
{% extends "samhuri.net.html" %}
{% block body %}
<article class="container">
<h1>Projects</h1>
@ -13,4 +15,5 @@
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
{% endblock %}

View file

@ -1,9 +1,11 @@
{% extends "samhuri.net.html" %}
{% block body %}
<div class="container">
{% for post in recentPosts %}
{% for post in recentPosts %}
{% include "partial-post.html" post %}
{% endfor %}
{% endfor %}
</div>
{% endblock %}