Class: Gopher::Rendering::Menu
- Inherits:
-
Base
- Object
- AbstractRenderer
- Base
- Gopher::Rendering::Menu
- 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
-
#br(n = 1) ⇒ Object
add some empty lines to the menu.
-
#determine_type(selector) ⇒ Object
Determines the gopher type for
selector
based on the extension. -
#directory(name, selector, host = nil, port = nil) ⇒ Object
(also: #menu)
output a link to a sub-menu/directory.
-
#error(msg) ⇒ Object
output an error message.
-
#line(type, text, selector, host = nil, port = nil) ⇒ Object
output a gopher menu line.
-
#link(text, selector, host = nil, port = nil) ⇒ Object
output a menu link.
-
#sanitize_text(raw) ⇒ Object
Sanitizes text for use in gopher menus.
-
#search(text, selector, *args) ⇒ Object
(also: #input)
output a search entry.
-
#text(text, type = 'i') ⇒ Object
output a line of text, with no selector.
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
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
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:
output a link to a sub-menu/directory
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
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
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 |
#link(text, selector, host = nil, port = nil) ⇒ Object
output a menu link
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
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 |