about summary refs log tree commit diff homepage
path: root/utils.jl
blob: 41d057f3b0d08dc4cd0442758157ee3e717aabcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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("<a href=/%s>%s</a>", 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"),
                 "<pre><code>" => "<pre><code class=plaintext>")
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])]
  "<a href=https://$adj>$(args[2])</a>"
end

hfun_github(args) = "<a href=https://github.com/$(args[1])>@$(args[1])</a>"
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

function hfun_fead()
  ads = readchomp(joinpath(path(:libs), "fead", "out.html"))
  """<h2>See Also</h2>
  <div class=fead>
  $(ads)
  </div>
  <small class=right>Generated by
    <a href=https://trong.loang.net/~cnx/fead>fead</a></small>
  """
end

function hfun_pep(args)
  postfix = length(args) < 2 ? "" : '#' * args[2]
  @sprintf("<a href=https://www.python.org/dev/peps/pep-%04d%s>PEP %s%s</a>",
           Meta.parse(args[1]), postfix, args[1], postfix)
end

function hfun_pip(args)
  content = length(args) < 2 ? "GH-" * args[1] : args[2]
  "<a href=https://github.com/pypa/pip/pull/$(args[1])>$content</a>"
end