class MiniMindmap::Mindmap

Attributes

config[RW]
declares[RW]
dsl[RW]
name[RW]
nodes[RW]
output[RW]

Public Class Methods

compiles_meta() click to toggle source
# File lib/mini_mindmap.rb, line 23
def self.compiles_meta
  @@compiles_meta
end
new(name, dsl,output={},config={}) { |self| ... } click to toggle source
# File lib/mini_mindmap.rb, line 27
def initialize(name, dsl,output={},config={})

  @name = name
  @dsl = dsl
  @config =  {
    rankdir: 'LR', # "TB", "LR", "BT", "RL", 分别对应于从上到下,从左到右,从下到上和从右到左绘制的有向图
    node: {},
    edge: {}
  }.deep_merge(config)
  @output = {
    dir: Dir.home,
    format: "png"
  }.deep_merge(output) 

  yield(self) if block_given?
end

Public Instance Methods

basic_processor() click to toggle source
# File lib/mini_mindmap.rb, line 68
def basic_processor
  declares = [];
  nodes = []
  stack = []
  dsl = @dsl.split("\n")
  dsl.each_with_index do |code, current_index|

    if not code.strip.empty?
      current_id = current_index
      current = self.compile(code)
      if not current
        next
      end
      current.unshift(current_id)
      # [id, type_id, level, content, config]


      current_declare = "node#{current_id}[label=\"#{current[3]}\"]";
      declares.push(current_declare)

      unless stack.empty?
        top = stack.pop
        if current[2] > top[2]
          node_line = "node#{top[0]} -> node#{current[0]}"
          if current.length >=5
            node_line += " #{current[4]}"
          end
          nodes << node_line 
          stack.push(top)
        else
          while (current[2] <= top[2]) and (not stack.empty?)
            top = stack.pop
          end
          if current[2] > top[2]
            node_line = "node#{top[0]} -> node#{current[0]}"
            if current.length >=5
              node_line += " #{current[4]}"
            end
            nodes << node_line 
            stack.push top
          end
          
        end
      end
      stack.push(current)
    end
  end
  @declares = declares
  @nodes = nodes
end
compile(code) click to toggle source
# File lib/mini_mindmap.rb, line 46
def compile(code)
    # TODO  增加拓展语法支持 label等自定义
  case code.strip
  when @@compiles_meta[:basic][:syntax]
    level_prefix = $1
    content = $2
    config = $3
    level = level_prefix.length
    node = [@@compiles_meta[:basic][:id],level, content]

    if config
      node.push(config)
    end
  
    return node
  when @@compiles_meta[:annotation][:syntax]
    # pass annotation
  else
    # rest pass
  end
end
export() click to toggle source
# File lib/mini_mindmap.rb, line 151
def export
  self.run_tasks
  export = self.export_cmd

  puts("[command]: #{export}")
  `#{export}`
end
export_cmd() click to toggle source
# File lib/mini_mindmap.rb, line 143
def export_cmd
  output_dir = @output[:dir]
  output_file = File.join(output_dir, "#{@name}.#{@output[:format]}")

  output_dotfile = File.join(output_dir, "#{@name}.dot")
  "dot #{output_dotfile} -T #{@output[:format]} -o #{output_file}"
end
nodes_to_doc() click to toggle source
# File lib/mini_mindmap.rb, line 129
def nodes_to_doc
  output_dir = File.absolute_path(@output[:dir])
  FileUtils::mkdir_p output_dir
  output_file = File.join(output_dir, "#{@name}.dot")

  File.open("#{output_file}", "w") { |f| f.write(package_nodes) }
end
package_nodes() click to toggle source
# File lib/mini_mindmap.rb, line 123
def package_nodes
  node_config = @config[:node].map {|k,v| "#{k}=\"#{v}\""}.join(",")
  edge_config = @config[:edge].map {|k,v| "#{k}=\"#{v}\""}.join(",")
  "digraph \"#{@name}\" {\nrankdir = #{@config[:rankdir]};\nnode [#{node_config}];\nedge [#{edge_config}];\n#{@declares.join("\n")}\n#{@nodes.join("\n")}\n}"
end
processor() click to toggle source
# File lib/mini_mindmap.rb, line 119
def processor
  self.send @@compiles_meta[:basic][:processor]  
end
run_tasks() click to toggle source
# File lib/mini_mindmap.rb, line 137
def run_tasks
  self.processor
  self.package_nodes
  self.nodes_to_doc
end