Verbatim lines collapsing on parse

This commit is contained in:
maddiebaka 2023-08-02 10:37:04 -04:00
parent b150a808ab
commit 72e841cfcc

View File

@ -3,38 +3,99 @@
# https://github.com/genenetwork/gemini-rb/blob/main/lib/gemini-rb/parser.rb # https://github.com/genenetwork/gemini-rb/blob/main/lib/gemini-rb/parser.rb
module Skeksis module Skeksis
class IR < Array
def collapse_verbatim_blocks!
last_block = nil
self.each do |line|
type = get_type(line)
# Last thing
last_block = type
end
end
def strip_markers!
self.map! do |line|
type = line[:type]
content = line[:content]
text = content[0]
case type
when :blank
#{ type: type, content: nil }
{ type: type }
when :header
m = /^(#+)(\s*)(.*)/.match(text)
level = m[1].count("#")
{ type: type, level: level, content: [m[3]] }
when :list
{ type: type, content: content.map { |t| t.sub(/^\*\s*/, "") } }
when :quote
{ type: type, content: content.map { |t| t.sub(/^\>\s?/, "") } }
when :uri
a = text.sub(/^=>\s*/, "").split(" ", 2)
link = a[0]
text = a[1]
{ type: type, link: link, text: text }
when :verbatim
# TODO: Match with syntax highlighting, maybe
m = /^```(.*)/.match(text)
unless m.nil?
nil
else
{ type: type, content: content }
end
else
{ type: type, content: content }
end
end.compact!
end
end
module Parser module Parser
extend self extend self
def parse(input) def parse(input)
puts("##### PARSING STARTED #####") puts("##### PARSING STARTED #####")
list = [] list = IR.new
data = input.map(&:chomp) data = input.map(&:chomp)
in_verbatim_block = false in_verbatim_block = false
content = []
data.each do |line| data.each do |line|
type = get_type(line) type = get_type(line)
if type == :verbatim and in_verbatim_block == false if type == :verbatim and in_verbatim_block == false
in_verbatim_block = true in_verbatim_block = true
elsif type == :verbatim 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 in_verbatim_block = false
content.push(line)
list.push({ type: :verbatim, content: content })
content = []
next
end end
if in_verbatim_block == true if in_verbatim_block == false
list.push({ type: :verbatim, content: [line] })
else
list.push({ type: type, content: [line] }) list.push({ type: type, content: [line] })
end end
end end
puts strip_markers(list) list.strip_markers!
puts list
#puts strip_markers(list)
puts("##### PARSING FINISHED #####") puts("##### PARSING FINISHED #####")
end end
private private
def collapse_verbatim_blocks(gemtext)
end
def strip_markers(gemtext) def strip_markers(gemtext)
gemtext.map do |line| gemtext.map do |line|
type = line[:type] type = line[:type]