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) rubocop-performance (~> 1.18.0)
unicode-display_width (2.4.2) unicode-display_width (2.4.2)
webrick (1.8.1) webrick (1.8.1)
yaml (0.2.1)
PLATFORMS PLATFORMS
arm64-darwin-22 arm64-darwin-22
@ -77,6 +78,7 @@ DEPENDENCIES
rspec (~> 3.0) rspec (~> 3.0)
skeksis! skeksis!
standard (~> 1.3) standard (~> 1.3)
yaml
BUNDLED WITH BUNDLED WITH
2.4.15 2.4.15

View File

@ -3,26 +3,18 @@ require 'skeksis'
SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini" SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini"
class SkeksisApp class SkeksisApp
def initialize
@skeksis = Skeksis::GemBridge.new
end
def call(env) def call(env)
status = 200 status = 200
headers = { "content-type" => "text/html" } headers = { "content-type" => "text/html" }
#headers = {} body = @skeksis.query(SERVE_DIR + env['PATH_INFO'], env)
body = resolve_path(SERVE_DIR + env['PATH_INFO'])
[status, headers, body] [status, headers, body]
end 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 end
run SkeksisApp.new 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 # frozen_string_literal: true
require "yaml"
require_relative "skeksis/version" require_relative "skeksis/version"
require_relative "skeksis/parser" require_relative "skeksis/parser"
require_relative "skeksis/htmlize" require_relative "skeksis/htmlize"
module Skeksis module Skeksis
class Error < StandardError; end #class Error < StandardError; end
# Your code goes here...
extend self
def htmlize(data) class GemBridge
#puts Skeksis::Parser.parse(data).htmlize def initialize
Skeksis::Parser.parse(data).htmlize @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
end end

View File

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

View File

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