md_edit 0.1.9 → 0.1.10

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
  SHA1:
3
- metadata.gz: 546abacabfcd21d682abb75886ec1d7c3b51f7d7
4
- data.tar.gz: 4c7e4d6c504182140095af692cba1fded5f8da82
3
+ metadata.gz: 47045b90019ff71705e40bb300f6a14acc4c4ec0
4
+ data.tar.gz: ba10aac7d1d29fa69a7b589a13d7e464c35c6cc3
5
5
  SHA512:
6
- metadata.gz: f3f7c3d5cd4a7c529784d253ba1c69f08266d38cfb1f3eaaa16112e5f7f93227d004e94911e56b165d85a18e926b417a885d5decff3b73a4e9477a4b3bf7a276
7
- data.tar.gz: 2cdb3fbc4f67e924bae4552178fbf911c7d897aee9ed2dfd0d4537a8dccaec6704140e4db139afb0d922806985d9707dba3a861563584333ab9e97510663124d
6
+ metadata.gz: 0d13ae45845fd8f982284abb6078414d4d92ce269c64b072dbdd4788a574ef6aa82207032bc82ff60c1b2096a1a71c4ebef537783ad25d66c5648a0686c18490
7
+ data.tar.gz: 96be01978e1caeae144d73d9bb021bf6f062d46d8093934932ea43ce1421102002189fff4a42d91fbe56d1f3b4c506820ded6625c7347d123aec6fcbc5a08177
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/md_edit.rb CHANGED
@@ -13,13 +13,13 @@ class MdEdit
13
13
 
14
14
  # pass in a Markdown document or a Markdown filename
15
15
  #
16
- def initialize(md, debug: false)
16
+ def initialize(md, debug: false, root: 'Thoughts')
17
17
 
18
18
  @debug = debug
19
19
 
20
20
  s, @filename = if md.lines.length == 1 then
21
21
 
22
- File.write(md, "# Thoughts\n\n") unless File.exists? md
22
+ File.write(md, "# #{root}\n\n") unless File.exists? md
23
23
  File.exists?(md) ? [File.read(md), md] : md
24
24
 
25
25
  else
@@ -48,7 +48,8 @@ class MdEdit
48
48
 
