mindwords 0.5.0 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 318da7a9e4daf9a4f4d69d4315d062fab0f4b2876c3a0302a86d4d1b40909a32
4
- data.tar.gz: cd0336cf0a70c0e90fec5964f5a987cbad0abb9e295fc96ddf54a6999832af91
3
+ metadata.gz: 3dce3985e118c20bf9564d5d5d80b471cfb1422387d02a9b2faedaa29ee6b847
4
+ data.tar.gz: c74d01bae140212eb3e3d8f6a65fed89a1fde92920fd4924f4f32b15cc8fd901
5
5
  SHA512:
6
- metadata.gz: 1eb530ac7ff6db8b68e2827c51a7ad4ed0ed7244340e842b6ff967c2da1f32a777fff8fc2ddc91d3d04380d0e045cb9d468eba883bf064c8dd00f4686af064e2
7
- data.tar.gz: 41708695837cfdc20e091889d88bb1d4e1d8a8c564855f5dbd75f640fc773ffd080353c30f073e4bbb8f76ff5e2ebefccc99a7272d55992358e22f4c8e9984ed
6
+ metadata.gz: 7867fb1ac1224ec7d5f4515b450182d01344fe95cd4002409fe45eeb59a6cedaa7ba07074c1f22030d332fe3a09e4290c1aa0243e5033575898b098328aa160c
7
+ data.tar.gz: 0c10f58552946d28c0a81fca5e1ce078575ec8c7402e2b64bc8c56e1b24c0979053356898af377f036d7bdabfd9e4957bc87e970eded50a7ee3d94703ab75b88
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -20,114 +20,53 @@ class MindWords
20
20
  using ColouredText
21
21
  using HashCopy
22
22
 
23
+ attr_accessor :lines, :filepath
23
24
 
24
- def initialize(raws, parent: nil, debug: false)
25
+ def initialize(raws='', parent: nil, debug: false)
25
26
 
26
- @debug = debug
27
-
28
- 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
27
+ @parent, @debug = parent, debug
64
28
 
65
- e2.parent.add e
66
- e2.delete
67
-
68
- end
69
-
70
- end
71
-
29
+ s, type = RXFHelper.read raws
72
30
 
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
31
+ @filepath = raws if type == :file or type == :dfs
32
+ @lines = s.strip.gsub(/^\n/,'').lines
33
+ @lines.shift if @lines.first =~ /<\?mindwords\?>/
117
34
 
118
35
  end
119
36
 
37
+ def add(line)
38
+ @lines << line
39
+ end
40
+
41
+ def breadcrumb()
42
+ @parent.attributes[:breadcrumb].split(/ +\/ +/) if @parent
43
+ end
44
+
45
+
120
46
  def element(id)
121
47
 
122
- doc = Rexle.new(@xml)
48
+ doc = Rexle.new(to_xml())
123
49
  e = doc.root.element("//*[@id='#{id}']")
124
50
  #e.attributes[:breadcrumb].to_s if e
125
51
 
126
- end
52
+ end
53
+
54
+ def hashtags()
55
+ @parent.attributes[:hashtags].split if @parent
56
+ end
57
+
58
+ def save(file=@filepath)
59
+
60
+ return if @lines.empty?
61
+
62
+ puts 'before save' if @debug
63
+ File.write file, to_s()
64
+
65
+ end
127
66
 
128
67
  def search(keyword)
129
68
 
