URI proxying support
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -13,16 +13,17 @@ module Skeksis
 | 
			
		||||
    </html>
 | 
			
		||||
    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]
 | 
			
		||||
          "<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
 | 
			
		||||
 
 | 
			
		||||
@@ -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 == :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
 | 
			
		||||
          #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 ""
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user