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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + + +
+ +
+
+ +

Cats

+ + + +
+

Cats are neat. Look at these kittens!

+
+ + +
+
+ +

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/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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + +
+
+ +

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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + +
+
+ +

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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + + +
+

December 2019

+
+ + +
+
+ +

Cats

+ + + +
+

Cats are neat. Look at these kittens!

+
+ + +
+
+ +

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/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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + + +
+

2019

+ + +

+ December +

+ + + + +
+ + + + + + + + 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 + + + + + + +
+
+

gonzo journalism

+
+

By Hunter S. Thompson

+
+
+ + + +

Archive

+ + +
+

2019

+ + +

+ December +

+ + + + +
+ + + + + + + + + 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 %} +

{{ post.title }}

+{% else %} +

{{ post.title }}

+{% 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 %} +
+

{{ year.title }}

+ + {% for month in year.months %} +

+ {{ month.name }} +

+ + + + {% 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 %} +

+ {{ month.name }} +

+ + + +{% 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 %} + + + +
+
+

{{ site.title }}

+
+

By {{ site.author }}

+
+
+ + {% 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 @@ diff --git a/Tests/test-projects/expected/projects/linux.html b/Tests/test-projects/expected/projects/linux/index.html similarity index 100% rename from Tests/test-projects/expected/projects/linux.html rename to Tests/test-projects/expected/projects/linux/index.html diff --git a/Tests/test-public-subdirs/expected/about.html b/Tests/test-public-subdirs/expected/about/index.html similarity index 100% rename from Tests/test-public-subdirs/expected/about.html rename to Tests/test-public-subdirs/expected/about/index.html diff --git a/bin/test b/bin/test index c93ecef..f103280 100755 --- a/bin/test +++ b/bin/test @@ -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 diff --git a/public/missing.md b/public/missing.md index 7c57d9e..0b6be3a 100644 --- a/public/missing.md +++ b/public/missing.md @@ -1 +1,5 @@ +--- +Hide extension: no +--- +

four oh four

diff --git a/templates/posts-month.html b/templates/posts-month.html index 6a84d15..4b4c21f 100644 --- a/templates/posts-month.html +++ b/templates/posts-month.html @@ -1,6 +1,7 @@ {% extends "samhuri.net.html" %} {% block body %} +

{{ title }}

@@ -8,4 +9,5 @@ {% for post in posts %} {% include "partial-post.html" post %} {% endfor %} + {% endblock %} diff --git a/templates/posts-year.html b/templates/posts-year.html index c3206e6..0d154f8 100644 --- a/templates/posts-year.html +++ b/templates/posts-year.html @@ -14,7 +14,7 @@ {% for post in month.posts %}
  • {% if post.isLink %} - → {{ post.title }} + → {{ post.title }} {% else %} {{ post.title }} {% endif %} @@ -28,4 +28,5 @@ {% endfor %} + {% endblock %} diff --git a/templates/projects.html b/templates/projects.html index 2c48385..88c1650 100644 --- a/templates/projects.html +++ b/templates/projects.html @@ -1,5 +1,7 @@ {% extends "samhuri.net.html" %} + {% block body %} +

    Projects

    @@ -13,4 +15,5 @@

    + {% endblock %} diff --git a/templates/recent-posts.html b/templates/recent-posts.html index d122c34..6ad4593 100644 --- a/templates/recent-posts.html +++ b/templates/recent-posts.html @@ -1,9 +1,11 @@ {% extends "samhuri.net.html" %} {% block body %} +
    - {% for post in recentPosts %} - {% include "partial-post.html" post %} - {% endfor %} +{% for post in recentPosts %} + {% include "partial-post.html" post %} +{% endfor %}
    + {% endblock %}