I’m in the process of migrating from Jekyll to Franklin for my static website (Garrek.org) hosted on GitHub. (In the near future, I will have LaTeX and plots and things, which Franklin seems to excel at). Jekyll has some nice features for retrieving site attributes and I’m trying to do something similar with Franklin. For example, my landing page is simply a list of all of my blog posts:
Jekyll snippet
{% for post in site.posts %}
<article>
<h2>
<a href="{{ post.url}}">{{ post.title }}</a>
</h2>
<p>{{ post.date | date_to_string }}</p>
{{ post.content }}
</article>
{% endfor %}
I can almost replicate this by modifying hfun_recentblogposts()
in utils.jl
on the Julia website. But I can’t grab the post contents. I wrote a function that writes out the following:
Julia snippet within a function in utils.jl
write(io, """
<article>
<h2><a href="$url">$title</a>
</h2>
<p>
$sdate
<a class="permalink" title="Permalink" href="$url">
⚓︎
︎</a>
</p>
{{include $surl}}
</article>
""")
but I don’t know how to use include
to get the post contents. ($surl
gives the path of a post) I’m sure it’s not difficult, but I can’t find instructions in the Franklin.jl documentation.
- How do I get the contents of a blog post in my example?
- Is writing a Julia function like this in
utils.jl
the best way to go about implementing this sort of thing, or would it be better to put code in an html layout or something?
I understand that Franklin will work differently than Jekyll, and perhaps what I’m trying to do is not the best way to go about it; I’m struggling with how Franklin wants me to structure certain things.
My version of the "recent posts" function, which works except for grabbing the body of a post:
function hfun_recentposts()
curyear = Dates.Year(Dates.today()).value
ntofind = 2
nfound = 0
recent = Vector{Pair{String,Date}}(undef, ntofind)
for year in curyear:-1:2020
for month in 12:-1:1
ms = "0"^(1-div(month, 10)) * "$month"
base = joinpath("posts", "$year", "$ms")
isdir(base) || continue
posts = filter!(p -> endswith(p, ".md"), readdir(base))
days = zeros(Int, length(posts))
surls = Vector{String}(undef, length(posts))
for (i, post) in enumerate(posts)
ps = splitext(post)[1]
surl = "posts/$year/$ms/$ps"
surls[i] = surl
pubdate = pagevar(surl, :published)
days[i] = isnothing(pubdate) ?
1 : day(Date(pubdate, dateformat"d u Y"))
end
# go over month post in antichronological orders
sp = sortperm(days, rev=true)
for (i, surl) in enumerate(surls[sp])
recent[nfound + 1] = (surl => Date(year, month, days[sp[i]]))
nfound += 1
nfound == ntofind && break
end
nfound == ntofind && break
end
nfound == ntofind && break
end
#
io = IOBuffer()
for (surl, date) in recent
url = "/$surl/"
title = pagevar(surl, :title)
title === nothing && (title = "Untitled")
sdate = "$(day(date)) $(monthname(date)) $(year(date))"
write(io, """
<article>
<h2><a href="$url">$title</a>
</h2>
<p>
$sdate
<a class="permalink" title="Permalink" href="$url">
⚓︎
︎</a>
</p>
{{include $surl.html}}
</article>
""")
end
return String(take!(io))
end
Edit: clarified my question.