mini_mindmap 0.1.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03611e87e07f7d586a6f0338a57d671e476d67e7dbc727d9977bdb0f5876ed48
4
- data.tar.gz: cb861ebcadcf43ea70e463063425b7ef4fb14e247880edc8a1e98490bd4cb985
3
+ metadata.gz: '0941fbef61833009099b52dc0561ef6c2672bffd0e5e12ae16a54db7a1aa479d'
4
+ data.tar.gz: ee55990fa4bced3ce05ff9e69099d806bfc4cb3bf0af5695f5f72501ffa26353
5
5
  SHA512:
6
- metadata.gz: 1bf82626bfe580a51e38d55a1b3cbd61d5f160d5b55e1d8e81f885ad331b37b831d2f688f5e9d061389b80ab6361a3da747b30fdf569f111de133d64f0be728e
7
- data.tar.gz: 9d980a34bc29f60afcee9b87f671b2d3bafc9938dfa3a728b71a605d88f520f95a1e458f41317e05a0db043816116a258a5decb4cdc4806d599bce62b6d9b399
6
+ metadata.gz: 95894edeafb90a53e04bae3429e5b2e0fbcc7bc487d98b8a9d23524396210c9a594055bf5c316f1b6f731d7d8149f3d2b533c506a1facf69ba3216d69665c6d5
7
+ data.tar.gz: 0cf17b703155452c3252be70afbe622b4440736ed92f5c9f28e74d4f74928ea8856e02ee7a2ed948cbee8e437400182ccd61a2e746371c4ac725276706e483e6
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
File without changes
@@ -1,3 +1,21 @@
1
+ ## 0.4.1
2
+
3
+ * Fix name bug.
4
+
5
+ ## 0.4.0
6
+
7
+ * Add Config Field with `node` ,`edge`. You can config fonts sharp etc.
8
+ * Also support add attrs http://www.graphviz.org/doc/info/attrs.html
9
+ now we can write config like `** name [color=red]`
10
+
11
+ ## 0.3.0
12
+
13
+ * Change to node style to export dotfile.
14
+
15
+ ## 0.2.0
16
+
17
+ * Export mindmap.
18
+
1
19
  ## 0.1.0
2
20
 
3
21
  * Initial gem release of 0.1 codebase.
File without changes
data/Gemfile CHANGED
File without changes
File without changes
File without changes
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # MiniMindmap
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mini_mindmap`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ## Prepare
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ You need install graphviz first.
6
+
7
+ [graphviz download](http://www.graphviz.org/download/)
6
8
 
7
9
  ## Installation
8
10
 
@@ -22,23 +24,74 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ DSL Example
26
28
 
27
- ## Development
29
+ ```ruby
30
+ require "mini_mindmap"
31
+
32
+ name = 'mindmap' # filename
33
+
34
+ # support add attrs http://www.graphviz.org/doc/info/attrs.html
35
+ config = {
36
+ rankdir: "LR",# "TB", "LR", "BT", "RL", 分别对应于从上到下,从左到右,从下到上和从右到左绘制的有向图
37
+ node: {
38
+ fontname: "wqy-microhei",
39
+ fontcolor: "black"
40
+ },
41
+ edge: {
42
+ fontname: "wqy-microhei"
43
+ }
44
+ }
45
+ output = {
46
+ format: 'png',
47
+ dir: "#{Dir.home}/mindmap" # output dir
48
+ }
49
+
50
+ # online
51
+
52
+ dsl = %Q{
53
+ * MiniMindmap
54
+ ** name
55
+ ** DSL
56
+ ** output
57
+ *** dir
58
+ *** format
59
+ ** 汉字测试
60
+ }
61
+
62
+ demo = MiniMindmap::Mindmap.new(name,dsl,output=output,config=config)
63
+
64
+ demo.export # export files to dir
28
65
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+ ```
67
+ output img
30
68
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+ ![demo.png](https://wx2.sbimg.cn/2020/07/16/CiWr6.png)
32
70
 
33
- ## Contributing
71
+ ## Q&A
34
72
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/mark24code/mini_mindmap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mark24code/mini_mindmap/blob/master/CODE_OF_CONDUCT.md).
73
+ ### Chinese charset code problem
36
74
 
75
+ 1. To check fonts. Run `fc-list` or go to `/etc/fonts` to check if you have correct fonts.
37
76
 
38
- ## License
77
+ 2. You may need to install fonts, for example : Chinese font —— 文泉驿。 Run `sudo apt list fonts-wqy-zenhei`
78
+ 3. Make config with correct font name
39
79
 
40
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+ >
81
+ ```ruby
82
+ config = {
83
+ rankdir: "LR",# "TB", "LR", "BT", "RL" direction
84
+ node: {
85
+ fontname: "wqy-microhei"
86
+ },
87
+ edge: {
88
+ fontname: "wqy-microhei"
89
+ }
90
+ }
91
+
92
+ demo = MiniMindmap::Mindmap.new(name,dsl,output=output,config=config)
41
93
 
