Sha256: 533b684d26684604e3383fc0b26159cc54eb7a5c58f1d82d45d171f0aeb1e737
Contents?: true
Size: 1.13 KB
Versions: 12
Compression:
Stored size: 1.13 KB
Contents
module MyHelp class Md2Hash attr_accessor :md_text, :contents, :results, :opts def initialize(md_text = "", **opts) @opts = opts @md_text = md_text.split("\n") @contents = fsm #fsm() end TRANS = { #current # new action #----------------------------------- init: { #"*" => [:init, :ignore], :default => [:init, :ignore], "#" => [:reading, :item], }, reading: { "#" => [:reading, :item], :default => [:reading, :data], }, } 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 end end
Version data entries
12 entries across 12 versions & 1 rubygems