71 lines
1.7 KiB
Ruby
71 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "yaml"
|
|
|
|
require_relative "skeksis/version"
|
|
require_relative "skeksis/parser"
|
|
require_relative "skeksis/htmlize"
|
|
|
|
module Skeksis
|
|
#class Error < StandardError; end
|
|
Header = <<~HTML
|
|
<html>
|
|
<head>
|
|
<title>Gembridge</title>
|
|
</head>
|
|
<body>
|
|
<h1>Directory Listing</h1>
|
|
HTML
|
|
|
|
Footer = <<~HTML
|
|
</body>
|
|
</html>
|
|
HTML
|
|
|
|
class GemBridge
|
|
def initialize
|
|
config = YAML.load(File.read("config.yml"))
|
|
@gemini_uri = config['skeksis_config']['gemini_uri']
|
|
@serve_dir = config['skeksis_config']['serve_dir']
|
|
end
|
|
|
|
def query(path, env)
|
|
# Chomps the first / and builds path
|
|
full_path = Pathname.new(@serve_dir) + path[1..-1]
|
|
puts full_path.to_s
|
|
if Dir.exist?(full_path)
|
|
index_file = full_path + "index.gmi"
|
|
if File.exist?(index_file)
|
|
data = File.open(index_file, 'r').readlines
|
|
[htmlize(data, env)]
|
|
else
|
|
create_dir_listing(full_path, env)
|
|
end
|
|
elsif File.exist?(full_path)
|
|
file = File.open(full_path, 'r')
|
|
data = file.readlines
|
|
[htmlize(data, env)]
|
|
else # path is invalid
|
|
return nil
|
|
end
|
|
end
|
|
|
|
def htmlize(data, env)
|
|
Skeksis::Parser.parse(data, strip_blanks=true).htmlize(env['REQUEST_URI'], @gemini_uri)
|
|
end
|
|
|
|
private
|
|
|
|
def create_dir_listing(path, env)
|
|
http = URI.parse(env['REQUEST_URI'])
|
|
|
|
listing = Dir.each_child(path).map do |i|
|
|
path = Pathname.new(env['PATH_INFO']).join(i)
|
|
uri = URI::HTTP.build(host: http.host, port: http.port, path: path.to_s)
|
|
"<a href=\"#{uri.to_s}\">#{i}</a><br/>"
|
|
end.join("\n")
|
|
[Header + listing + Footer]
|
|
end
|
|
end
|
|
end
|