42
- ## Code of Conduct
94
+ ```
95
+ ## License
43
96
 
44
- Everyone interacting in the MiniMindmap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mark24code/mini_mindmap/blob/master/CODE_OF_CONDUCT.md).
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
File without changes
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-cayman
@@ -1,29 +1,36 @@
1
- react_mind = RubyMindmap::Mindmap.new do |m|
2
- m.name = 'React'
3
- m.output = {
4
- format: 'png',
5
- dir: './out'
1
+ require_relative "helper.rb"
2
+ require "mini_mindmap"
3
+
4
+ name = 'mindmap' # filename
5
+
6
+ # support add attrs http://www.graphviz.org/doc/info/attrs.html
7
+ config = {
8
+ rankdir: "LR",# "TB", "LR", "BT", "RL", 分别对应于从上到下,从左到右,从下到上和从右到左绘制的有向图
9
+ node: {
10
+ fontname: "wqy-microhei",
11
+ fontcolor: "black"
12
+ },
13
+ edge: {
14
+ fontname: "wqy-microhei"
6
15
  }
16
+ }
17
+ output = {
18
+ format: 'png',
19
+ dir: "#{Dir.home}/mindmap" # output dir
20
+ }
21
+
22
+ # online
7
23
 
24
+ dsl = %Q{
25
+ * MiniMindmap
26
+ ** name
27
+ ** DSL
28
+ ** output
29
+ *** dir
30
+ *** format
31
+ ** 汉字测试
32
+ }
8
33
 
9
- m.dsl = <<-Mindmap
10
- * React
11
- ** 生命周期
12
- *** ComponentDidMount
13
- **** ComponentWillMount
14
- ***** AAAComponentWillMount
15
- ** BBBB
16
- *** CCCCC
17
- **** DDDD
18
- ***** FFFF
19
- ******* KK
20
- ** OPLL
21
- * GO
22
- ** Woker
23
- *** KKKKKLLL
24
- * CASIO
25
- * KO
26
- Mindmap
27
- end
34
+ demo = MiniMindmap::Mindmap.new(name,dsl,output=output,config=config)
28
35
 
29
- react_mind.export
36
+ demo.export # export files to dir
@@ -0,0 +1 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
Binary file
@@ -1,75 +1,146 @@
1
1
  require "mini_mindmap/version"
2
+ require "mini_mindmap/utils/deep_merge"
3
+ require "fileutils"
2
4
 
3
5
  module MiniMindmap
4
6
  class Error < StandardError; end
5
7
 
6
8
  class Mindmap
7
- def initialize(name, dsl)
9
+ @@compiles_meta = {
10
+ basic: {
11
+ id: "basic",
12
+ description: "basic expression",
13
+ syntax: /^(\*+)\s+([^\s]*[^\[\]]*)\s*(\[.*\])*\s*$/,
14
+ processor: "basic_processor",
15
+ }
16
+ }
17
+ def self.compiles_meta
18
+ @@compiles_meta
19
+ end
20
+
21
+ def initialize(name, dsl,output={},config={})
22
+
8
23
  @name = name
9
24
  @dsl = dsl
25
+ @config = {
26
+ rankdir: 'LR', # "TB", "LR", "BT", "RL", 分别对应于从上到下,从左到右,从下到上和从右到左绘制的有向图
27
+ node: {},
28
+ edge: {}
29
+ }.deep_merge(config)
30
+ @output = {
31
+ dir: Dir.home,
32
+ format: "png"
33
+ }.deep_merge(output)
10
34
 
11
35
  yield(self) if block_given?
12
-
13
- # self.compile
14
36
  end
15
37
 
16
- attr_accessor(:name, :dsl, :output, :nodes)
38
+ attr_accessor(:name, :dsl, :config, :output, :nodes, :declares)
17
39
 
18
- def node(code)
40
+ def compile(code)
19
41
  # TODO 增加拓展语法支持 label等自定义
20
- # 同步Test也要更新
21
- node_express = /^(\*+)\s+([^\s]+.*)$/
22
- node_express =~ code.strip
23
-
24
- level_prefix = $1
25
- content = $2
26
- level = level_prefix.length
27
- node = [level, content]
42
+ case code.strip
43
+ when @@compiles_meta[:basic][:syntax]
44
+ level_prefix = $1
45
+ content = $2
46
+ config = $3
47
+ level = level_prefix.length
48
+ node = [@@compiles_meta[:basic][:id],level, content]
49
+
50
+ if config
51
+ node.push(config)
52
+ end
53
+
54
+ return node
55
+ else
56
+ # pass
57
+ end
28
58
  end
29
59
 
30
- def dsl_to_nodes
60
+ def basic_processor
61
+ declares = [];
31
62
  nodes = []
32
63
  stack = []
33
64
  dsl = @dsl.split("\n")
34
65
  dsl.each_with_index do |code, current_index|
35
- current = self.node(code)
36
- unless stack.empty?
37
- top = stack.pop
38
- if (current[0] > top[0])
39
- (nodes << "#{top[1]} -> #{current[1]}")
40
- stack.push(top)
41
- else
42
- while (current[0] <= top[0]) and (not stack.empty?)
43
- top = stack.pop
66
+
67
+ if not code.strip.empty?
68
+ current_id = current_index
69
+ current = self.compile(code)
70
+ current.unshift(current_id)
71
+ # [id, type_id, level, content, config]
72
+
73
+
74
+ current_declare = "node#{current_id}[label=\"#{current[3]}\"]";
75
+ declares.push(current_declare)
76
+
77
+ unless stack.empty?
78
+ top = stack.pop
79
+ if current[2] > top[2]
80
+ node_line = "node#{top[0]} -> node#{current[0]}"
81
+ if current.length >=5
82
+ node_line += " #{current[4]}"
83
+ end
84
+ nodes << node_line
85
+ stack.push(top)
86
+ else
87
+ while (current[2] <= top[2]) and (not stack.empty?)
88
+ top = stack.pop
89
+ end
90
+ if current[2] > top[2]
91
+ node_line = "node#{top[0]} -> node#{current[0]}"
92
+ if current.length >=5
93
+ node_line += " #{current[4]}"
94
+ end
95
+ nodes << node_line
96
+ stack.push top
97
+ end
98
+
44
99
  end
45
- (nodes << "#{top[1]} -> #{current[1]}") if (current[0] > top[0])
46
100
  end
101
+ stack.push(current)
47
102
  end
48
- stack.push(current)
49
103
  end
104
+ @declares = declares
50
105
  @nodes = nodes
51
106
  end
52
107
 
108
+ def processor
109
+ self.send @@compiles_meta[:basic][:processor]
110
+ end
111
+
53
112
  def package_nodes
54
- "digraph #{@name} {\n#{@nodes.join("\n")}\n}"
113
+ node_config = @config[:node].map {|k,v| "#{k}=\"#{v}\""}.join(",")
114
+ edge_config = @config[:edge].map {|k,v| "#{k}=\"#{v}\""}.join(",")
115
+ "digraph \"#{@name}\" {\nrankdir = #{@config[:rankdir]};\nnode [#{node_config}];\nedge [#{edge_config}];\n#{@declares.join("\n")}\n#{@nodes.join("\n")}\n}"
55
116
  end
56
117
 
57
118
  def nodes_to_doc
58
- File.open("#{@name}.dot", "w") { |f| f.write(package_nodes) }
119
+ output_dir = File.absolute_path(@output[:dir])
120
+ FileUtils::mkdir_p output_dir
121
+ output_file = File.join(output_dir, "#{@name}.dot")
122
+
123
+ File.open("#{output_file}", "w") { |f| f.write(package_nodes) }
59
124
  end
60
125
 
61
- def compile
62
- self.dsl_to_nodes
126
+ def run_tasks
127
+ self.processor
63
128
  self.package_nodes
64
129
  self.nodes_to_doc
65
130
  end
66
131
 
67
132
  def export_cmd
68
- "dot #{@name}.dot -T #{@output[:format]} -o #{@name}.#{@output[:format]}"
133
+ output_dir = @output[:dir]
134
+ output_file = File.join(output_dir, "#{@name}.#{@output[:format]}")
135
+
136
+ output_dotfile = File.join(output_dir, "#{@name}.dot")
137
+ "dot #{output_dotfile} -T #{@output[:format]} -o #{output_file}"
69
138
  end
70
139
 
71
140
  def export
141
+ self.run_tasks
72
142
  export = self.export_cmd
143
+
73
144
  puts("[command]: #{export}")
74
145
  `#{export}`
