URI proxying support

This commit is contained in:
maddiebaka 2023-08-04 21:49:40 -04:00
parent 4d0f9802fc
commit a68a88bd1a
6 changed files with 59 additions and 56 deletions

View File

@ -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

View File

@ -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

2
config.yml Normal file
View File

@ -0,0 +1,2 @@
skeksis_config:
gemini_uri: "gemini://gemini.hackers.town/"

View File

@ -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

View File

@ -13,16 +13,17 @@ module Skeksis
</html>
HTML
def htmlize
build_html_doc
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]
"<h#{level}>#{text}</h#{level}>"
when :list
# TODO: Does not handle lists properly, only one <ul> tag per group of lists needs to be
# created instead
"<ul><li>#{text}</li></ul>"
list = entry[:content].map do |textline|
"<li>" + textline + "</li>"
end.join("\n")
"<ul>" + list + "</ul>"
when :quote
"<pre>#{text}</pre>"
when :uri
uri = entry[:uri]
text = entry[:text]
"<a href=\"#{uri}\">#{text}</a>"
http_link = build_uri(uri)
"<a href=\"#{http_link}\">#{text}</a>"
when :verbatim
"<pre>" + entry[:content].join("\n") + "</pre>"
when :text
@ -56,5 +59,15 @@ module Skeksis
html
end
def build_uri(link)
uri = URI.parse(link)
if uri.host == @proxied_uri.host
URI::HTTP.build(host: @http.host, path: uri.path, port: @http.port)
else
link
end
end
end
end

View File

@ -36,7 +36,7 @@ module Skeksis
a = text.sub(/^=>\s*/, "").split(" ", 2)
link = a[0]
text = a[1]
{ type: type, link: link, text: text }
{ type: type, uri: link, text: text }
when :verbatim
# TODO: Match with syntax highlighting, maybe
content.map! do |line|
@ -52,15 +52,15 @@ module Skeksis
{ type: type, content: content }
end
end.compact!
self
end
end
module Parser
extend self
def parse(input)
puts("##### PARSING STARTED #####")
def parse(input, strip_blanks=false)
list = IR.new
data = input.map(&:chomp)
@ -93,7 +93,6 @@ module Skeksis
end
else
if in_multiline_block == true
#puts("in multiline block")
list.push({ type: in_block, content: content })
in_multiline_block = false
content = []
@ -104,40 +103,18 @@ module Skeksis
end
end
#if type == :verbatim and in_verbatim_block == false
# in_verbatim_block = true
# content.push(line)
#elsif type != :verbatim and in_verbatim_block == true
# content.push(line)
#elsif type == :verbatim and in_verbatim_block == true
# in_verbatim_block = false
# content.push(line)
# list.push({ type: :verbatim, content: content })
# content = []
# next
#end
if type == :blank and strip_blanks == true then next end
if in_verbatim_block == false and in_multiline_block == false
#puts("catch-all push")
list.push({ type: type, content: [line] })
end
end
list.strip_markers!
puts list
#puts strip_markers(list)
puts("##### PARSING FINISHED #####")
list
end
private
def collapse_verbatim_blocks(gemtext)
end
def get_type(l)
case l
when ""