Class: Gopher::Rendering::Menu

Inherits:
Base show all
Defined in:
lib/gopher2000/rendering/menu.rb

Overview

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

Constant Summary collapse

NO_HOST =

default host value when rendering a line with no selector

'(FALSE)'
NO_PORT =

default port value when rendering a line with no selector

0

Instance Attribute Summary

Attributes inherited from Base

#application, #params, #request, #result, #spacing, #width

Instance Method Summary collapse

Methods inherited from Base

#<<, #big_header, #block, #figlet, #header, #initialize, #small_header, #to_s, #underline

Constructor Details

This class inherits a constructor from Gopher::Rendering::Base

Instance Method Details

#br(n = 1) ⇒ Object

add some empty lines to the menu

Parameters:

  • n (integer) (defaults to: 1)

    how many breaks to add



57
58
59
60
61
62
# File 'lib/gopher2000/rendering/menu.rb', line 57

def br(n=1)
  1.upto(n) do
    text 'i', ""
  end
  self.to_s
end

#determine_type(selector) ⇒ Object

Determines the gopher type for selector based on the extension. This is a pretty simple check based on the entities list in www.ietf.org/rfc/rfc1436.txt

Parameters:

  • selector (String)

    presumably a link to a file name with an extension

Returns:

  • gopher selector type



117
118
119
120
121
122
123
124
125
126
# File 'lib/gopher2000/rendering/menu.rb', line 117

def determine_type(selector)
  ext = File.extname(selector).downcase
  case ext
  when '.zip', '.gz', '.bz2' then '5'
  when '.gif' then 'g'
  when '.jpg', '.png' then 'I'
  when '.mp3', '.wav' then 's'
  else '0'
  end
end

#directory(name, selector, host = nil, port = nil) ⇒ Object Also known as: menu

output a link to a sub-menu/directory

Parameters:

  • name (String)

    of the menu/directory

  • selector (String)

    we are linking to

  • host (String) (defaults to: nil)

    for link, defaults to current host

  • port (String) (defaults to: nil)

    for link, defaults to current port



79
80
81
# File 'lib/gopher2000/rendering/menu.rb', line 79

def directory(name, selector, host=nil, port=nil)
  line '1', name, selector, host, port
end

#error(msg) ⇒ Object

output an error message

Parameters:

  • msg (String)

    text of the message



68
69
70
# File 'lib/gopher2000/rendering/menu.rb', line 68

def error(msg)
  text(msg, '3')
end

#line(type, text, selector, host = nil, port = nil) ⇒ Object

output a gopher menu line

Parameters:

  • type (String)

    what sort of entry is this? @see www.ietf.org/rfc/rfc1436.txt for a list

  • text (String)

    the text of the line

  • selector (String)

    if this is a link, the path of the route we are linking to

  • host (String) (defaults to: nil)

    for link, defaults to current host

  • port (String) (defaults to: nil)

    for link, defaults to current port



35
36
37
38
39
40
41
42
# File 'lib/gopher2000/rendering/menu.rb', line 35

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

output a menu link

Parameters:

  • text (String)

    the text of the link

  • selector (String)

    the path of the link. the extension of this path will be used to detemine the type of link – image, archive, etc. If you want to specify a specific link-type, you should use the text method instead

  • host (String) (defaults to: nil)

    for link, defaults to current host

  • port (String) (defaults to: nil)

    for link, defaults to current port



95
96
97
98
# File 'lib/gopher2000/rendering/menu.rb', line 95

def link(text, selector, host=nil, port=nil)
  type = determine_type(selector)
  line type, text, selector, host, port
end

#sanitize_text(raw) ⇒ Object

Sanitizes text for use in gopher menus

Parameters:

  • raw (String)

    text to cleanup

Returns:

  • string that can be used in a gopher menu



20
21
22
23
24
25
# File 'lib/gopher2000/rendering/menu.rb', line 20

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

#search(text, selector, *args) ⇒ Object Also known as: input

output a search entry

Parameters:

  • text (String)

    the text of the link

  • selector (String)

    the path of the selector



104
105
106
# File 'lib/gopher2000/rendering/menu.rb', line 104

def search(text, selector, *args)
  line '7', text, selector, *args
end

#text(text, type = 'i') ⇒ Object

output a line of text, with no selector

Parameters:

  • text (String)

    the text of the line

  • type (String) (defaults to: 'i')

    what sort of entry is this? @see www.ietf.org/rfc/rfc1436.txt for a list



49
50
51
# File 'lib/gopher2000/rendering/menu.rb', line 49

def text(text, type = 'i')
  line type, text, 'null', NO_HOST, NO_PORT
end