💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › gen-crossroads.rb.txt captured on 2024-08-18 at 17:26:47.

View Raw

More Information

⬅️ Previous capture (2024-05-26)

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

#!/usr/bin/ruby

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

GEMINI_DIR = "/home/de_alchmst/public_gemini/"
HTML_DIR = "/home/de_alchmst/public_html/"

TO_MANAGE = ["b-logs", "data"]

SELF_DIR = File.dirname __FILE__

#########################
# DELETE OLD CROSSROADS #
#########################

Dir.children(SELF_DIR).each { |child|
  if child.match /^crossroad/
    File.delete SELF_DIR + "/" + child
  end
}

################
# EXTRACT DATA #
################

def extract(path)
  outcome = { :all => [], :dirs => [] }

  # get children and sort dirs
  Dir.children(path).each { |child_end|
    if child_end == "index.html" or child_end == "index.gmi"
      next
    end

    child = path + "/" + child_end
    # don't add links, they are already covered
    if File.directory?(child) && !File.symlink?(child)
      outcome[:dirs].push child
    end

    outcome[:all].push child
  }

  outcome
end

#################
# CONVERT PATHS #
#################

def path_convert(path)
  path.sub /^\/home\/de_alchmst\/public_.+?\//, "~de_alchmst/"
end

##################
# GENERATE FILES #
##################

def gen(path, format)
  outcome = ""

  # get data
  data = extract(path)

  # generate contents #
  if format == :gemtext
    path_to_show = path.partition("gemini/")[2]

    outcome = path + "/index.gmi\n"
    outcome += ";;GEMTEXT_CROSSROAD_HEADER;;#{path_to_show};;\n"

    data[:all].sort.each { |dat|
      unless File.symlink? dat
        outcome += "=> /#{path_convert dat} #{dat.split(/[\/:]/).last}\n"
      else
        outcome += "=> /#{path_convert dat} ~> #{dat.split(/[\/:]/).last}\n"
      end
    }

    outcome += "\n||GEMTEXT_CROSSROAD_FOOTER||"
  else
    path_to_show = path.partition("html/")[2]

    outcome = path + "/index.html\n"
    outcome += ";;HTML_HEADER;;#{path_to_show};;\n"
    outcome += ";;HTML_CROSSROAD_HEADER;;#{path_to_show};;\n"

    data[:all].sort.each { |dat|
      unless File.symlink? dat
        outcome += "<li><a href=\"/#{path_convert dat}\">" + \
                   "#{dat.split(/[\/:]/).last}</a></li>\n"
      else
      outcome += "<li><a href=\"/#{path_convert dat}\">" + \
                 "~> #{dat.split(/[\/:]/).last}</a></li>\n"
      end
    }

    outcome += "\n||HTML_CROSSROAD_FOOTER||\n||HTML_FOOTER||"
  end

  # save file #
  f = File.open(SELF_DIR + "/crossroad" + path.gsub("/", ".") + ".index" + \
                (format == :getext ? ".gmi" : ".html") + ".template", "w")
  f.puts outcome
  f.close

  # recurse #
  data[:dirs].each { |dat|
    gen(dat, format)
  }
end

############
# CALL GEN #
############

TO_MANAGE.each { |managed|

  gen GEMINI_DIR + managed, :gemtext
  gen HTML_DIR + managed, :html
}