130
- a = @a.grep(/#{keyword}/i).map do |line|
69
+ a = @lines.grep(/#{keyword}/i).map do |line|
131
70
 
132
71
  puts 'line: ' + line.inspect if @debug
133
72
 
@@ -144,12 +83,16 @@ class MindWords
144
83
 
145
84
  a2 = a.sort_by(&:last).map(&:first)
146
85
  puts 'a2: ' + a2.inspect if @debug
147
- MindWords.new(a2.join, parent: keyword, debug: @debug)
86
+ e = element(keyword.downcase.gsub(/ +/,'-'))
87
+
88
+ return nil if e.nil?
89
+
90
+ MindWords.new(a2.join, parent: e, debug: @debug)
148
91
 
149
92
  end
150
93
 
151
94
  def sort()
152
- s = @a.sort.join
95
+ s = @lines.sort.join
153
96
 
154
97
  def s.to_s()
155
98
  self.lines.map do |x|
@@ -162,13 +105,13 @@ class MindWords
162
105
  end
163
106
 
164
107
  def sort!()
165
- @a = sort().lines
108
+ @lines = sort().lines
166
109
  self
167
110
  end
168
111
 
169
112
  def tag_sort()
170
113
 
171
- h = @a.group_by {|x| x[/#\w+/]}
114
+ h = @lines.group_by {|x| x[/#\w+/]}
172
115
  s = h.sort.map {|key, value| value.sort }.join
173
116
 
174
117
  def s.to_s()
@@ -183,46 +126,142 @@ class MindWords
183
126
  end
184
127
 
185
128
  def tag_sort!()
186
- @a = tag_sort().lines
129
+ @lines = tag_sort().lines
187
130
  self
188
131
  end
189
-
190
- def to_h()
191
- @h
192
- end
193
-
132
+
194
133
  def to_hashtags()
195
134
  @hashtags
196
135
  end
197
136
 
198
137
  def to_outline(sort: true)
138
+ build()
199
139
  sort ? a2tree(tree_sort(LineTree.new(@outline).to_a)) : @outline
200
140
  end
201
141
 
202
142
  def to_s(colour: false)
203
143
 
204
144
  header = "<?mindwords?>\n\n"
205
- return header + @a.join unless colour
145
+ return header + @lines.map(&:chomp).join("\n") unless colour
206
146
 
207
- body = @a.map do |x|
147
+ body = @lines.map do |x|
208
148
  title, hashtags = x.split(/(?=#)/,2)
209
149
  title + hashtags.chomp.brown
210
150
  end.join("\n")
211
151
 
212
152
  header + body
213
153
 
214
- end
154
+ end
215
155
 
216
156
  def to_words()
217
- to_outline.lines.map {|x| x[/\w[\w ]+/] }.uniq
218
- end
157
+
158
+ h = {}
159
+
160
+ Rexle.new(to_xml).root.each_recursive do |e|
161
+
162
+ h[e.attributes[:title]] = {
163
+ breadcrumb: e.attributes[:breadcrumb],
164
+ hashtags: e.attributes[:hashtags]
165
+ }
166
+
167
+ end
168
+
169
+ h
170
+
171
+ end
219
172
 
220
173
  def to_xml()
174
+ build() unless @xml
221
175
  @xml
222
176
  end
223
177
 
224
178
  private
225
179
 
180
+ def build()
181
+
182
+ h = {}
183
+
184
+ @lines.each do |line|
185
+
186
+ title, rawtags = line.split(/ (?=#)/,2)
187
+
188
+ rawtags.scan(/#(\w+)/).flatten(1).each do |rawtag|
189
+ tag = rawtag.gsub(/ +/, '_')
190
+ h[tag] ||= []
191
+ h[tag] << title
192
+ end
193
+
194
+ end
195
+
196
+ @hashtags = h.deep_clone.sort.map {|tag, fields| [tag, fields.sort]}.to_h
197
+
198
+
199
+ a = rexlize(h)
200
+ doc = Rexle.new(['root', {}, '', *a])
201
+
202
+ # apply node nesting
203
+
204
+ doc.root.elements.each do |e|
205
+
206
+ doc.root.xpath('//' + e.name).each do |e2|
207
+
208
+ next if e2 === e
209
+
210
+ e2.parent.add e
211
+ e2.delete
212
+
213
+ end
214
+
215
+ end
216
+
217
+
218
+ # remove duplicates which appear in the same branch above the nested node
219
+ rm_duplicates(doc)
220
+
221
+ # remove redundant nodes (outsiders)
222
+ # a redundant node is where all children exist in existing nested nodes
223
+
224
+ redundants = doc.root.elements.map do |e|
225
+
226
+ r = e.elements.all? {|x| !x.has_elements?}
227
+ puts "%s %s" % [e.name, r] if @debug
228
+ dups = e.elements.all? {|x| doc.root.xpath('//' + x.name).length > 1}
229
+ puts 'dups: ' + dups.inspect if @debug
230
+ e.backtrack.to_s if dups
231
+
232
+ end
233
+
234
+ redundants.compact.each {|x| doc.element(x).delete }
235
+
236
+
237
+ node = if @parent then
238
+ found = doc.root.element('//' + @parent.name)
239
+ found ? found : doc.root
240
+ else
241
+ doc.root
242
+ end
243
+
244
+ @outline = treeize node
245
+
246
+ node.root.each_recursive do |e|
247
+
248
+ e.attributes[:id] = e.attributes[:title].downcase.gsub(/ +/,'-')
249
+
250
+ s = e.parent.attributes[:breadcrumb] ? \
251
+ e.parent.attributes[:breadcrumb].to_s + ' / ' : ''
252
+ e.attributes[:breadcrumb] = s + e.value.strip
253
+
254
+ r = @lines.grep(/^#{e.attributes[:title]} #/i)
255
+ next unless r.any?
256
+ e.attributes[:hashtags] = r[0].scan(/(?<=#)\w+/).join(' ')
257
+
258
+
259
+ end
260
+
261
+ @xml = node.xml pretty: true
262
+
263
+ end
264
+
226
265
  def rexlize(a)
227
266
 
228
267
  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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  aWH7D2AmhOpqNwWnPHzWR/yzpigAVTrvpHfRxZleQj6Z/090nIH2KR0RdioMmPFq
36
36
  3+574KQzs/gR9Y5a+iMcvHRN
37
37
  -----END CERTIFICATE-----
38
- date: 2021-01-25 00:00:00.000000000 Z
38
+ date: 2021-01-31 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: line-tree
metadata.gz.sig CHANGED
Binary file