main
 1-- inject-time.lua — Add a <time> element from org-mode date metadata
 2-- Enables David Larlet's seasonal list markers via body:has(time[datetime])
 3--
 4-- Org exports date as: <p class="date">Date: 2026-04-01 Tue 00:00</p>
 5-- or in the postamble. We also check <p class="subtitle"> and
 6-- timestamp spans from inline dates.
 7--
 8-- Injects: <time datetime="2026-04-01" hidden></time> into <body>
 9
10-- Try to find a date from various org export locations
11
12-- 1. Check for .date paragraph (postamble)
13date_found = nil
14date_els = HTML.select(page, "p.date")
15i = 1
16while date_els[i] do
17  text = HTML.inner_text(date_els[i])
18  d = Regex.find_all(text, "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]")
19  if d[1] then
20    date_found = d[1]
21  end
22  i = i + 1
23end
24
25-- 2. Check for timestamp spans (inline org dates)
26if not date_found then
27  ts = HTML.select(page, "time.timestamp")
28  if ts[1] then
29    date_found = HTML.get_attribute(ts[1], "datetime")
30  end
31end
32
33-- 3. Check <meta> tags (if we ever add date meta)
34if not date_found then
35  metas = HTML.select(page, "meta")
36  i = 1
37  while metas[i] do
38    name = HTML.get_attribute(metas[i], "name")
39    if name == "date" then
40      date_found = HTML.get_attribute(metas[i], "content")
41    end
42    i = i + 1
43  end
44end
45
46-- Inject hidden <time> element if we found a date
47if date_found then
48  -- Normalize to YYYY-MM-DD
49  clean = Regex.find_all(date_found, "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]")
50  if clean[1] then
51    time_el = HTML.create_element("time")
52    HTML.set_attribute(time_el, "datetime", clean[1])
53    HTML.set_attribute(time_el, "hidden", "")
54    HTML.append_child(HTML.select_one(page, "body"), time_el)
55  end
56end