Files
skeksis/lib/skeksis/htmlize.rb
2023-08-04 21:49:40 -04:00

74 lines
1.6 KiB
Ruby

module Skeksis
class IR
Header = <<~HTML
<html>
<head>
<title>Gembridge</title>
</head>
<body>
HTML
Footer = <<~HTML
</body>
</html>
HTML
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
content = self.parse_ir.join("\n")
Header + "#{content}\n" + Footer
end
private
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
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]
http_link = build_uri(uri)
"<a href=\"#{http_link}\">#{text}</a>"
when :verbatim
"<pre>" + entry[:content].join("\n") + "</pre>"
when :text
"<p>#{text}</p>"
end
end
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