module Gemini::GmiParser
Contains specific method to parse text/gemini documents.
Private Instance Methods
parse_body()
click to toggle source
# File lib/net/gemini/gmi_parser.rb, line 46 def parse_body buf = StringIO.new(@body) while (line = buf.gets) if line.start_with?('```') parse_preformatted_block(line, buf) elsif line.start_with?('=>') parse_link(line) end end end
parse_link(line)
click to toggle source
# File lib/net/gemini/gmi_parser.rb, line 34 def parse_link(line) m = line.strip.match(/\A=>\s*([^\s]+)(?:\s*(.+))?\z/) return if m.nil? begin uri = URI(m[1]) rescue URI::InvalidURIError return end uri = @uri.merge(uri) if @uri && uri.is_a?(URI::Generic) @links << { uri: uri, label: m[2]&.chomp } end
parse_meta()
click to toggle source
# File lib/net/gemini/gmi_parser.rb, line 8 def parse_meta header = { status: @status, meta: @meta, mimetype: nil } return header unless body_permitted? mime = { lang: nil, charset: 'utf-8', format: nil } raw_meta = meta.split(';').map(&:strip) header[:mimetype] = raw_meta.shift return header unless raw_meta.any? raw_meta.map { |m| m.split('=') }.each do |opt| key = opt[0].downcase.to_sym next unless mime.has_key? key mime[key] = opt[1].downcase end header.merge(mime) end
parse_preformatted_block(line, buf)
click to toggle source
# File lib/net/gemini/gmi_parser.rb, line 23 def parse_preformatted_block(line, buf) cur_block = { meta: line[3..].chomp, content: '' } while (line = buf.gets) if line.start_with?('```') @preformatted_blocks << cur_block break end cur_block[:content] += line end end