Class: MyHelp::Md2Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/my_help/md2hash.rb

Constant Summary collapse

TRANS =
{
  #current
  #            new         action
  #-----------------------------------
  init: {
    #"*" => [:init, :ignore],
    :default => [:init, :ignore],
    "#" => [:reading, :item],
  },
  reading: {
    "#" => [:reading, :item],
    :default => [:reading, :data],
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(md_text = "", **opts) ⇒ Md2Hash

Returns a new instance of Md2Hash.



5
6
7
8
9
10
# File 'lib/my_help/md2hash.rb', line 5

def initialize(md_text = "", **opts)
  @opts = opts
  @md_text = md_text.split("\n")
  @contents = fsm
  #fsm()
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



3
4
5
# File 'lib/my_help/md2hash.rb', line 3

def contents
  @contents
end

#md_textObject

Returns the value of attribute md_text.



3
4
5
# File 'lib/my_help/md2hash.rb', line 3

def md_text
  @md_text
end

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/my_help/md2hash.rb', line 3

def opts
  @opts
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/my_help/md2hash.rb', line 3

def results
  @results
end

Instance Method Details

#fsmObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/my_help/md2hash.rb', line 27

def fsm
  contents = {}
  state = :init
  @results = []
  item = ""
  @md_text.each do |line|
    state, action = TRANS[state][line[0]] || TRANS[state][:default]

    if line[0..1] == "#+" # line[1] != " "
      state = :init
      action = :ignore
    end

    results << [line, state, action]

    case action
    when :ignore
    when :item
      item = line.match(/^# (.+)/)[1]
      contents[item] = []
    when :data
      contents[item] << line
    end
  end
  return contents
end