💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › gen-templates.rb.txt captured on 2024-05-26 at 16:34:36.

View Raw

More Information

⬅️ Previous capture (2024-05-10)

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

#!/usr/bin/ruby

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

TEMPLATE_DIR = "#{ENV["HOME"]}/templates/"
SELF_DIR = "#{ENV["HOME"]}/b-logs/"
HTML_DIR = "#{ENV["HOME"]}/public_html/"
GEMINI_DIR = "#{ENV["HOME"]}/public_gemini/"

dir_structure = {}

#######################
# READ RELEVANT FILES #
#######################

Dir.children(File.dirname(__FILE__)).each { |child|
  if child.match(/^b\-log/)
    # get relevant parts of name
    file_parts = child.split(".")[1..]

    # add to dir_structure
    dir = file_parts[..file_parts.length-2].join "."

    if dir_structure.keys.include? dir
      dir_structure[dir].push file_parts.last
    else
      dir_structure[dir] = [file_parts.last]
    end

  end
}

#####################################
# REMOVE PREVIOUSLY GENERATED FILES #
#####################################

Dir.children(TEMPLATE_DIR).each{ |child|
  if child.match(/^b-log/)
    File.delete TEMPLATE_DIR + child
  end
}

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

dir_structure.keys.each { |key|
  # get all files in directory
  files = dir_structure[key].sort

  for i in 0..files.length-1 do
    name = key.gsub(".", "/") + "/" + files[i]
    path = "#{SELF_DIR}b-log.#{key}.#{files[i]}"

    prev_name = i == 0 ? nil : files[i-1]
    next_name = i == files.length-1 ? nil : files[i+1]

    # get file contents for both protocols #
    html = HTML_DIR + "b-logs/" + name + ".html\n" + \
      ";;HTML_HEADER;;#{name.split(":").last};;\n" + \
      "||HTML_B-LOG_HEADER||\n" + \
      "<div id=\"contents\">;;TO_HTML;;#{path};;</div>\n" + \
      "<div id=\"footer\">" + \
      (prev_name ? " <a href=\"./#{prev_name}.html\">previous:" + \
       " #{prev_name.split(":")[1]}</a> "
                 : "") + \
      (next_name ? "<a href=\"./#{next_name}.html\">next:" + \
       " #{next_name.split(":")[1]}</a> "
                 : "") + "<br>\n" + \
      "||HTML_B-LOG_FOOTER||\n</div>\n" + \
      "||HTML_FOOTER||"

    gemtext = GEMINI_DIR + "b-logs/" + name + ".gmi\n" + \
      "||GEMTEXT_B-LOG_HEADER||\n" + \
      ";;TO_GEMTEXT;;#{path};;\n" + \
      "----------------------------\n" + \
      (prev_name ? "=> ./#{prev_name}.gmi" +\
       " previous: #{prev_name.split(":")[1]}\n" : "") + \
      (next_name ? "=> ./#{next_name}.gmi" + \
       " next: #{next_name.split(":")[1]}\n" : "") + \
      "||GEMTEXT_B-LOG_FOOTER||"

    # write them to coresponding files
    f = File.open(TEMPLATE_DIR + "b-log." \
            + key + "." + files[i] + ".html.template", "w")
    f.puts html
    f.close
    f = File.open(TEMPLATE_DIR + "b-log." \
            + key + "." + files[i] + ".gmi.template", "w")
    f.puts gemtext
    f.close
  end
}