Class: Ritsu::Block

Inherits:
Object
  • Object
show all
Includes:
BlockMixin
Defined in:
lib/ritsu/block.rb

Instance Attribute Summary collapse

Attributes included from BlockMixin

#contents, #id, #indent_length, #indent_level, #local_indentation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlockMixin

#add_line, #add_new_line, #block_structure?, #clear_contents, #indent, #initialize_block_mixin, #outdent

Constructor Details

#initialize(id = nil, options = {}) ⇒ Block

Returns a new instance of Block.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ritsu/block.rb', line 75

def initialize(id = nil, options={})
  options = {
    :block_start_prefix => "//<<",
    :block_end_prefix => "//>>"
  }.merge(options)

  @block_start_prefix = options[:block_start_prefix]
  @block_end_prefix = options[:block_end_prefix]
  
  initialize_block_mixin(id, options)
end

Instance Attribute Details

#block_end_prefixObject

Returns the value of attribute block_end_prefix.



73
74
75
# File 'lib/ritsu/block.rb', line 73

def block_end_prefix
  @block_end_prefix
end

#block_start_prefixObject

Returns the value of attribute block_start_prefix.



72
73
74
# File 'lib/ritsu/block.rb', line 72

def block_start_prefix
  @block_start_prefix
end

Class Method Details

.extract_block_id(str, prefix) ⇒ Object



87
88
89
# File 'lib/ritsu/block.rb', line 87

def self.extract_block_id(str, prefix)
  str.strip.slice((prefix.length)..-1).strip
end

.parse(*args) ⇒ Object



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
# File 'lib/ritsu/block.rb', line 184

def self.parse(*args)
  if args.length > 2
    raise ArgumentError.new("only at most 2 arguments, the second one is a hash, are accepted")
  end
  options = {}
  
  prepare_options_from_kth_arg = Proc.new do |k|
    options = options.merge(args[k]) if (args.length > k)
  end
  
  block = Block.new
  result = case args[0]
  when Array
    prepare_options_from_kth_arg.call(1)
    block.parse_lines(args[0], options)
  when String
    prepare_options_from_kth_arg.call(1)
    block.parse_string(args[0], options)
  when Hash
    if filename = args[0].delete(:file)
      prepare_options_from_kth_arg.call(0)
      block.parse_file(filename, options)
    else
      raise ArgumentError.new("you must specify :file option if the first argument is a hash")
    end
  end
  return block
end

Instance Method Details

#add_block(block) ⇒ Object



247
248
249
# File 'lib/ritsu/block.rb', line 247

def add_block(block)
  add_block_structure(block)
end

#add_content(content) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/ritsu/block.rb', line 251

def add_content(content)
  if content.kind_of?(Block)
    add_block(content)
  else
    add_line_or_other_content(content)
  end
end

#child_block_countObject



213
214
215
216
217
218
# File 'lib/ritsu/block.rb', line 213

def child_block_count
  count = 0
  contents.each do |content|
    count += 1 if content.kind_of?(Block)
  end
end

#child_block_with_id(id) ⇒ Block

Returns the first child block with the given ID. nil if there is no such child block.

Returns:

  • (Block)

    the first child block with the given ID. nil if there is no such child block.



222
223
224
225
226
227
228
229
# File 'lib/ritsu/block.rb', line 222

def child_block_with_id(id)
  contents.each do |content|
    if content.kind_of?(Block) and content.id == id
      return content
    end
  end
  return nil
end

#child_block_with_id_position(id) ⇒ Integer

Returns the position of the child block with the given ID in the contents array. nil if there is no such child block.

Returns:

  • (Integer)

    the position of the child block with the given ID in the contents array. nil if there is no such child block.



238
239
240
241
242
243
244
245
# File 'lib/ritsu/block.rb', line 238

def child_block_with_id_position(id)
  contents.length.times do |i|
    if contents[i].kind_of?(Block) and contents[i].id == id
      return i
    end
  end
  return nil
end

#child_blocksObject



231
232
233
# File 'lib/ritsu/block.rb', line 231

def child_blocks
  contents.select {|x| x.kind_of?(Block)}
end

#extract_block_id(str, prefix) ⇒ Object



91
92
93
# File 'lib/ritsu/block.rb', line 91

def extract_block_id(str, prefix)
  Block.extract_block_id(str, prefix)
end

#parse_file(filename, options = {}) ⇒ Object



150
151
152
153
# File 'lib/ritsu/block.rb', line 150

def parse_file(filename, options={})
  text = Ritsu::Utility::Files.read(filename)
  parse_string(text, options)
end

#parse_lines(lines, options = {}) ⇒ Object



95
96
97
98
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
# File 'lib/ritsu/block.rb', line 95

def parse_lines(lines, options={})
  options = {
    :block_start_prefix => block_start_prefix,
    :block_end_prefix => block_end_prefix
  }.merge(options)
  @block_start_prefix = options[:block_start_prefix]
  @block_end_prefix = options[:block_end_prefix]
  
  block_stack = [self]
  global_indentation_length = 0
  
  get_local_indentation = Proc.new do |line|
    leading_spaces_length = Ritsu::Utility::Strings.leading_spaces(line, options).length
    remaining_space_length = leading_spaces_length - global_indentation_length
    if remaining_space_length < 0 then remaining_space_length = 0 end
    " " * remaining_space_length
  end
  
  append_line = Proc.new do |line|
    block_stack.last.contents << (get_local_indentation.call(line) + line.lstrip)
  end
  
  lines.each do |line|
    if line.strip.starts_with?(block_start_prefix)
      id = extract_block_id(line, block_start_prefix)
      local_indentation = get_local_indentation.call(line)
      
      options[:local_indentation] = local_indentation
      block = Block.new(id, options)
      block_stack.last.contents << block
      block_stack.push(block)
      global_indentation_length += block.local_indentation.length
    elsif line.strip.starts_with?(block_end_prefix)
      id = extract_block_id(line, block_end_prefix)
      if block_stack.last.id == id
        block = block_stack.pop()
        global_indentation_length -= block.local_indentation.length
      else
        append_line.call(line)
      end
    else
      append_line.call(line)
    end
  end
  
  if block_stack.length != 1
    raise ArgumentError.new("error in input. some blocks are malformed")
  end
end

#parse_string(string, options = {}) ⇒ Object



145
146
147
148
# File 'lib/ritsu/block.rb', line 145

def parse_string(string, options={})
  lines = string.rstrip.split("\n")
  parse_lines(lines, options)
end

#to_s(options = {}) ⇒ Object

In all case, the generated string shall have no trailing whitespaces.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ritsu/block.rb', line 157

def to_s(options={})
  options = {:no_delimiter => false, :indentation => ""}.merge(options)
  no_delimiter = options.delete(:no_delimiter)
  indentation = options.delete(:indentation)
  
  io = StringIO.new
  io << indentation + local_indentation + block_start_prefix + " " + id + "\n" unless no_delimiter
  contents.each do |content|
    if content.kind_of?(Block)
      io << content.to_s({:indentation=>indentation+local_indentation}.merge(options)) + "\n"
    else
      io << indentation + local_indentation + content.to_s + "\n"
    end
  end
  io << indentation + local_indentation + block_end_prefix + " " + id unless no_delimiter
  
  io.string.rstrip
end

#write_to_file(filename, options = {}) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/ritsu/block.rb', line 176

def write_to_file(filename, options={})
  options = {:no_delimiter => true}.merge(options)
  
  File.open(filename, "w") do |f|
    f.write(to_s(options))
  end
end