Class: AsciiParadise::ParseStaticAscii
- Inherits:
-
Object
- Object
- AsciiParadise::ParseStaticAscii
- Defined in:
- lib/ascii_paradise/ascii_say/parse_static_ascii.rb
Overview
AsciiParadise::ParseStaticAscii
Instance Attribute Summary collapse
-
#message_callout_sentinel ⇒ Object
Returns the value of attribute message_callout_sentinel.
-
#message_callout_symbol ⇒ Object
Returns the value of attribute message_callout_symbol.
-
#message_sentinel ⇒ Object
# Note that the various .ascii files will keep the entries for message_sentinel, message_callout_symbol and message_callout_sentinel within that respective file.
Instance Method Summary collapse
-
#alignment=(i) ⇒ Object
# === alignment= ========================================================================= #.
-
#alignment? ⇒ Boolean
(also: #alignment)
# === alignment? ========================================================================= #.
-
#initialize(this_template_file, run_already = true) ⇒ ParseStaticAscii
constructor
initialize.
-
#parse(i) ⇒ Object
# === parse.
-
#parse_the_template_file(i = @path_to_the_template_file) ⇒ Object
# === parse_the_template_file ========================================================================= #.
-
#print(i = nil) ⇒ Object
(also: #ee, #say)
# === print ========================================================================= #.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#run ⇒ Object
# === run ========================================================================= #.
-
#set_body(i) ⇒ Object
# === set_body ========================================================================= #.
-
#set_path_to_the_template_file(i) ⇒ Object
# === set_path_to_the_template_file.
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_sentinel ⇒ Object
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 end |
#message_callout_symbol ⇒ Object
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 end |
#message_sentinel ⇒ Object
#
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 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?
#
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(i = nil) ⇒ Object Also known as: ee, say
#
#
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(, ' ') # ===================================================================== # # Handle the case when there is a message_callout_sentinel deined. # ===================================================================== # if # =================================================================== # # In this case, replace one with the other. # =================================================================== # body_buffer = body_buffer.tr( , ) end body_buffer = body_buffer.each_line.to_a line_idx_stack = @body.each_line.each_with_index.select { |line, idx| line[/#{Regexp.escape()}/] } = i.split(' ') # ===================================================================== # # Obtain the current message next: # ===================================================================== # = .pop until (.nil? && .empty?) || line_idx_stack.empty? current_line, idx = line_idx_stack.pop = '' # =================================================================== # # TODO making an assumption that lines only have one place # to replace text and it is contiguous. # =================================================================== # current_line_capacity = current_line[/#{Regexp.escape()}+/].length # =================================================================== # # Until no more messages or buffer would overflow # =================================================================== # until .nil? || (.length + (.length == 0 ? 0 : .length + 1)) > current_line_capacity # ================================================================= # # Add the message to the buffer. # ================================================================= # if .length > 0 = [, ].join(' ') else = end = .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 << ' ' * (current_line_capacity - .length) # =================================================================== # # === :right # # Align to the right hand side of the ASCII picture. # =================================================================== # when :right .insert 0, ' ' * (current_line_capacity - .length) else # ================================================================= # # Determine the half. # ================================================================= # half = (current_line_capacity - .length) / 2.0 = .dup if .frozen? .insert(0, ' ' * half.floor) << ' ' * half.ceil end # =================================================================== # # Flush the buffer. # =================================================================== # line = current_line.gsub( /#{Regexp.escape( * current_line_capacity)}/, ) body_buffer[idx] = line end if .empty? puts body_buffer.join else # The error may be raised when we have too many characters. n_characters = .join(' ').size raise "Too many characters given. Should not give "\ "more than #{n_characters} characters." end else body = @body.tr(, ' ') if body.tr!(, ' ') end e body end end |
#reset ⇒ Object
#
reset
#
63 64 65 |
# File 'lib/ascii_paradise/ascii_say/parse_static_ascii.rb', line 63 def reset @alignment = :center end |
#run ⇒ Object
#
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 |