mindwords 0.5.0 → 0.5.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: 318da7a9e4daf9a4f4d69d4315d062fab0f4b2876c3a0302a86d4d1b40909a32
4
- data.tar.gz: cd0336cf0a70c0e90fec5964f5a987cbad0abb9e295fc96ddf54a6999832af91
3
+ metadata.gz: cecbfa6443266445084268c94ccedf12ca1f1c4a8e601a510ad49d375c54e910
4
+ data.tar.gz: 184f031666fbc23c62ca1fb7b61bbfbce99a1bd88c75072fc9645153f4e1f502
5
5
  SHA512:
6
- metadata.gz: 1eb530ac7ff6db8b68e2827c51a7ad4ed0ed7244340e842b6ff967c2da1f32a777fff8fc2ddc91d3d04380d0e045cb9d468eba883bf064c8dd00f4686af064e2
7
- data.tar.gz: 41708695837cfdc20e091889d88bb1d4e1d8a8c564855f5dbd75f640fc773ffd080353c30f073e4bbb8f76ff5e2ebefccc99a7272d55992358e22f4c8e9984ed
6
+ metadata.gz: 5fd523b6544be615a5a1112deca758af2cc5f6053f03e48c4382acecd413f32db9ed440d572171be342fd64141c0f34debd18f7bd6ebc615cf125029e86ce3f4
7
+ data.tar.gz: 047f68b8746aa20cda1bff2e9c09472fee21f70d4f36cee1599576b71f6243b5107e45f632eedf2ab2e953e9df00867dda4b9a94ec13f896f6d38a136cda9ed9
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -20,100 +20,15 @@ class MindWords
20
20
  using ColouredText
21
21
  using HashCopy
22
22
 
23
+ attr_accessor :lines
23
24
 
24
25
  def initialize(raws, parent: nil, debug: false)
25
26
 
26
- @debug = debug
27
+ @parent, @debug = parent, debug
27
28
 
28
29
  s, _ = RXFHelper.read raws
