class Gopher::Rendering::Menu

The MenuContext is for rendering gopher menus in the “pseudo file-system hierarchy” defined by RFC1436

@see www.ietf.org/rfc/rfc1436.txt

Constants

NO_HOST

default host value when rendering a line with no selector

NO_PORT

default port value when rendering a line with no selector

Public Instance Methods

br(n=1) click to toggle source

add some empty lines to the menu @param [integer] n how many breaks to add

# File lib/gopher2000/rendering/menu.rb, line 58
def br(n=1)
  1.upto(n) do
    text 'i', ""
  end
  self.to_s
end
determine_type(filepath) click to toggle source

Determines the gopher type for selector based on the information stored in the shared mime database. @param [String] filepath The full path to the file (should also exist, if possible) @return gopher selector type

# File lib/gopher2000/rendering/menu.rb, line 157
def determine_type(filepath)
  # Determine MIME type by path
  mimetype = MimeMagic.by_path(filepath)

  # Determine MIME type by contents
  if !mimetype
    begin
      # Open file
      file = File.open(filepath)

      # Try to detect MIME type using by recognition of typical characters
      mimetype = MimeMagic.by_magic(file)

      if !mimetype
        file.rewind

        # Read up to 1k of file data and look for a "\0\0" sequence (typical for binary files)
        if file.read(1000).include?("\0\0")
          mimetype = MimeMagic.new("application/octet-stream")
        else
          mimetype = MimeMagic.new("text/plain")
        end
        
        file.close
      end
    rescue SystemCallError,IOError
      nil
    end
  end

  if !mimetype
    ext = File.extname(filepath).split(".").last
    mimetype = MimeMagic.by_extension(ext)
  end
  
  if !mimetype
    return '9' # Binary file
  elsif mimetype.child_of?('application/gzip') || mimetype.child_of?('application/x-bzip') || mimetype.child_of?('application/zip')
    return '5' # archive
  elsif mimetype.child_of?('image/gif')
    return 'g' # GIF image
  elsif mimetype.child_of?('text/x-uuencode')
    return '6' # UUEncode encoded file
  elsif mimetype.child_of?('application/mac-binhex40')
    return '4' # BinHex encoded file
  elsif mimetype.child_of?('text/html') || mimetype.child_of?('application/xhtml+xml')
    return 'h' # HTML file
  elsif mimetype.mediatype == 'text'    || mimetype.child_of?('text/plain')
    return '0' # General text file
  elsif mimetype.mediatype == 'image'
    return 'I' # General image file
  elsif mimetype.mediatype == 'audio'
    return 's' # General audio file
  elsif mimetype.mediatype == 'video'
    return 'v' # General video file
  else
    return '9' # Binary file
  end
end
directory(name, selector, host=nil, port=nil) click to toggle source

output a link to a sub-menu/directory @param [String] name of the menu/directory @param [String] selector we are linking to @param [String] host for link, defaults to current host @param [String] port for link, defaults to current port

# File lib/gopher2000/rendering/menu.rb, line 80
def directory(name, selector, host=nil, port=nil)
  line '1', name, selector, host, port
end
Also aliased as: menu
error(msg) click to toggle source

output an error message @param [String] msg text of the message

# File lib/gopher2000/rendering/menu.rb, line 69
def error(msg)
  text(msg, '3')
end
http(text, url, host=nil, port=nil) click to toggle source

Create an HTTP link entry. This is how this works (via wikipedia)

For example, to create a link to gopher.quux.org/, the item type is “h”, the display string is the title of the link, the item selector is “URL:gopher.quux.org/”, and the domain and port are that of the originating Gopher server (so that clients that do not support URL links will query the server and receive an HTML redirection page).

@param [String] text the text of the link @param [String] URL of the link @param [String] host for link, defaults to current host @param [String] port for link, defaults to current port

# File lib/gopher2000/rendering/menu.rb, line 137
def http(text, url, host=nil, port=nil)
  line "h", text, "URL:#{url}", host, port
end
input(text, selector, *args)
Alias for: search
line(type, text, selector, host=nil, port=nil) click to toggle source

output a gopher menu line

@param [String] type what sort of entry is this? @see www.ietf.org/rfc/rfc1436.txt for a list @param [String] text the text of the line @param [String] selector if this is a link, the path of the route we are linking to @param [String] host for link, defaults to current host @param [String] port for link, defaults to current port

# File lib/gopher2000/rendering/menu.rb, line 36
def line(type, text, selector, host=nil, port=nil)
  text = sanitize_text(text)

  host = application.host if host.nil?
  port = application.port if port.nil?

  self << ["#{type}#{text}", selector, host, port].join("\t") + LINE_ENDING
end
menu(name, selector, host=nil, port=nil)
Alias for: directory
sanitize_text(raw) click to toggle source

Sanitizes text for use in gopher menus @param [String] raw text to cleanup @return string that can be used in a gopher menu

# File lib/gopher2000/rendering/menu.rb, line 21
def sanitize_text(raw)
  raw.
    rstrip. # Remove excess whitespace
    gsub(/\t/, ' ' * 8). # Tabs to spaces
    gsub(/\n/, '') # Get rid of newlines (\r as well?)
end
text(text, type = 'i') click to toggle source

output a line of text, with no selector @param [String] text the text of the line @param [String] type what sort of entry is this? @see www.ietf.org/rfc/rfc1436.txt for a list

# File lib/gopher2000/rendering/menu.rb, line 50
def text(text, type = 'i')
  line type, text, 'null', NO_HOST, NO_PORT
end