From a68a88bd1afac59fb79d826cc9807588cac4145e Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Fri, 4 Aug 2023 21:49:40 -0400 Subject: [PATCH] URI proxying support --- Gemfile.lock | 2 ++ config.ru | 18 +++++------------- config.yml | 2 ++ lib/skeksis.rb | 29 +++++++++++++++++++++++------ lib/skeksis/htmlize.rb | 31 ++++++++++++++++++++++--------- lib/skeksis/parser.rb | 33 +++++---------------------------- 6 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 config.yml diff --git a/Gemfile.lock b/Gemfile.lock index 4aad841..c2071e9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,7 @@ GEM rubocop-performance (~> 1.18.0) unicode-display_width (2.4.2) webrick (1.8.1) + yaml (0.2.1) PLATFORMS arm64-darwin-22 @@ -77,6 +78,7 @@ DEPENDENCIES rspec (~> 3.0) skeksis! standard (~> 1.3) + yaml BUNDLED WITH 2.4.15 diff --git a/config.ru b/config.ru index 4f427e6..63d9964 100644 --- a/config.ru +++ b/config.ru @@ -3,26 +3,18 @@ require 'skeksis' SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini" class SkeksisApp + def initialize + @skeksis = Skeksis::GemBridge.new + end + def call(env) status = 200 headers = { "content-type" => "text/html" } - #headers = {} - body = resolve_path(SERVE_DIR + env['PATH_INFO']) + body = @skeksis.query(SERVE_DIR + env['PATH_INFO'], env) [status, headers, body] end - def resolve_path(path) - if Dir.exist?(path) - return Dir.each_child(path).map {|i| "#{i}\n"} - elsif File.exist?(path) - file = File.open(path, 'r') - data = file.readlines - [Skeksis.htmlize(data)] - else # path is invalid - return nil - end - end end run SkeksisApp.new diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..8ad659f --- /dev/null +++ b/config.yml @@ -0,0 +1,2 @@ +skeksis_config: + gemini_uri: "gemini://gemini.hackers.town/" diff --git a/lib/skeksis.rb b/lib/skeksis.rb index f387706..c92a15b 100644 --- a/lib/skeksis.rb +++ b/lib/skeksis.rb @@ -1,16 +1,33 @@ # frozen_string_literal: true +require "yaml" + require_relative "skeksis/version" require_relative "skeksis/parser" require_relative "skeksis/htmlize" module Skeksis - class Error < StandardError; end - # Your code goes here... - extend self + #class Error < StandardError; end - def htmlize(data) - #puts Skeksis::Parser.parse(data).htmlize - Skeksis::Parser.parse(data).htmlize + class GemBridge + def initialize + @gemini_uri = YAML.load(File.read("config.yml"))['skeksis_config']['gemini_uri'] + end + + def query(path, env) + if Dir.exist?(path) + return Dir.each_child(path).map {|i| "#{i}\n"} + elsif File.exist?(path) + file = File.open(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 end end diff --git a/lib/skeksis/htmlize.rb b/lib/skeksis/htmlize.rb index 9fc981f..4a035eb 100644 --- a/lib/skeksis/htmlize.rb +++ b/lib/skeksis/htmlize.rb @@ -13,16 +13,17 @@ module Skeksis HTML - def htmlize - build_html_doc - end + def htmlize(request_uri, proxied_uri=nil, port=80) + @http = URI.parse(request_uri) + unless proxied_uri.nil? + @proxied_uri = URI.parse(proxied_uri) + end - private - def build_html_doc content = self.parse_ir.join("\n") Header + "#{content}\n" + Footer end + private def parse_ir html = self.map do |entry| content = lambda { |entry| @@ -38,15 +39,17 @@ module Skeksis level = entry[:level] "#{text}" when :list - # TODO: Does not handle lists properly, only one