diff --git a/SiteGenerator/Sources/SiteGenerator/Posts/PostWriter.swift b/SiteGenerator/Sources/SiteGenerator/Posts/PostWriter.swift
index 5fc2fb0..29276a8 100644
--- a/SiteGenerator/Sources/SiteGenerator/Posts/PostWriter.swift
+++ b/SiteGenerator/Sources/SiteGenerator/Posts/PostWriter.swift
@@ -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"
}
}
diff --git a/SiteGenerator/Sources/SiteGenerator/Projects/ProjectsPlugin.swift b/SiteGenerator/Sources/SiteGenerator/Projects/ProjectsPlugin.swift
index bfc5785..946c483 100644
--- a/SiteGenerator/Sources/SiteGenerator/Projects/ProjectsPlugin.swift
+++ b/SiteGenerator/Sources/SiteGenerator/Projects/ProjectsPlugin.swift
@@ -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)
}
}
diff --git a/SiteGenerator/Sources/SiteGenerator/Renderers/MarkdownRenderer.swift b/SiteGenerator/Sources/SiteGenerator/Renderers/MarkdownRenderer.swift
index e416a1b..a8b7976 100644
--- a/SiteGenerator/Sources/SiteGenerator/Renderers/MarkdownRenderer.swift
+++ b/SiteGenerator/Sources/SiteGenerator/Renderers/MarkdownRenderer.swift
@@ -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)
}
diff --git a/Tests/test-markdown/expected/index.html b/Tests/test-markdown/expected/hello/index.html
similarity index 100%
rename from Tests/test-markdown/expected/index.html
rename to Tests/test-markdown/expected/hello/index.html
diff --git a/Tests/test-markdown/in/public/index.md b/Tests/test-markdown/in/public/hello.md
similarity index 100%
rename from Tests/test-markdown/in/public/index.md
rename to Tests/test-markdown/in/public/hello.md
diff --git a/Tests/test-posts/expected/index.html b/Tests/test-posts/expected/index.html
new file mode 100644
index 0000000..126e3df
--- /dev/null
+++ b/Tests/test-posts/expected/index.html
@@ -0,0 +1,62 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: gonzo journalism
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ∞
+
+ Cats are neat. Look at these kittens!
+
+
+
+
+
+
+
+
+
+ ∞
+
+ We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/expected/posts/2019/12/cats/index.html b/Tests/test-posts/expected/posts/2019/12/cats/index.html
new file mode 100644
index 0000000..a4bd88a
--- /dev/null
+++ b/Tests/test-posts/expected/posts/2019/12/cats/index.html
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: Cats
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ∞
+
+ Cats are neat. Look at these kittens!
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/expected/posts/2019/12/fear-and-loathing/index.html b/Tests/test-posts/expected/posts/2019/12/fear-and-loathing/index.html
new file mode 100644
index 0000000..83881ca
--- /dev/null
+++ b/Tests/test-posts/expected/posts/2019/12/fear-and-loathing/index.html
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: Fear and Loathing in Las Vegas
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ∞
+
+ We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/expected/posts/2019/12/index.html b/Tests/test-posts/expected/posts/2019/12/index.html
new file mode 100644
index 0000000..bb88fe1
--- /dev/null
+++ b/Tests/test-posts/expected/posts/2019/12/index.html
@@ -0,0 +1,64 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: December 2019
+
+
+
+
+
+
+
+
+
+
+
+
December 2019
+
+
+
+
+
+
+
+
+
+ ∞
+
+ Cats are neat. Look at these kittens!
+
+
+
+
+
+
+
+
+
+ ∞
+
+ We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/expected/posts/2019/index.html b/Tests/test-posts/expected/posts/2019/index.html
new file mode 100644
index 0000000..7ab3614
--- /dev/null
+++ b/Tests/test-posts/expected/posts/2019/index.html
@@ -0,0 +1,67 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: 2019
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/expected/posts/index.html b/Tests/test-posts/expected/posts/index.html
new file mode 100644
index 0000000..84f335f
--- /dev/null
+++ b/Tests/test-posts/expected/posts/index.html
@@ -0,0 +1,71 @@
+
+
+
+
+
+
+
+
+ gonzo journalism: Archive
+
+
+
+
+
+
+
+
+
+
+Archive
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Tests/test-posts/in/posts/2019/12/cats.md b/Tests/test-posts/in/posts/2019/12/cats.md
new file mode 100644
index 0000000..4e78ddc
--- /dev/null
+++ b/Tests/test-posts/in/posts/2019/12/cats.md
@@ -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!
diff --git a/Tests/test-posts/in/posts/2019/12/fear-and-loathing.md b/Tests/test-posts/in/posts/2019/12/fear-and-loathing.md
new file mode 100644
index 0000000..62db99f
--- /dev/null
+++ b/Tests/test-posts/in/posts/2019/12/fear-and-loathing.md
@@ -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.
diff --git a/Tests/test-posts/in/site.json b/Tests/test-posts/in/site.json
new file mode 100644
index 0000000..7d0ae63
--- /dev/null
+++ b/Tests/test-posts/in/site.json
@@ -0,0 +1,5 @@
+{
+ "author": "Hunter S. Thompson",
+ "title": "gonzo journalism",
+ "url": "http://example.net"
+}
diff --git a/Tests/test-posts/in/templates/partial-post.html b/Tests/test-posts/in/templates/partial-post.html
new file mode 100644
index 0000000..1cbf952
--- /dev/null
+++ b/Tests/test-posts/in/templates/partial-post.html
@@ -0,0 +1,12 @@
+
+
+{% if post.isLink %}
+
+{% else %}
+
+{% endif %}
+
+ ∞
+
+ {{ post.body }}
+
diff --git a/Tests/test-posts/in/templates/post.html b/Tests/test-posts/in/templates/post.html
new file mode 100644
index 0000000..eaa3001
--- /dev/null
+++ b/Tests/test-posts/in/templates/post.html
@@ -0,0 +1,5 @@
+{% extends "site.html" %}
+
+{% block body %}
+{% include "partial-post.html" post %}
+{% endblock %}
diff --git a/Tests/test-posts/in/templates/posts-archive.html b/Tests/test-posts/in/templates/posts-archive.html
new file mode 100644
index 0000000..683a020
--- /dev/null
+++ b/Tests/test-posts/in/templates/posts-archive.html
@@ -0,0 +1,36 @@
+{% extends "site.html" %}
+
+{% block body %}
+
+{{ title }}
+
+{% for year in years %}
+
+
+
+ {% for month in year.months %}
+
+
+
+ {% for post in month.posts %}
+ -
+ {% if post.isLink %}
+ → {{ post.title }}
+ {% else %}
+ {{ post.title }}
+ {% endif %}
+
+ {% if post.isLink %}
+ ∞
+ {% endif %}
+
+ {% endfor %}
+
+
+ {% endfor %}
+
+{% endfor %}
+
+{% endblock %}
diff --git a/Tests/test-posts/in/templates/posts-month.html b/Tests/test-posts/in/templates/posts-month.html
new file mode 100644
index 0000000..8699f72
--- /dev/null
+++ b/Tests/test-posts/in/templates/posts-month.html
@@ -0,0 +1,13 @@
+{% extends "site.html" %}
+
+{% block body %}
+
+
+
{{ title }}
+
+
+{% for post in posts %}
+ {% include "partial-post.html" post %}
+{% endfor %}
+
+{% endblock %}
diff --git a/Tests/test-posts/in/templates/posts-year.html b/Tests/test-posts/in/templates/posts-year.html
new file mode 100644
index 0000000..abc0659
--- /dev/null
+++ b/Tests/test-posts/in/templates/posts-year.html
@@ -0,0 +1,32 @@
+{% extends "site.html" %}
+
+{% block body %}
+
+
+
{{ title }}
+
+{% for month in months %}
+
+
+
+ {% for post in month.posts %}
+ -
+ {% if post.isLink %}
+ → {{ post.title }}
+ {% else %}
+ {{ post.title }}
+ {% endif %}
+
+ {% if post.isLink %}
+ ∞
+ {% endif %}
+
+ {% endfor %}
+
+
+{% endfor %}
+
+
+{% endblock %}
diff --git a/Tests/test-posts/in/templates/recent-posts.html b/Tests/test-posts/in/templates/recent-posts.html
new file mode 100644
index 0000000..fdd96d5
--- /dev/null
+++ b/Tests/test-posts/in/templates/recent-posts.html
@@ -0,0 +1,11 @@
+{% extends "site.html" %}
+
+{% block body %}
+
+
+{% for post in recentPosts %}
+ {% include "partial-post.html" post %}
+{% endfor %}
+
+
+{% endblock %}
diff --git a/Tests/test-posts/in/templates/site.html b/Tests/test-posts/in/templates/site.html
new file mode 100644
index 0000000..ce26177
--- /dev/null
+++ b/Tests/test-posts/in/templates/site.html
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+ {% if title %}
+ {{ site.title }}: {{ title }}
+ {% elif page.title %}
+ {{ site.title }}: {{ page.title }}
+ {% else %}
+ {{ site.title }}
+ {% endif %}
+
+{% for style in styles %}
+
+{% endfor %}
+
+
+
+
+
+ {% block body %}{{ body }}{% endblock %}
+
+
+
+{% for script in scripts %}
+
+{% endfor %}
+
+
diff --git a/Tests/test-projects/expected/projects/index.html b/Tests/test-projects/expected/projects/index.html
index 6caec2d..e110c53 100644
--- a/Tests/test-projects/expected/projects/index.html
+++ b/Tests/test-projects/expected/projects/index.html
@@ -8,7 +8,7 @@