49
49
  key = @sections.keys.grep(/#{s.downcase}/i).first
50
50
  old_value = @sections[key].flatten.join
51
- old_section = key + old_value
51
+ heading = last_heading(key)
52
+ old_section = heading + old_value
52
53
 
53
54
  @s.sub!(old_section, '')
54
55
  load_sections(@s)
@@ -63,13 +64,17 @@ class MdEdit
63
64
 
64
65
  value = raw_value.gsub(/\r/,'')
65
66
 
66
- title = heading ? heading : value.lines.first.chomp
67
+ title = (heading ? heading : value.lines.first.chomp)[/#+ +(.*)/,1]
68
+
67
69
  key = @sections.keys.grep(/#{title.downcase}/i).first
70
+
68
71
  return unless key
69
72
 
70
73
  old_value = @sections[key].flatten.join
71
74
  puts 'old_value: ' + old_value.inspect if @debug
72
- old_section = value =~ /^#+/ ? key + old_value : old_value
75
+
76
+ heading = last_heading(key)
77
+ old_section = value =~ /^#+/ ? heading + old_value : old_value
73
78
  puts 'old_section: ' + old_section.inspect if @debug
74
79
 
75
80
  @s.sub!(old_section, value)
@@ -85,9 +90,14 @@ class MdEdit
85
90
  alias edit update
86
91
 
87
92
  def find(s, heading: true)
88
- key = @sections.keys.reverse.grep(/#{s.downcase}/i).first
93
+
94
+ key = @sections.keys.grep(/#{s.downcase}/i).first
89
95
  return unless key
90
- a = [key, @sections[key].join]
96
+
97
+ headings = key.lines.first.split(/ > /)
98
+ title = "%s %s" % ['#' * headings.length, headings.last]
99
+ a = [title, @sections[key].join]
100
+
91
101
  heading ? a.join : a
92
102
  end
93
103
 
@@ -100,6 +110,14 @@ class MdEdit
100
110
  def to_h()
101
111
  @h
102
112
  end
113
+
114
+ def to_outline(bullets: false)
115
+
116
+ a = indentor(@s.scan(/^#+ [^\n]+/).join("\n"))
117
+ .lines.map {|x| x.sub(/#+ +/,'')}
118
+ bullets ? a.map{|x| x.sub(/\b/,'- ')} : a.join
119
+
120
+ end
103
121
 
104
122
  def to_s()
105
123
  @s
@@ -107,6 +125,29 @@ class MdEdit
107
125
 
108
126
  private
109
127
 
128
+ def indentor(s)
129
+
130
+ a = s.split(/(?<=\n)(?=#+)/)
131
+
132
+ a.map.with_index do |x, i|
133
+
134
+ # get the indentation level
135
+ indent = x[/^#+/].count('#') - 1
136
+
137
+ lines = x.lines
138
+ lines.first.prepend(' ' * indent) +
139
+ lines[1..-1].map {|y| (' ' * indent) + ' ' + y}.join
140
+ end.join
141
+
142
+ end
143
+
144
+ def last_heading(key)
145
+
146
+ a = key.lines.first.split(/ > /)
147
+ "%s %s" % ['#' * a.length, a.last]
148
+
149
+ end
150
+
110
151
  def load_sections(raw_s)
111
152
 
112
153
  # strip out any new lines gaps which are greater than 1
@@ -123,25 +164,14 @@ class MdEdit
123
164
 
124
165
  end
125
166
 
126
- def parse(s)
167
+ def parse(markdown)
127
168
 
128
- a = s.split(/(?<=\n)(?=#+)/)
129
-
130
- a2 = a.map.with_index do |x, i|
131
-
132
- # get the indentation level
133
- indent = x[/^#+/].count('#') - 1
134
-
135
- lines = x.lines
136
- lines.first.prepend(' ' * indent) +
137
- lines[1..-1].map {|y| (' ' * indent) + ' ' + y}.join
138
- end
139
-
140
- puts "a2.join: \n" + a2.join if @debug
141
- a3 = LineTree.new(a2.join, ignore_blank_lines: false, ignore_newline: false).to_a
142
- puts 'a3: ' + a3.inspect if @debug
169
+ s = indentor(markdown)
170
+ puts "s: \n" + s if @debug
171
+ a = LineTree.new(s, ignore_blank_lines: false, ignore_newline: false).to_a
172
+ puts 'a: ' + a.inspect if @debug
143
173
  h = {}
144
- a4 = scan a3, h
174
+ scan a, h
145
175
 
146
176
  return h
147
177
 
@@ -151,22 +181,25 @@ class MdEdit
151
181
  File.write @filename, @s if @filename
152
182
  end
153
183
 
154
- def scan(a, h={})
184
+ def scan(a, h={}, trail=[])
155
185
 
156
186
  a.map do |x|
157
187
 
158
- head = x.first
188
+ raw_head = x.first
189
+
190
+ if raw_head =~ /^#/ then
159
191
 
160
- if head =~ /^#/ then
161
-
162
- r = scan(x[1..-1], h)
192
+ head = raw_head[/^[^\n]+/]
163
193
 
164
- h[head[/^[^\n]+/]] = ["\n"] + r
194
+ fullkey = trail + [head[/#+ +(.*)/,1]]
195
+ key = fullkey.join(" > ")
196
+ h[key] = nil
197
+ r = scan(x[1..-1], h, fullkey)
198
+
199
+ h[key] = ["\n"] + r
165
200
 
166
201
  [head, r]
167
202
 
168
- elsif x.length > 1
169
- [head] + scan(x[1..-1])
170
203
  else
171
204
  x.flatten.join
172
205
  end
@@ -175,4 +208,5 @@ class MdEdit
175
208
 
176
209
  end
177
210
 
211
+
178
212
  end
data.tar.gz.sig CHANGED
@@ -1 +1,4 @@
1
- ?4Q��lt�)�t0�u�5���}����Q��tKeIs�0_�4���45���0�� �]t3����Ķ�།÷�i�2����y�@RӃܘ�`�-�� ��M��a[i����)7���}:� ��f~���Ӂ�yfI�b��.w'�G��� SIn��lAG@jE[� hd/G��?�d�v�9�����|�ҙ�vBJ� ��-�ҙ�� �l���j��d[�$m�lo�?ĸ���P�I�e
1
+ EXG�����_w��<��T�)��kbF^���9C��΀2����"��잽�X��Щ�����ڞ#[X�섽c��zi�i�a]/X7ܹ#
2
+ ]E7�ڀ�y͖���>v�z:/&���u<x��SH��}�jdz^?�Z
3
+ I�-��/ܔ�H9$e�7�
4
+ �n�+Y���+�d/�B��X�����
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_edit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  R5r1JjTBaaHOaqI14bIkwUSY5q1cO4Wl7HWly80kX2AD8o/tPHvyF9Tx3pBvaboU
32
32
  etnSPHbQwmBvFQ==
33
33
  -----END CERTIFICATE-----
34
- date: 2017-12-24 00:00:00.000000000 Z
34
+ date: 2017-12-25 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: line-tree
metadata.gz.sig CHANGED
Binary file