Comparison

mod_pubsub_forgejo/format.lib.lua @ 6203:131b8bfbefb4

mod_pubsub_forgejo: new module for forgejo webhooks
author nicoco <nicoco@nicoco.fr>
date Mon, 17 Feb 2025 23:28:05 +0100
comparison
equal deleted inserted replaced
6202:6d5a19bdd718 6203:131b8bfbefb4
1 local st = require "util.stanza";
2 local datetime = require "util.datetime";
3
4 local function shorten(x) return string.sub(x, 1, -32) end
5
6 local function firstline(x) return x:match("^[^\r\n]*") end
7
8 local function branch(x) return string.sub(x, 12) end
9
10 local function tag(x) return string.sub(x, 11) end
11
12 local function noop(x) return x end
13
14 local filters = {
15 shorten = shorten,
16 firstline = firstline,
17 branch = branch,
18 tag = tag
19 }
20
21 local render = require"util.interpolation".new("%b{}", noop, filters);
22
23 local function get_item(data, templates)
24 local function render_tpl(name) return render(templates[name], {data = data}) end
25
26 local now = datetime.datetime()
27 local id = render(templates["id"], {data = data})
28 -- LuaFormatter off
29 return st.stanza("item", {id = id, xmlns = "http://jabber.org/protocol/pubsub"})
30 :tag("entry", {xmlns = "http://www.w3.org/2005/Atom"})
31 :tag("id"):text(id):up()
32 :tag("title"):text(render_tpl("title")):up()
33 :tag("content", {type = "text"}):text(render_tpl("content")):up()
34 :tag("link", {rel = "alternate", href = render_tpl("link")}):up()
35 :tag("published"):text(now):up()
36 :tag("updated"):text(now):up()
37 :tag("author")
38 :tag("name")
39 :text(data.sender.username):up()
40 :tag("email"):text(data.sender.email)
41 -- LuaFormatter on
42 end
43
44 return get_item