75
146
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ # Returns a new hash with +self+ and +other_hash+ merged recursively.
5
+ #
6
+ # h1 = { a: true, b: { c: [1, 2, 3] } }
7
+ # h2 = { a: false, b: { x: [3, 4, 5] } }
8
+ #
9
+ # h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
10
+ #
11
+ # Like with Hash#merge in the standard library, a block can be provided
12
+ # to merge values:
13
+ #
14
+ # h1 = { a: 100, b: 200, c: { c1: 100 } }
15
+ # h2 = { b: 250, c: { c1: 200 } }
16
+ # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
17
+ # # => { a: 100, b: 450, c: { c1: 300 } }
18
+ def deep_merge(other_hash, &block)
19
+ dup.deep_merge!(other_hash, &block)
20
+ end
21
+
22
+ # Same as +deep_merge+, but modifies +self+.
23
+ def deep_merge!(other_hash, &block)
24
+ merge!(other_hash) do |key, this_val, other_val|
25
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
26
+ this_val.deep_merge(other_val, &block)
27
+ elsif block_given?
28
+ block.call(key, this_val, other_val)
29
+ else
30
+ other_val
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module MiniMindmap
2
- VERSION = "0.1.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = %q{A kind of DSL to generate mindmap.}
10
10
  spec.description = %q{[Experimenting!] A kind of DSL to generate mindmap. Depend on Graphviz.}
