using Printf: @sprintf using Franklin: convert_html, path function hfun_author_date() readchomp(`git log -1 --date=short --format=%ad $(locvar(:fd_rpath))`) end function hfun_abslink(args) rpath = args[1] @sprintf("%s", rpath, pagevar(rpath, :title)) end dir_url() = strip(dirname(locvar(:fd_url)), '/') message_id() = "%3C$(dir_url())@cnx%3E" hfun_comments() = @sprintf("https://lists.sr.ht/~cnx/site?search=%s:%s", "In-Reply-To", message_id()) function render_comments(template) prefix = joinpath(path(:libs), "formbox") mbox = joinpath(prefix, "mbox") template_path = joinpath(prefix, template) readchomp(`formbox $mbox $(message_id()) $template_path`) end function hfun_comments_rendered() return replace(render_comments("comment.html"), "
" => "
")
end

hfun_comment_rss_feed_url() = joinpath(dirname(locvar(:fd_full_url)),
                                       "comments.xml")

function hfun_comment_rss()
  rpath = joinpath(dir_url(), "comments.xml")
  open(joinpath(path(:site), rpath), "w") do feed
    write(feed, convert_html(readchomp(joinpath(path(:rss), "comments.xml"))))
  end
  hfun_comment_rss_feed_url()
end

hfun_comment_rss_items() = render_comments("comment.xml")

function hfun_fediring(args)
  adj = readlines(joinpath(path(:libs), "fediring"))[parse(Int, args[1])]
  "$(args[2])"
end

hfun_github(args) = "@$(args[1])"
hfun_job_url() = get(ENV, "JOB_URL", "https://builds.sr.ht/~cnx/site")

function hfun_mailto_comment()
  @sprintf("mailto:%s?%s=%s&%s=Re: %s", "~cnx/site@lists.sr.ht",
           "In-Reply-To", message_id(), "Subject", locvar(:title))
end

hfun_openring() = readchomp(joinpath(path(:libs), "openring", "out.html"))

function hfun_pep(args)
  postfix = length(args) < 2 ? "" : '#' * args[2]
  @sprintf("PEP %s%s",
           Meta.parse(args[1]), postfix, args[1], postfix)
end

function hfun_pip(args)
  content = length(args) < 2 ? "GH-" * args[1] : args[2]
  "$content"
end