29
- @a = s.strip.gsub(/^\n/,'').lines
30
- @a.shift if @a.first =~ /<\?mindwords\?>/
31
-
32
- lines = @a.map do |line|
33
-
34
- word = line.split(/ (?=#)/,2)
35
-
36
- end
37
-
38
- h = {}
39
- @hashtags = {}
40
-
41
- lines.each do |title, rawtags|
42
-
43
- rawtags.scan(/#(\w+)/).flatten(1).each do |rawtag|
44
- tag = rawtag.gsub(/ +/, '_')
45
- h[tag] ||= []
46
- h[tag] << title
47
- end
48
-
49
- end
50
-
51
- @hashtags = h.deep_clone.sort.map {|tag, fields| [tag, fields.sort]}.to_h
52
-
53
-
54
- a = rexlize(h)
55
- doc = Rexle.new(['root', {}, '', *a])
56
-
57
- # apply node nesting
58
-
59
- doc.root.elements.each do |e|
60
-
61
- doc.root.xpath('//' + e.name).each do |e2|
62
-
63
- next if e2 === e
64
-
65
- e2.parent.add e
66
- e2.delete
67
-
68
- end
69
-
70
- end
71
-
72
-
73
- # remove duplicates which appear in the same branch above the nested node
74
- rm_duplicates(doc)
75
-
76
- # remove redundant nodes (outsiders)
77
- # a redundant node is where all children exist in existing nested nodes
78
-
79
- redundants = doc.root.elements.map do |e|
80
-
81
- r = e.elements.all? {|x| !x.has_elements?}
82
- puts "%s %s" % [e.name, r] if @debug
83
- dups = e.elements.all? {|x| doc.root.xpath('//' + x.name).length > 1}
84
- puts 'dups: ' + dups.inspect if @debug
85
- e.backtrack.to_s if dups
86
-
87
- end
88
-
89
- redundants.compact.each {|x| doc.element(x).delete }
90
-
91
-
92
- node = if parent then
93
- found = doc.root.element('//' + parent)
94
- found ? found : doc.root
95
- else
96
- doc.root
97
- end
98
-
99
- @outline = treeize node
100
-
101
- node.root.each_recursive do |e|
102
-
103
- e.attributes[:id] = e.attributes[:title].downcase.gsub(/ +/,'-')
104
-
105
- s = e.parent.attributes[:breadcrumb] ? \
106
- e.parent.attributes[:breadcrumb].to_s + ' / ' : ''
107
- e.attributes[:breadcrumb] = s + e.value.strip
108
-
109
- r = @a.grep(/^#{e.attributes[:title]} #/i)
110
- next unless r.any?
111
- e.attributes[:hashtags] = r[0].scan(/(?<=#)\w+/).join(' ')
112
-
113
-
114
- end
115
-
116
- @xml = node.xml pretty: true
30
+ @lines = s.strip.gsub(/^\n/,'').lines
31
+ @lines.shift if @lines.first =~ /<\?mindwords\?>/
117
32
 
118
33
  end
119
34
 
@@ -123,11 +38,12 @@ class MindWords
123
38
  e = doc.root.element("//*[@id='#{id}']")
124
39
  #e.attributes[:breadcrumb].to_s if e
125
40
 
126
- end
41
+ end
42
+
127
43
 
128
44
  def search(keyword)
129
45
 
130
- a = @a.grep(/#{keyword}/i).map do |line|
46
+ a = @lines.grep(/#{keyword}/i).map do |line|
131
47
 
132
48
  puts 'line: ' + line.inspect if @debug
133
49
 
@@ -149,7 +65,7 @@ class MindWords
149
65
  end
150
66
 
151
67
  def sort()
152
- s = @a.sort.join
68
+ s = @lines.sort.join
153
69
 
154
70
  def s.to_s()
155
71
  self.lines.map do |x|
@@ -162,13 +78,13 @@ class MindWords
162
78
  end
163
79
 
164
80
  def sort!()
165
- @a = sort().lines
81
+ @lines = sort().lines
166
82
  self
167
83
  end
168
84
 
169
85
  def tag_sort()
170
86
 
171
- h = @a.group_by {|x| x[/#\w+/]}
87
+ h = @lines.group_by {|x| x[/#\w+/]}
172
88
  s = h.sort.map {|key, value| value.sort }.join
173
89
 
174
90
  def s.to_s()
@@ -183,7 +99,7 @@ class MindWords
183
99
  end
184
100
 
185
101
  def tag_sort!()
186
- @a = tag_sort().lines
102
+ @lines = tag_sort().lines
187
103
  self
188
104
  end
189
105
 
@@ -196,15 +112,16 @@ class MindWords
196
112
  end
197
113
 
198
114
  def to_outline(sort: true)
115
+ build()
199
116
  sort ? a2tree(tree_sort(LineTree.new(@outline).to_a)) : @outline
200
117
  end
201
118
 
202
119
  def to_s(colour: false)
203
120
 
204
121
  header = "<?mindwords?>\n\n"
205
- return header + @a.join unless colour
122
+ return header + @lines.join unless colour
206
123
 
207
- body = @a.map do |x|
124
+ body = @lines.map do |x|
208
125
  title, hashtags = x.split(/(?=#)/,2)
209
126
  title + hashtags.chomp.brown
210
127
  end.join("\n")
@@ -223,6 +140,91 @@ class MindWords
223
140
 
224
141
  private
225
142
 
143
+ def build()
144
+
145
+ h = {}
146
+
147
+ @lines.each do |line|
148
+
149
+ title, rawtags = line.split(/ (?=#)/,2)
150
+
151
+ rawtags.scan(/#(\w+)/).flatten(1).each do |rawtag|
152
+ tag = rawtag.gsub(/ +/, '_')
153
+ h[tag] ||= []
154
+ h[tag] << title
155
+ end
156
+
157
+ end
158
+
159
+ @hashtags = h.deep_clone.sort.map {|tag, fields| [tag, fields.sort]}.to_h
160
+
161
+
162
+ a = rexlize(h)
163
+ doc = Rexle.new(['root', {}, '', *a])
164
+
165
+ # apply node nesting
166
+
167
+ doc.root.elements.each do |e|
168
+
169
+ doc.root.xpath('//' + e.name).each do |e2|
170
+
171
+ next if e2 === e
172
+
173
+ e2.parent.add e
174
+ e2.delete
175
+
176
+ end
177
+
178
+ end
179
+
180
+
181
+ # remove duplicates which appear in the same branch above the nested node
182
+ rm_duplicates(doc)
183
+
184
+ # remove redundant nodes (outsiders)
185
+ # a redundant node is where all children exist in existing nested nodes
186
+
187
+ redundants = doc.root.elements.map do |e|
188
+
189
+ r = e.elements.all? {|x| !x.has_elements?}
190
+ puts "%s %s" % [e.name, r] if @debug
191
+ dups = e.elements.all? {|x| doc.root.xpath('//' + x.name).length > 1}
192
+ puts 'dups: ' + dups.inspect if @debug
193
+ e.backtrack.to_s if dups
194
+
195
+ end
196
+
197
+ redundants.compact.each {|x| doc.element(x).delete }
198
+
199
+
200
+ node = if @parent then
201
+ found = doc.root.element('//' + @parent)
202
+ found ? found : doc.root
203
+ else
204
+ doc.root
205
+ end
206
+
207
+ @outline = treeize node
208
+
209
+ node.root.each_recursive do |e|
210
+
211
+ e.attributes[:id] = e.attributes[:title].downcase.gsub(/ +/,'-')
212
+
213
+ s = e.parent.attributes[:breadcrumb] ? \
214
+ e.parent.attributes[:breadcrumb].to_s + ' / ' : ''
215
+ e.attributes[:breadcrumb] = s + e.value.strip
216
+
217
+ r = @lines.grep(/^#{e.attributes[:title]} #/i)
218
+ next unless r.any?
219
+ e.attributes[:hashtags] = r[0].scan(/(?<=#)\w+/).join(' ')
220
+
221
+
222
+ end
223
+
224
+ @xml = node.xml pretty: true
225
+
226
+ end
227
+
226
228
  def rexlize(a)
227
229
 
228
230
  a.map do |x|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mindwords
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file