all repos

olexsmir.xyz @ fb75056d4a957152d09ac68bcb84773e41072209

my site, yes, i like lua

olexsmir.xyz/lua/lego/rss.lua(view raw)

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
local a = require "lego.html.attribute"
local formatDate = require("lego.date").date
local h = require "lego.html"
local rss = {}

function rss.escape_html(html)
  local map = {
    ["&"] = "&",
    ["<"] = "&lt;",
    [">"] = "&gt;",
    ['"'] = "&quot;",
    ["'"] = "&#39;",
  }

  html = html:gsub("[%z\1-\8\11-\12\14-\31]", "") -- remove control chars
  return (html:gsub("[&<>\"']", function(c)
    return map[c]
  end))
end

---@param config {feed_url:string, home_url:string, title:string, name:string, email:string, subtitle:string}
---@param posts lego.Post[]
---@return string
function rss.rss(posts, config)
  local entries = vim
    .iter(posts)
    ---@param post lego.Post
    :map(function(post)
      return h.el("entry", {}, {
        h.title({}, { h.text(post.meta.title) }),
        h.link { a.href(config.home_url .. "/" .. post.meta.slug) },
        h.el("id", {}, { h.text(config.home_url .. "/" .. post.meta.slug) }),
        h.el("updated", {}, { h.text(formatDate(post.meta.date)) }),
        h.el("content", { a.attr("type", "html") }, { h.raw(rss.escape_html(post.content)) }),
      })
    end)
    :totable()

  return [[<?xml version="1.0" encoding="utf-8"?>]]
    .. h.render(h.el("feed", { a.attr("xmlns", "http://www.w3.org/2005/Atom") }, {
      h.title({}, { h.text(config.title) }),
      h.el("subtitle", {}, { h.text(config.subtitle) }),
      h.el("id", {}, { h.text(config.home_url .. "/") }),
      h.link { a.href(config.home_url), a.attr("rel", "alternate") },
      h.link {
        a.href(config.feed_url),
        a.attr("rel", "self"),
        a.attr("type", "application/atom+xml"),
      },
      h.el("updated", {}, { h.text(formatDate(posts[1].meta.date)) }),
      h.el("author", {}, {
        h.el("name", {}, { h.text(config.name) }),
        h.el("email", {}, { h.text(config.email) }),
      }),
      unpack(entries),
    }))
end

return rss