Htmlizer implementation

This commit is contained in:
maddiebaka 2023-08-03 13:42:28 -04:00
parent 9c99732c24
commit 26cbb6d345
4 changed files with 85 additions and 7 deletions

View File

@ -5,8 +5,8 @@ SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini"
class SkeksisApp
def call(env)
status = 200
#headers = { "Content-Type" => "text/html" }
headers = {}
headers = { "content-type" => "text/html" }
#headers = {}
body = resolve_path(SERVE_DIR + env['PATH_INFO'])
[status, headers, body]
@ -18,8 +18,7 @@ class SkeksisApp
elsif File.exist?(path)
file = File.open(path, 'r')
data = file.readlines
Skeksis::Parser.parse(data)
data
[Skeksis.htmlize(data)]
else # path is invalid
return nil
end

View File

@ -2,8 +2,15 @@
require_relative "skeksis/version"
require_relative "skeksis/parser"
require_relative "skeksis/htmlize"
module Skeksis
class Error < StandardError; end
# Your code goes here...
extend self
def htmlize(data)
puts Skeksis::Parser.parse(data).htmlize
Skeksis::Parser.parse(data).htmlize
end
end

60
lib/skeksis/htmlize.rb Normal file
View File

@ -0,0 +1,60 @@
module Skeksis
class IR
Header = <<~HTML
<html>
<head>
<title>Gembridge</title>
</head>
<body>
HTML
Footer = <<~HTML
</body>
</html>
HTML
def htmlize
build_html_doc
end
private
def build_html_doc
content = self.parse_ir.join("\n")
Header + "#{content}\n" + Footer
end
def parse_ir
html = self.map do |entry|
content = lambda { |entry|
unless entry[:content].nil? then return entry[:content][0] end
}
text = content.call(entry)
case entry[:type]
when :blank
"<br/>"
when :header
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>"
when :quote
"<pre>#{text}</pre>"
when :uri
uri = entry[:uri]
text = entry[:text]
"<a href=\"#{uri}\">#{text}</a>"
when :verbatim
"<pre>" + entry[:content].join("\n") + "</pre>"
when :text
"<p>#{text}</p>"
end
end
html
end
end
end

View File

@ -1,6 +1,16 @@
# Code adapted from MIT-licensed gemini-rb project
# parser.rb hosted at
# https://github.com/genenetwork/gemini-rb/blob/main/lib/gemini-rb/parser.rb
#
# Internal Representation types
# :blank
# :header
# :list
# :quote
# :uri
# :verbatim
# :text
#
module Skeksis
class IR < Array
@ -49,7 +59,7 @@ module Skeksis
extend self
def parse(input)
puts("##### PARSING STARTED #####")
#puts("##### PARSING STARTED #####")
list = IR.new
@ -78,9 +88,11 @@ module Skeksis
end
list.strip_markers!
puts list
#puts list
#puts strip_markers(list)
puts("##### PARSING FINISHED #####")
#puts("##### PARSING FINISHED #####")
list
end
private