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(filepath) ⇒ Object
Determines the gopher type for
selector
based on the information stored in the shared mime database. -
#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.
-
#http(text, url, host = nil, port = nil) ⇒ Object
Create an HTTP link entry.
-
#line(type, text, selector, host = nil, port = nil) ⇒ Object
output a gopher menu line.
-
#link(text, selector, host = nil, port = nil, filepath = nil, type = 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.
-
#text_link(text, selector, host = nil, port = nil, filepath = nil) ⇒ Object
output a link to text output.
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
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.
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:
output a link to a sub-menu/directory
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
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).
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
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 |
#link(text, selector, host = nil, port = nil, filepath = nil, type = nil) ⇒ Object
output a menu link
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
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
145 146 147 |
# File 'lib/gopher2000/rendering/menu.rb', line 145 def search(text, selector, *args) line '7', text, selector, *args end |