11
- spec.homepage = "https://github.com/Mark24Code/mini_mindmap"
11
+ spec.homepage = "https://mark24code.github.io/mini_mindmap/"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_mindmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark24
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-16 00:00:00.000000000 Z
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "[Experimenting!] A kind of DSL to generate mindmap. Depend on Graphviz."
14
14
  email:
@@ -26,17 +26,21 @@ files:
26
26
  - LICENSE.txt
27
27
  - README.md
28
28
  - Rakefile
29
+ - _config.yml
29
30
  - bin/console
30
31
  - bin/setup
31
32
  - examples/demo.rb
33
+ - examples/helper.rb
34
+ - examples/mindmap.png
32
35
  - lib/mini_mindmap.rb
36
+ - lib/mini_mindmap/utils/deep_merge.rb
33
37
  - lib/mini_mindmap/version.rb
34
38
  - mini_mindmap.gemspec
35
- homepage: https://github.com/Mark24Code/mini_mindmap
39
+ homepage: https://mark24code.github.io/mini_mindmap/
36
40
  licenses:
37
41
  - MIT
38
42
  metadata:
39
- homepage_uri: https://github.com/Mark24Code/mini_mindmap
43
+ homepage_uri: https://mark24code.github.io/mini_mindmap/
40
44
  source_code_uri: https://github.com/Mark24Code/mini_mindmap
41
45
  changelog_uri: https://github.com/Mark24Code/mini_mindmap/blob/master/CHANGELOG.md
42
46
  post_install_message: