Verbatim lines collapsing on parse
This commit is contained in:
		@@ -3,38 +3,99 @@
 | 
			
		||||
# https://github.com/genenetwork/gemini-rb/blob/main/lib/gemini-rb/parser.rb
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
    extend self
 | 
			
		||||
 | 
			
		||||
    def parse(input)
 | 
			
		||||
      puts("##### PARSING STARTED #####")
 | 
			
		||||
 | 
			
		||||
      list = []
 | 
			
		||||
      list = IR.new
 | 
			
		||||
 | 
			
		||||
      data = input.map(&:chomp)
 | 
			
		||||
 | 
			
		||||
      in_verbatim_block = false
 | 
			
		||||
      content = []
 | 
			
		||||
      data.each do |line|
 | 
			
		||||
        type = get_type(line)
 | 
			
		||||
        if type == :verbatim and in_verbatim_block == false
 | 
			
		||||
          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
 | 
			
		||||
          content.push(line)
 | 
			
		||||
          list.push({ type: :verbatim, content: content })
 | 
			
		||||
          content = []
 | 
			
		||||
          next
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        if in_verbatim_block == true
 | 
			
		||||
          list.push({ type: :verbatim, content: [line] })
 | 
			
		||||
        else
 | 
			
		||||
        if in_verbatim_block == false
 | 
			
		||||
          list.push({ type: type, content: [line] })
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      puts strip_markers(list)
 | 
			
		||||
      list.strip_markers!
 | 
			
		||||
      puts list
 | 
			
		||||
      #puts strip_markers(list)
 | 
			
		||||
      puts("##### PARSING FINISHED #####")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def collapse_verbatim_blocks(gemtext)
 | 
			
		||||
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def strip_markers(gemtext)
 | 
			
		||||
      gemtext.map do |line|
 | 
			
		||||
        type = line[:type]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user