class AsciiParadise::ParseStaticAscii
Attributes
message_callout_sentinel[RW]
message_callout_symbol[RW]
message_sentinel[RW]
#¶ ↑
Note that the various .ascii files will keep the entries for message_sentinel
, message_callout_symbol
and message_callout_sentinel
within that respective file.
This explains why eval is used - we will eval on the header.
#¶ ↑
Public Class Methods
new( this_template_file, run_already = true )
click to toggle source
Public Instance Methods
alignment=(i)
click to toggle source
alignment?()
click to toggle source
#¶ ↑
alignment?¶ ↑
#¶ ↑
# File lib/ascii_paradise/ascii_say/parse_static_ascii.rb, line 208 def alignment? @alignment end
Also aliased as: alignment
parse(i)
click to toggle source
#¶ ↑
parse¶ ↑
Input to this method should be an Array.
#¶ ↑
# File lib/ascii_paradise/ascii_say/parse_static_ascii.rb, line 79 def parse(i) array = i.split(/^---\n/) # Split on lines starting with --- and then a newline. # ======================================================================= # # === Get rid of comments. This seems to be unnecessary, though. # It is retained here in the event that we may wish to support # comments in these files one day. # ======================================================================= # # array.reject! {|entry| entry.strip.start_with? '#' } # ======================================================================= # # Our .ascii file typically has a header and a body section. Obtain # each section next. # ======================================================================= # header, body = array binding.eval(header) # <- The header includes 3 instance variables. set_body(body) # Set the body. end
parse_the_template_file(i = @path_to_the_template_file)
click to toggle source
print(i = nil)
click to toggle source
#¶ ↑
print¶ ↑
#¶ ↑
# File lib/ascii_paradise/ascii_say/parse_static_ascii.rb, line 99 def print(i = nil) if i # ===================================================================== # # Create a clean slate for the body buffer. We will overwrite # it at a later time. # # Note that message_sentinel is typically the character 'x', # so this character will be removed in the following line. # ===================================================================== # body_buffer = @body.tr(message_sentinel, ' ') # ===================================================================== # # Handle the case when there is a message_callout_sentinel deined. # ===================================================================== # if message_callout_sentinel # =================================================================== # # In this case, replace one with the other. # =================================================================== # body_buffer = body_buffer.tr( message_callout_sentinel, message_callout_symbol ) end body_buffer = body_buffer.each_line.to_a line_idx_stack = @body.each_line.each_with_index.select { |line, idx| line[/#{Regexp.escape(message_sentinel)}/] } array_message_stack = i.split(' ') # ===================================================================== # # Obtain the current message next: # ===================================================================== # current_message = array_message_stack.pop until (current_message.nil? && array_message_stack.empty?) || line_idx_stack.empty? current_line, idx = line_idx_stack.pop message_buffer = '' # =================================================================== # # TODO making an assumption that lines only have one place # to replace text and it is contiguous. # =================================================================== # current_line_capacity = current_line[/#{Regexp.escape(message_sentinel)}+/].length # =================================================================== # # Until no more messages or buffer would overflow # =================================================================== # until current_message.nil? || (current_message.length + (message_buffer.length == 0 ? 0 : message_buffer.length + 1)) > current_line_capacity # ================================================================= # # Add the message to the buffer. # ================================================================= # if message_buffer.length > 0 message_buffer = [current_message, message_buffer].join(' ') else message_buffer = current_message end current_message = array_message_stack.pop end # =================================================================== # # Pad the buffer to capacity. # =================================================================== # case alignment # case tag. # =================================================================== # # === :left # # This entry point will shift the message-buffer by a bit. # =================================================================== # when :left message_buffer << ' ' * (current_line_capacity - message_buffer.length) # =================================================================== # # === :right # # Align to the right hand side of the ASCII picture. # =================================================================== # when :right message_buffer.insert 0, ' ' * (current_line_capacity - message_buffer.length) else # ================================================================= # # Determine the half. # ================================================================= # half = (current_line_capacity - message_buffer.length) / 2.0 message_buffer = message_buffer.dup if message_buffer.frozen? message_buffer.insert(0, ' ' * half.floor) message_buffer << ' ' * half.ceil end # =================================================================== # # Flush the buffer. # =================================================================== # line = current_line.gsub( /#{Regexp.escape(message_sentinel * current_line_capacity)}/, message_buffer ) body_buffer[idx] = line end if array_message_stack.empty? puts body_buffer.join else # The error may be raised when we have too many characters. n_characters = array_message_stack.join(' ').size raise "Too many characters given. Should not give "\ "more than #{n_characters} characters." end else body = @body.tr(message_sentinel, ' ') if message_callout_sentinel body.tr!(message_callout_sentinel, ' ') end e body end end
reset()
click to toggle source
run()
click to toggle source
set_body(i)
click to toggle source
set_path_to_the_template_file(i)
click to toggle source
#¶ ↑
set_path_to_the_template_file
¶ ↑
Set to the path where the template file at hand is kept. This is important, as otherwise the class will not be able to work properly.
#¶ ↑
# File lib/ascii_paradise/ascii_say/parse_static_ascii.rb, line 56 def set_path_to_the_template_file(i) @path_to_the_template_file = i end