💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › gen-rss.rb.txt captured on 2024-08-31 at 13:03:05.

View Raw

More Information

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

#! /usr/bin/ruby

#################
# LINE STARTS:
#  h  - title
#  l  - link
#  de - description
#  dt - date
#
# SUBSTITIONS:
#  [HOME]     - homepage address
#  [PROT]     - protocol part of URL
#  [PROT+GMI] - adds 'gemini.' to the gemini path
#  [EXT]      - adds file extension for given protocol
#  [DOC-NAME] - 'webpage' or 'capsule'
#  [SELF]     - link to the generated file
#####################

#############
# VARIABLES #
#############

SOURCE = "/home/de_alchmst/rss"
WEB_PATH = "/home/de_alchmst/public_html/web-rss.xml"
GEMINI_PATH = "/home/de_alchmst/public_html/gemini-rss.xml"
TODAY = `date "+%a, %d %b %Y %X %Z"`.strip

##########
# HEADER #
##########

header = %{<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Ðe-Alchmst's [DOC-NAME]</title>
  <link>[HOME]/</link>
  <description>My personal [DOC-NAME], hosted on the ctrl-c.club tilde server.</description>
  <language>en-us</language>
  <lastBuildDate>#{TODAY}</lastBuildDate>
  <atom:link href="[SELF]" rel="self" type="application/rss+xml" />

  <item>}

footer = %{
  </item>

</channel>
</rss>
}

##################
# PARSE THE FILE #
##################

File.open(SOURCE) { |file|
  file.readlines.each { |line|
    header += "\n  "
    line = line.strip
    if line.empty?
      header += "</item>\n  <item>"
    elsif line.start_with? "h "
      header += "  <title>#{line[2..]}</title>"
    elsif line.start_with? "l "
      header += "  <link>#{line[2..]}</link>\n    <guid>#{line[2..]}</guid>"
    elsif line.start_with? "de "
      header += "  <description>#{line[3..]}</description>"
    elsif line.start_with? "dt "
      header += "  <pubDate>#{line[3..]}</pubDate>"
    else
      abort "\x1b[31minvalid line: #{line}\x1b[0m"
    end
  }
}

header += footer

######################################
# CONVERT FOR BOTH VERSIONS AND SAVE #
######################################

# web #
wb = header.gsub("[HOME]", "https://ctrl-c.club/~de_alchmst") \
           .gsub("[PROT]", "https://") \
           .gsub("[PROT+GMI]", "https://") \
           .gsub("[EXT]", ".html") \
           .gsub("[DOC-NAME]", "webpage") \
           .gsub("[SELF]", "https://ctrl-c.club/~de_alchmst/web-rss.xml")
File.open(WEB_PATH, "w") { |file|
  file.print wb
}

# gemini #
gm = header.gsub("[HOME]", "gemini://gemini.ctrl-c.club/~de_alchmst") \
           .gsub("[PROT]", "gemini://") \
           .gsub("[PROT+GMI]", "gemini://gemini.") \
           .gsub("[EXT]", ".gmi") \
           .gsub("[DOC-NAME]", "capsule") \
           .gsub("[SELF]", "https://ctrl-c.club/~de_alchmst/gemini-rss.xml")
File.open(GEMINI_PATH, "w") { |file|
  file.print gm
}