class Gopher::Rendering::Base

base class for rendering output. this class provides methods that can be used when rendering both text and gopher menus

Attributes

application[RW]
params[RW]
request[RW]
result[RW]
spacing[RW]
width[RW]

Public Class Methods

new(app=nil) click to toggle source
# File lib/gopher2000/rendering/base.rb, line 23
def initialize(app=nil)
  @application = app
  @result = ""
  @spacing = 1

  # default to 70 per RFC1436 3.9
  # "the user display string should be kept under 70 characters in length"
  @width = 70
end

Public Instance Methods

<<(string) click to toggle source

add a line to the output @param [String] string text to add to the output

# File lib/gopher2000/rendering/base.rb, line 37
def <<(string)
  @result << clean_line(string.to_s)
end
big_header(str, under = '=') click to toggle source

output a centered string in a box @param [String] str the string to output @param [Strnig] under the character to use to make the box

# File lib/gopher2000/rendering/base.rb, line 170
def big_header(str, under = '=')
  br
  underline(@width, under)
  header(str, under, true)

  # enforcing some extra space around headers for now
  br
end
block(text, width=@width) click to toggle source

wrap text into lines no wider than width. Hacked from ActionView @see github.com/rails/rails/blob/196407c54f0736c275d2ad4e6f8b0ac55360ad95/actionpack/lib/action_view/helpers/text_helper.rb#L217

@param [String] text the text you want to wrap @param [Integer] width the desired width of the block – defaults to the

current output width
# File lib/gopher2000/rendering/base.rb, line 87
def block(text, width=@width)

  # this is a hack - recombine lines, then re-split on newlines
  # doing this because word_wrap is returning an array of lines, but
  # those lines have newlines in them where we should wrap
  #
  lines = word_wrap(text, width)
            .join("\n").split("\n")

  lines.each do |line|
    text line.lstrip.rstrip
  end

  self.to_s
end
br(n=1) click to toggle source

Add some empty lines to the output @param [Integer] n how many lines to add

# File lib/gopher2000/rendering/base.rb, line 75
def br(n=1)
  self << (LINE_ENDING * n)
end
figlet(str, font = 'big') click to toggle source

@param [String] str the text you want to use for your figlet @param [String] font name of the font. Defaults to ‘big’.

# File lib/gopher2000/rendering/base.rb, line 121
def figlet(str, font = 'big')
  a = Artii::Base.new(:font => font)
  a.asciify(str).split("\n").each do |l|
    text l
  end
  self.to_s
end
header(str, under = '=', edge = false) click to toggle source

output a centered string with a nice underline below it, centered on the current output width

@param [String] str - the string to output @param [String] under - the desired underline character @param [Boolean] edge - should we output an edge? if so, there will be a

character to the left/right edges of the string, so you can
draw a box around the text
# File lib/gopher2000/rendering/base.rb, line 139
def header(str, under = '=', edge = false)
  w = @width
  if edge
    w -= 2
  end

  tmp = str.center(w)
  if edge
    tmp = "#{under}#{tmp}#{under}"
  end

  text(tmp)
  underline(@width, under)
end
small_header(str, under = '=') click to toggle source

output a ‘small’ header, just the text with an underline @param [String] str - the string to output @param [String] under - the desired underline character

# File lib/gopher2000/rendering/base.rb, line 159
def small_header(str, under = '=')
  str = " " + str + " "
  text(str)
  underline(str.length, under)
end
text(text) click to toggle source

Adds text to the result @param text text to add to the result. Adds the line,

then adds any required spacing
# File lib/gopher2000/rendering/base.rb, line 46
def text(text)
  self << text.force_encoding(DEFAULT_ENCODING)
  add_spacing
end
to_s() click to toggle source

return the output as a string @return rendered output

# File lib/gopher2000/rendering/base.rb, line 195
def to_s
  @result
end
underline(length=@width, char='=') click to toggle source

output an underline

@param [Integer] length the length of the underline – defaults to current

output width.

@param [String] char the character to output

# File lib/gopher2000/rendering/base.rb, line 186
def underline(length=@width, char='=')
  text(char * length)
end

Protected Instance Methods

clean_line(line) click to toggle source

handle lines of a single period not at the end of the transmission

RFC 1436 states: Note: Lines beginning with periods must be prepended with an extra period to ensure that the transmission is not terminated early. The client should strip extra periods at the beginning of the line.

# File lib/gopher2000/rendering/base.rb, line 220
def clean_line(line)
  line.match?(/^\./) ? ['.', line].join('') : line
end
word_wrap(text, width=70*args) click to toggle source

borrowed and modified from ActionView – wrap text at specified width returning an array of lines for now in case we want to do nifty processing with them

File actionpack/lib/action_view/helpers/text_helper.rb, line 217

# File lib/gopher2000/rendering/base.rb, line 205
def word_wrap(text, width=70*args)
  text.split("\n").collect do |line|
    line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line
  end
end

Private Instance Methods

add_spacing() click to toggle source
# File lib/gopher2000/rendering/base.rb, line 225
def add_spacing
  br(@spacing)
end