class MessengerPigeon::OrgMode::Target

Target definition

Public Class Methods

new(options = nil) click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 15
def initialize(options = nil)
  @options = options
  open_file File.expand_path(options[:file])
  @known_headings = known_headings
end

Public Instance Methods

complete() click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 74
def complete
  @fd.seek 0
  @fd.write filedata
  @fd.close
end
filedata() click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 21
def filedata
  (@filedata.join "\n") + "\n"
end
find_next_heading(star_count, start_line, end_line) click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 55
def find_next_heading(star_count, start_line, end_line)
  @filedata[start_line + 1..end_line].each do |line|
    if /^\*{#{star_count}}\ (?<heading>.+?)\ +(:[^ ]*:)?$/ =~ line
      return start_line
    end
    start_line += 1
  end
  end_line
end
find_target(headings, star_count, start_line, end_line) click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 34
def find_target(headings, star_count, start_line, end_line)
  if headings.empty?
    # Found the target
    return start_line + 1
  end
  target_heading = headings.pop
  @filedata[start_line..end_line].each do |line|
    if /^\*{#{star_count}}\ #{target_heading}(\ +(:[^ ]*:)?)?$/ =~ line
      return find_target(headings, star_count + 1, start_line, find_next_heading(star_count, start_line, end_line))
    end
    start_line += 1
  end
  # Left-over headings
  ([target_heading] + headings).each do |heading|
    @filedata.insert end_line, "#{'*' * star_count} #{heading}"
    star_count += 1
    end_line += 1
  end
  end_line
end
known_headings() click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 25
def known_headings
  res = []
  @filedata.each do |l|
    /^\*+\ (?<heading>.+?)\ +(:[^ ]*:)?$/ =~ l
    res.push heading if heading
  end
  res
end
update(data) click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 65
def update(data)
  selector = @options[:heading_selector] % data
  headings = selector.split(@options[:level_separator])
  star_count = 1
  target = find_target headings.reverse, star_count, 0, @filedata.length
  data_string = @options[:data_format].call data
  @filedata.insert target, data_string
end

Private Instance Methods

open_file(file) click to toggle source
# File lib/messenger_pigeon/modules/orgmode.rb, line 82
def open_file file
  if File.exist? file
    @fd = File.open(file, 'r+')
    @fd.seek 0
    @filedata = @fd.read nil
  else
    @fd = File.open(file, 'w')
    @filedata = ''
  end
  @filedata = @filedata.split(/[\n\r]/)
end