💾 Archived View for rwv.io › src › atom.xml captured on 2021-12-03 at 14:04:38.

View Raw

More Information

⬅️ Previous capture (2021-11-30)

➡️ Next capture (2024-02-05)

-=-=-=-=-=-=-

#!/usr/bin/env ruby

CAPSULE_NAME = 'RWV.io'
MAX_ENTRIES = 20

require('find')
require('builder')

ISO_STRFTIME = '%Y-%m-%dT%H:%MZ'

PATH_INFO = ENV['PATH_INFO']
SERVER_NAME = ENV['SERVER_NAME']
GEMINI_URL = ENV['GEMINI_URL']

ROOT_PATH = File.absolute_path(__FILE__ + '/../')

print "20 text/xml\r\n"

builder = Builder::XmlMarkup.new(indent: 2)
builder.instruct!
builder.feed(xmlns: 'http://www.w3.org/2005/Atom') do |feed|
  feed.title("#{CAPSULE_NAME}#{PATH_INFO == '/' ? '' : PATH_INFO}")
  feed.link(href: GEMINI_URL, ref: 'self')
  feed.link(href: GEMINI_URL.sub('/atom.xml', ''), ref: 'alternate')
  feed.id(GEMINI_URL)
  feed.author{|author| author.name(CAPSULE_NAME)}

  paths = []
  updated = nil
  Find.find(ROOT_PATH) do |absolute_path|
    path = absolute_path[(ROOT_PATH.length)..-1]
    next if File.executable?(absolute_path)
    next if PATH_INFO && !path.start_with?(PATH_INFO)
    next unless path =~ %r{/(\d{4})-(\d{2})-(\d{2})}

    if File.file?(absolute_path)
      paths << path
      mtime = File.mtime(absolute_path)
      updated ||= mtime
      updated = mtime if updated < mtime
    end
  end

  if updated
    feed.updated(updated.strftime(ISO_STRFTIME))
  end

  paths.sort[0..MAX_ENTRIES].each do |path|
    absolute_path = File.join(ROOT_PATH, path)
    published = path[%r{/(\d{4}-\d{2}-\d{2})}, 1]
    content = File.read(absolute_path, encoding: 'utf-8')
    next if content[/\#\s*DRAFT/]

    feed.entry do |entry|
      url = "gemini://#{SERVER_NAME}#{path}"
      entry.title(content[/\#\s*(.*)\n/, 1])
      entry.link(href: url)
      entry.id(url)
      entry.published(published)
      entry.updated("#{published}T00:00Z")
    end
  end
end

puts builder.target!