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



58
59
60
61
62
63
# 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) ⇒ Object

Determines the gopher type for selector based on the information stored in the shared mime database.

Parameters:

  • filepath (String)

    The full path to the file (should also exist, if possible)

Returns:

  • gopher selector type



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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) ⇒ 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



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

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



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

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

#http(text, url, host = nil, port = nil) ⇒ Object

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).

Parameters:

  • text (String)

    the text of the link

  • URL (String)

    of the link

  • host (String) (defaults to: nil)

    for link, defaults to current host

  • port (String) (defaults to: nil)

    for link, defaults to current port



137
138
139
# File 'lib/gopher2000/rendering/menu.rb', line 137

def http(text, url, host=nil, port=nil)
  line "h", text, "URL:#{url}", host, port
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



36
37
38
39
40
41
42
43
# 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

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

  • real (String)

    filepath of the link

  • selector (String)

    type. if not specified, we will guess



98
99
100
101
102
103
104
105
106
107
# File 'lib/gopher2000/rendering/menu.rb', line 98

def link(text, selector, host=nil, port=nil, filepath=nil, type=nil)
  if !type
    if filepath
      type = determine_type(filepath)
    else
      type = determine_type(selector)
    end
  end
  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



21
22
23
24
25
26
# 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

#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



145
146
147
# File 'lib/gopher2000/rendering/menu.rb', line 145

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



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

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

output a link to text output

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

  • real (String)

    filepath of the link



120
121
122
# File 'lib/gopher2000/rendering/menu.rb', line 120

def text_link(text, selector, host=nil, port=nil, filepath=nil)
  link(text, selector, host, port, filepath, '0')
end