Class: AsciiParadise::ParseStaticAscii

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_paradise/ascii_say/parse_static_ascii.rb

Overview

AsciiParadise::ParseStaticAscii

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(this_template_file, run_already = true) ⇒ ParseStaticAscii

initialize

The first argument must be the path to the template file in question.

#


39
40
41
42
43
44
45
46
47
48
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 39

def initialize(
    this_template_file,
    run_already = true
  )
  reset
  set_path_to_the_template_file(
    this_template_file
  )
  run if run_already
end

Instance Attribute Details

#message_callout_sentinelObject

Returns the value of attribute message_callout_sentinel.



32
33
34
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 32

def message_callout_sentinel
  @message_callout_sentinel
end

#message_callout_symbolObject

Returns the value of attribute message_callout_symbol.



31
32
33
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 31

def message_callout_symbol
  @message_callout_symbol
end

#message_sentinelObject

#

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.

#


30
31
32
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 30

def message_sentinel
  @message_sentinel
end

Instance Method Details

#alignment=(i) ⇒ Object

#

alignment=

#


215
216
217
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 215

def alignment=(i)
  @alignment = i
end

#alignment?Boolean Also known as: alignment

#

alignment?

#

Returns:

  • (Boolean)


208
209
210
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 208

def alignment?
  @alignment
end

#parse(i) ⇒ Object

#

parse

Input to this method should be an Array.

#


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

#

parse_the_template_file

#


222
223
224
225
226
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 222

def parse_the_template_file(i = @path_to_the_template_file)
  parse(
    File.read(i)
  )
end
#

print

#


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
# 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

#resetObject

#

reset

#


63
64
65
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 63

def reset
  @alignment = :center
end

#runObject

#

run

#


231
232
233
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 231

def run
  parse_the_template_file
end

#set_body(i) ⇒ Object

#

set_body

#


70
71
72
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 70

def set_body(i)
  @body = i
end

#set_path_to_the_template_file(i) ⇒ Object

#

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.

#


56
57
58
# 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