Basic parser implementation

This commit is contained in:
maddiebaka
2023-08-01 22:53:54 -04:00
parent 7e0640dc1c
commit b150a808ab
6 changed files with 178 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative "skeksis/version"
require_relative "skeksis/parser"
module Skeksis
class Error < StandardError; end

99
lib/skeksis/parser.rb Normal file
View File

@@ -0,0 +1,99 @@
# 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
module Skeksis
module Parser
extend self
def parse(input)
puts("##### PARSING STARTED #####")
list = []
data = input.map(&:chomp)
in_verbatim_block = false
data.each do |line|
type = get_type(line)
if type == :verbatim and in_verbatim_block == false
in_verbatim_block = true
elsif type == :verbatim
in_verbatim_block = false
end
if in_verbatim_block == true
list.push({ type: :verbatim, content: [line] })
else
list.push({ type: type, content: [line] })
end
end
puts strip_markers(list)
puts("##### PARSING FINISHED #####")
end
private
def strip_markers(gemtext)
gemtext.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
def get_type(l)
case l
when ""
:blank
when /^#/
:header
when /^\*/
:list
when /^\>/
:quote
when /^\=>/
:uri
when /^```/
:verbatim
else
:text
end
end
def parse_line(line)
end
end
end