Class: WikiAnnotate

Inherits:
Object
  • Object
show all
Defined in:
app/models/wiki_page.rb

Overview

Since:

  • 0.7.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ WikiAnnotate

Returns a new instance of WikiAnnotate



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/wiki_page.rb', line 298

def initialize(content)
  @content = content
  current = content
  current_lines = current.text.split(/\r?\n/)
  @lines = current_lines.collect {|t| [nil, nil, t]}
  positions = []
  current_lines.size.times {|i| positions << i}
  while (current.previous)
    d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
    d.each_slice(3) do |s|
      sign, line = s[0], s[1]
      if sign == '+' && positions[line] && positions[line] != -1
        if @lines[positions[line]][0].nil?
          @lines[positions[line]][0] = current.version
          @lines[positions[line]][1] = current.author
        end
      end
    end
    d.each_slice(3) do |s|
      sign, line = s[0], s[1]
      if sign == '-'
        positions.insert(line, -1)
      else
        positions[line] = nil
      end
    end
    positions.compact!
    # Stop if every line is annotated
    break unless @lines.detect { |line| line[0].nil? }
    current = current.previous
  end
  @lines.each { |line|
    line[0] ||= current.version
    # if the last known version is > 1 (eg. history was cleared), we don't know the author
    line[1] ||= current.author if current.version == 1
  }
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content



296
297
298
# File 'app/models/wiki_page.rb', line 296

def content
  @content
end

#linesObject (readonly)

Returns the value of attribute lines



296
297
298
# File 'app/models/wiki_page.rb', line 296

def lines
  @lines
end