-- atom.lua -- SPDX-FileCopyrightText: © 2020 Daniil Baturin -- SPDX-FileCopyrightText: © 2023–2025 toastal -- SPDX-License-Identifier: MIT -- Forked from upstream atom.lua Plugin.require_version("4.0.0") data = {} date_input_formats = soupault_config["index"]["date_formats"] feed_file = config["feed_file"] custom_options = soupault_config["custom_options"] if not Table.has_key(custom_options, "site_url") then Plugin.exit([[Atom feed generation is not enabled in the config. If you want to enable it, add custom_options["atom_feeds = true"] ]]) end if not Table.has_key(custom_options, "site_url") then Plugin.fail([[custom_options["site_url"] option is required when feed generation is enabled]]) end data["site_url"] = custom_options["site_url"] data["soupault_version"] = Plugin.soupault_version() data["feed"] = {} data["feed"]["title"] = custom_options["site_headline"] data["feed"]["id"] = custom_options["site_url"] data["feed"]["url"] = Sys.join_path_unix(custom_options["site_url"], Sys.join_path_unix(target_dir, feed_file)) if Table.has_key(custom_options, "site_icon") then data["feed"]["icon"] = Sys.join_path_unix(custom_options["site_url"], custom_options["site_icon"]) end if Table.has_key(custom_options, "site_logo") then data["feed"]["logo"] = Sys.join_path_unix(custom_options["site_url"], custom_options["site_logo"]) end function in_section(entry) return (entry["nav_path"][1] == config["use_section"]) end function tags_match(entry) if config["use_tag"] then return (Regex.match(entry["tags"], format("\\b%s\\b", config["use_tag"]))) else return 1 end end entries = {} peers = Table.get_key_default(soupault_config["custom_options"], "peers", {}) function find_best_peer(needle) local idx = 1 -- Try by name while peers[idx] do local peer = peers[idx] local name = peer["name"] if name == needle then return peer end idx = idx + 1 end -- Try by aliases idx = 1 while peers[idx] do local peer = peers[idx] local aliases = peer["aliases"] if Value.is_list(aliases) then local jdx = 1 while aliases[jdx] do local alias = aliases[jdx] if alias == needle then return peer end jdx = jdx + 1 end end idx = idx + 1 end return nil end function find_or_make_peer(needle) local peer = find_best_peer(needle) if peer == nil then peer = {} peer["name"] = needle end return peer end -- Original, unfiltered entries index local n = 1 -- Index of the new array of entries we are building local m = 1 local count = size(site_index) if count < 1 then Plugin.fail("Atom feeds enabled, but no feed items. Add some items or fix configuration.") end while n <= count do entry = site_index[n] if (in_section(entry) and tags_match(entry)) then entry["headline"] = HTML.strip_tags(HTML.parse(entry["headline"])) if entry["alternative_headline"] then entry["alternative_headline"] = HTML.strip_tags(HTML.parse(entry["alternative_headline"])) end if entry["date_published"] then entry["date_published"] = Date.reformat(entry["date_published"], date_input_formats, "%Y-%m-%dT%H:%M:%S%:z") end if entry["date_amended"] then entry["date_amended"] = Date.reformat(entry["date_amended"], date_input_formats, "%Y-%m-%dT%H:%M:%S%:z") end if entry["url"] then entry["url"] = String.url_encode(entry["url"], {"/", "?", "&", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}) if Table.get_key_default(config, "trailing_slash", false) then entry["url"] = entry["url"] .. "/" end end -- turn string names into peers Table.apply_to_values(find_or_make_peer, entry["authors"]) Table.apply_to_values(find_or_make_peer, entry["contributors"]) Table.apply_to_values(find_or_make_peer, entry["editors"]) Table.apply_to_values(find_or_make_peer, entry["translators"]) entries[m] = entry m = m + 1 end n = n + 1 end if (soupault_config["index"]["sort_descending"] or (not Table.has_key(soupault_config["index"], "sort_descending"))) then data["feed"]["date_last_published"] = entries[1]["date_published"] else data["feed"]["date_last_published"] = entries[size(entries)]["date_published"] end data["entries"] = entries data["trailing_slash"] = Table.get_key_default(config, "trailing_slash", false) local feed_template = Sys.read_file(config["template"]) local feed = String.render_template(feed_template, data) Sys.write_file(Sys.join_path(target_dir, feed_file), String.trim(feed))