Clean up post templates with "include"

This commit is contained in:
Sami Samhuri 2019-12-04 17:46:34 -08:00
parent 09e45c9617
commit 085949bd87
9 changed files with 20 additions and 102 deletions

View file

@ -128,17 +128,12 @@ final class PostsPlugin: Plugin {
try fileManager.createDirectory(at: monthDir, withIntermediateDirectories: true, attributes: nil)
let filename = "\(post.slug).html"
let postURL = monthDir.appendingPathComponent(filename)
let templateName = self.templateName(for: post)
let bodyHTML = markdownParser.html(from: post.bodyMarkdown)
let renderedPost = RenderedPost(post: post, body: bodyHTML)
let postHTML = try templateRenderer.renderTemplate(name: templateName, context: ["post": renderedPost.dictionary])
let postHTML = try templateRenderer.renderTemplate(name: "post", context: ["post": renderedPost.dictionary])
try postHTML.write(to: postURL, atomically: true, encoding: .utf8)
}
private func templateName(for post: Post) -> String {
post.isLink ? "post-link" : "post-text"
}
private func enumerateMarkdownFiles(directory: URL) throws -> [URL] {
return try fileManager.contentsOfDirectory(atPath: directory.path).flatMap { (filename: String) -> [URL] in
let fileURL = directory.appendingPathComponent(filename)

View file

@ -1,34 +0,0 @@
HANDLED {% if post.link %} -%>
<article class="container">
<header>
<h1><a href="<%= post.link %>">&rarr; <%= post.title %></a></h1>
<time><%= post.date %></time>
<a class="permalink" href="<%= post.url %>">&infin;</a>
</header>
<%- yield %>
</article>
<div class="row clearfix">
<p class="fin clearfix"><i class="fa fa-code"></i></p>
</div>
HANDLED <% } else if (typeof date != 'undefined' && date !== null) { -%>
<article class="container">
<header>
<h1><a href="<%= url %>"><%= title %></a></h1>
<time><%= date %></time>
</header>
<%- yield %>
</article>
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
WTF IS THIS?? MUST BE PROJECTS! <% } else if (typeof title != 'undefined' && title !== null) { -%>
<article class="container">
<h1><%= title %></h1>
<%- yield %>
</article>
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
HANDLED <% } else { -%>
<%- yield %>
<% } -%>

View file

@ -1,4 +1,5 @@
{% extends "samhuri.net.html" %}
{% block body %}
<article class="container">
<h1>{{ page.title }}</h1>

View file

@ -1,15 +1,11 @@
{% extends "samhuri.net.html" %}
{% block body %}
<article class="container">
<header>
<h1><a href="{{ post.link }}">&rarr; {{ post.title }}</a></h1>
<time>{{ post.formattedDate }}</time>
<a class="permalink" href="{{ post.url }}">&infin;</a>
<a class="permalink" href="{{ post.path }}">&infin;</a>
</header>
{{ post.body }}
</article>
<div class="row clearfix">
<p class="fin clearfix"><i class="fa fa-code"></i></p>
</div>
{% endblock %}

View file

@ -1,6 +1,3 @@
{% extends "samhuri.net.html" %}
{% block body %}
<article class="container">
<header>
<h1><a href="{{ post.path }}">{{ post.title }}</a></h1>
@ -11,4 +8,3 @@
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
{% endblock %}

View file

@ -0,0 +1,5 @@
{% if post.isLink %}
{% include "partial-post-link.html" %}
{% else %}
{% include "partial-post-text.html" %}
{% endif %}

5
templates/post.html Normal file
View file

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

View file

@ -2,23 +2,6 @@
{% block body %}
{% for post in posts %}
<article class="container">
<header>
{% if post.isLink %}
<h2><a href="{{ post.link }}">&rarr; {{ post.title }}</a></h2>
<time>{{ post.formattedDate }}</time>
<a class="permalink" href="{{ post.path }}">&infin;</a>
{% else %}
<h2><a href="{{ post.path }}">{{ post.title }}</a></h2>
<time>{{ post.formattedDate }}</time>
{% endif %}
</header>
{{ post.body }}
</article>
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
{% include "partial-post.html" post %}
{% endfor %}
{% endblock %}

View file

@ -1,38 +1,9 @@
{% extends "samhuri.net.html" %}
{% block body %}
<div class="container">
<% var posts = public._data.posts -%>
<% for (var i in posts) { -%>
<% var post = posts[i] -%>
<% if (post.link) { -%>
<article>
<header>
<h2><a href="<%= post.link %>"><%= post.title %></a></h2>
<time><%= post.date %></time>
</header>
<%- partial(post.url) %>
<p><a class="permalink" href="<%= post.url %>">&infin;</a></p>
</article>
<% } else { -%>
<article>
<header>
<h2><a href="<%= post.url %>"><%= post.title %></a></h2>
<time><%= post.date %></time>
</header>
<%- partial(post.url) %>
</article>
<% } -%>
<div class="row clearfix">
<p class="fin"><i class="fa fa-code"></i></p>
</div>
<% } -%>
{% for post in recentPosts %}
{% include "partial-post.html" post %}
{% endfor %}
</div>
{% endblock %}