Sha256: 0c6b75ff804beda12dc726a4d3b290ae8f92b9296b82f94e73deefd81df36110

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

module MyHelp
  # get org and trans it to hash by FSM
  class Org2Hash
    attr_accessor :contents, :text
    # current_state => [  new_state,    action ]
    TRANSITIONS = {
      :header_read => {
        "* " => [:contents_read, :start_new_item],
        :default => [:header_read, :ignore],
      },
      :contents_read => {
        "* " => [:contents_read, :start_new_item],
        :default => [:contents_read, :add_contents],
      },
    }

    def initialize(org_text)
      @text = org_text
      @contents = Hash.new
      simple_fsm()
    end

    def simple_fsm()
      state = :header_read
      item = ""
      @text.split("\n").each do |line|
        next if line.size < 1
        state, action = TRANSITIONS[state][line[0..1]] ||
                        TRANSITIONS[state][:default]
        case action
        when :ignore
        when :start_new_item
          item = read_item(line)
          @contents[item] = ""
        when :add_contents
          @contents[item] += line + "\n"
        end
      end
    end

    def read_item(line)
      m = line.match(/\* (.+)/)
      item = if m
               m[1].match(/head\s*/) ? "head" : m[1]
             else
               nil
             end
      return item
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
my_help-1.2.10 lib/my_help/org2hash.rb
my_help-1.2.9 lib/my_help/org2hash.rb
my_help-1.2.8 lib/my_help/org2hash.rb
my_help-1.2.7 lib/my_help/org2hash.rb