markdown_exec 1.3.3.4 → 1.3.3.5

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: 9383de6fcdd1227b77dd3caf2bddd342eea49b72e3f0514c64a8e90d19b2e1c5
4
- data.tar.gz: 4d0eb1fc098c563edfc2a22b513b1ec78b98f6688926d64aeb4e836b3a83600e
3
+ metadata.gz: 6c3c3615fdab637a5df4f5616d9c07f277e499f1f8de3a5fa70dda72ae2cb0b3
4
+ data.tar.gz: b7cb1a2abfeaf114f25aac530d316aa181ca1464af5426e5b1f0b5aaa3538320
5
5
  SHA512:
6
- metadata.gz: 61977fc5f978b3f1c3fe518efc286a0f5d16f03aa920e941ca86dd142232ff8af1e866caba5f6abdc026f9c841b0920e123e4fa1731959c71101dcb5c288af49
7
- data.tar.gz: 966dffec9b5154da803f9b1207fcb4a42bd25132121d1304129753dd2192f866f3e76a39a10c3a187861097a8986f3bf6940740e18a633fe4065e31ccc0dd405
6
+ metadata.gz: 2bf8576d03115f6d769e961091d5a33bfc65fafdca8f2f282895230836670fb741a34a94beac49dc1008faa6ea293b3f882721b56f69fcbb697e4c011143ab7a
7
+ data.tar.gz: a53fecba24f39b4e5234164e4eada14a95b8c078111d50572c9094f5171671f3d79efc20065caaf67a1b7bce2f774c9cb52bb5c1bbbf8855678d317a3561bbef
data/CHANGELOG.md CHANGED
@@ -106,6 +106,165 @@
106
106
 
107
107
  - [! ] option to use most recent named block if dupiclate, currently appends same-name blocks but lists twice in menu
108
108
 
109
+ - [! ] improve error when imported file is not found
110
+
111
+ - [ ] decorations for import block
112
+ prefix line(s)
113
+ inline replacements
114
+ suffix line(s)
115
+
116
+ ```ruby
117
+ ##
118
+ # Replace substrings in an input string based on a regular expression pattern
119
+ # with named capture groups. The replacements are formatted using a provided
120
+ # format string. Additional context can be provided to supplement or override
121
+ # the named captures in the format string.
122
+ #
123
+ # @param input_str [String] The input string to process.
124
+ # @param regex [Regexp] The regular expression pattern with named capture groups.
125
+ # @param format_str [String] The format string for sprintf.
126
+ # @param context [Hash] Additional context to supplement or override named captures.
127
+ #
128
+ # @return [String] The processed string after replacements.
129
+ #
130
+ # def gsub_format(input_str, regex, format_str, context: {})
131
+ # input_str.gsub(regex) do
132
+ # captures_hash = $~.names.each_with_object({}) do |name, hash|
133
+ # hash[name.to_sym] = $~[name]
134
+ # end
135
+
136
+ # ### add import file name, line number, line, to captures_hash, chain
137
+ # # $~ (MatchData) - MatchData object created from the match; thread-local and frame-local. - English - $LAST_MATCH_INFO.
138
+ # # $& (Matched Substring) - The matched string. - English - $MATCH.
139
+ # # $` (Pre-Match Substring) - The string to the left of the match. - English - $PREMATCH.
140
+ # # $' (Post-Match Substring) - The string to the right of the match. - English - $POSTMATCH.
141
+ # # $+ (Last Matched Group) - The last group matched. - English - $LAST_PAREN_MATCH.
142
+
143
+ # sprintf(format_str, context.merge(captures_hash))
144
+ # end
145
+ # end
146
+
147
+ class Regexp
148
+ def gsub_format(input_str, format_str, context: {})
149
+ input_str.gsub(self) do
150
+ captures_hash = $~.names.each_with_object({}) do |name, hash|
151
+ hash[name.to_sym] = $~[name]
152
+ end
153
+
154
+ # ### add import file name, line number, line, to captures_hash, chain
155
+ # # $~ (MatchData) - MatchData object created from the match; thread-local and frame-local. - English - $LAST_MATCH_INFO.
156
+ # # $& (Matched Substring) - The matched string. - English - $MATCH.
157
+ # # $` (Pre-Match Substring) - The string to the left of the match. - English - $PREMATCH.
158
+ # # $' (Post-Match Substring) - The string to the right of the match. - English - $POSTMATCH.
159
+ # # $+ (Last Matched Group) - The last group matched. - English - $LAST_PAREN_MATCH.
160
+
161
+ # # Add file name, line number, line to captures_hash
162
+ # captures_hash[:file_name] = $~.pre_match.split("\n").last
163
+ # captures_hash[:line_number] = $~.pre_match.count("\n") + 1
164
+ # captures_hash[:line] = $&
165
+
166
+ sprintf(format_str, context.merge(captures_hash))
167
+ end
168
+ end
169
+ end
170
+
171
+ # # Example usage:
172
+ # str = "123 example"
173
+ # re = /(?<foo>\d+) (?<bar>\w+)/
174
+ # fmt = "%<foo>d : %<bar>s"
175
+ # new_str = gsub_format(str, re, fmt)
176
+ # puts new_str # Outputs: 123 : example
177
+
178
+ require 'minitest/autorun'
179
+ require_relative 'path_to_your_file' # Make sure to replace this with the path to the file containing the function
180
+
181
+ class ReplaceWithFormatTest < Minitest::Test
182
+ def test_basic_replacement
183
+ input_str = "123 example"
184
+ re = /(?<foo>\d+) (?<bar>\w+)/
185
+ fmt = "%<foo>d : %<bar>s"
186
+
187
+ result = gsub_format(input_str, re, fmt)
188
+
189
+ assert_equal "123 : example", result
190
+ end
191
+
192
+ def test_no_match
193
+ input_str = "This is a test."
194
+ re = /(?<foo>\d+) (?<bar>\w+)/
195
+ fmt = "%<foo>d : %<bar>s"
196
+
197
+ result = gsub_format(input_str, re, fmt)
198
+
199
+ assert_equal "This is a test.", result
200
+ end
201
+
202
+ def test_multiple_matches
203
+ input_str = "123 example, 456 test"
204
+ re = /(?<foo>\d+) (?<bar>\w+)/
205
+ fmt = "[%<foo>d %<bar>s]"
206
+
207
+ result = gsub_format(input_str, re, fmt)
208
+
209
+ assert_equal "[123 example], [456 test]", result
210
+ end
211
+
212
+ def test_different_named_captures
213
+ input_str = "Jane is 25 years old."
214
+ re = /(?<name>\w+) is (?<age>\d+)/
215
+ fmt = "%<name>s's age is %<age>d"
216
+
217
+ result = gsub_format(input_str, re, fmt)
218
+
219
+ assert_equal "Jane's age is 25", result
220
+ end
221
+
222
+ def test_with_context
223
+ input_str = "Jane is 25 years old."
224
+ re = /(?<name>\w+) is (?<age>\d+)/
225
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
226
+
227
+ result = gsub_format(input_str, re, fmt, context: { city: "New York" })
228
+
229
+ assert_equal "Jane's age is 25 and she lives in New York", result
230
+ end
231
+ end
232
+
233
+ require 'minitest/autorun'
234
+ require_relative 'path_to_your_file' # Ensure this path is correct
235
+
236
+ class RegexpGsubFormatTest < Minitest::Test
237
+ def test_basic_replacement
238
+ input_str = "123 example"
239
+ re = /(?<foo>\d+) (?<bar>\w+)/
240
+ fmt = "%<foo>d : %<bar>s"
241
+
242
+ result = re.gsub_format(input_str, fmt)
243
+
244
+ assert_equal "123 : example", result
245
+ end
246
+
247
+ # ... [other tests remain mostly unchanged, just updating the method call]
248
+
249
+ def test_with_context
250
+ input_str = "Jane is 25 years old."
251
+ re = /(?<name>\w+) is (?<age>\d+)/
252
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
253
+
254
+ result = re.gsub_format(input_str, fmt, context: { city: "New York" })
255
+
256
+ assert_equal "Jane's age is 25 and she lives in New York", result
257
+ end
258
+ end
259
+
260
+ ```
261
+
262
+ ## [1.3.5] - 2023-10-05
263
+
264
+ ### Changed
265
+
266
+ - Fix display of menu dividers.
267
+
109
268
  ## [1.3.3] - 2023-10-03
110
269
 
111
270
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_exec (1.3.3.4)
4
+ markdown_exec (1.3.3.5)
5
5
  clipboard (~> 1.3.6)
6
6
  open3 (~> 0.1.1)
7
7
  optparse (~> 0.1.1)
@@ -13,7 +13,7 @@ __filedirs_all()
13
13
  }
14
14
 
15
15
  _mde_echo_version() {
16
- echo "1.3.3.4"
16
+ echo "1.3.3.5"
17
17
  }
18
18
 
19
19
  _mde() {
@@ -138,4 +138,4 @@ _mde() {
138
138
 
139
139
  complete -o filenames -o nospace -F _mde mde
140
140
  # _mde_echo_version
141
- # echo "Updated: 2023-10-04 03:33:56 UTC"
141
+ # echo "Updated: 2023-10-05 14:21:31 UTC"
@@ -7,5 +7,5 @@ module MarkdownExec
7
7
  BIN_NAME = 'mde'
8
8
  GEM_NAME = 'markdown_exec'
9
9
  TAP_DEBUG = 'MDE_DEBUG'
10
- VERSION = '1.3.3.4'
10
+ VERSION = '1.3.3.5'
11
11
  end
data/lib/markdown_exec.rb CHANGED
@@ -260,7 +260,6 @@ module MarkdownExec
260
260
  else
261
261
  true
262
262
  end.tap_inspect
263
- # binding.pry
264
263
  rescue StandardError => err
265
264
  warn("ERROR ** Filter::fcb_select?(); #{err.inspect}")
266
265
  raise err
@@ -358,7 +357,7 @@ module MarkdownExec
358
357
  selrows = @table.select do |fcb_title_groups|
359
358
  Filter.fcb_select? options, fcb_title_groups
360
359
  end
361
- # binding.pry
360
+
362
361
  ### hide rows correctly
363
362
 
364
363
  if opts[:hide_blocks_by_name]
@@ -663,10 +662,7 @@ module MarkdownExec
663
662
  end
664
663
 
665
664
  def cfile
666
- # puts @options.inspect
667
- # binding.pry
668
665
  @cfile ||= CachedNestedFileReader.new(import_pattern: @options.fetch(:import_pattern))
669
- # @cfile ||= CachedNestedFileReader.new(import_pattern: /^ *#insert (.+)$/)
670
666
  end
671
667
 
672
668
  # :reek:DuplicateMethodCall
@@ -945,7 +941,7 @@ module MarkdownExec
945
941
  blocks.push FCB.new({
946
942
  # name: '',
947
943
  chrome: true,
948
- text: format(
944
+ name: format(
949
945
  opts[:menu_divider_format],
950
946
  opts[:menu_initial_divider]
951
947
  ).send(opts[:menu_divider_color].to_sym),
@@ -954,7 +950,6 @@ module MarkdownExec
954
950
  end
955
951
 
956
952
  iter_blocks_in_file(opts) do |btype, fcb|
957
- # binding.pry
958
953
  case btype
959
954
  when :filter
960
955
  ## return type of blocks to select
@@ -964,14 +959,12 @@ module MarkdownExec
964
959
  when :line
965
960
  ## convert line to block
966
961
  #
967
- # binding.pry
968
962
  if opts[:menu_divider_match].present? &&
969
963
  (mbody = fcb.body[0].match opts[:menu_divider_match])
970
- # binding.pry
971
964
  blocks.push FCB.new(
972
965
  { chrome: true,
973
966
  disabled: '',
974
- text: format(opts[:menu_divider_format],
967
+ name: format(opts[:menu_divider_format],
975
968
  mbody[:name]).send(opts[:menu_divider_color].to_sym) }
976
969
  )
977
970
  elsif opts[:menu_task_match].present? &&
@@ -979,7 +972,7 @@ module MarkdownExec
979
972
  blocks.push FCB.new(
980
973
  { chrome: true,
981
974
  disabled: '',
982
- text: format(opts[:menu_task_format],
975
+ name: format(opts[:menu_task_format],
983
976
  mbody[:name]).send(opts[:menu_task_color].to_sym) }
984
977
  )
985
978
  else
@@ -996,7 +989,7 @@ module MarkdownExec
996
989
  blocks.push FCB.new(
997
990
  { chrome: true,
998
991
  disabled: '',
999
- text: format(opts[:menu_divider_format],
992
+ name: format(opts[:menu_divider_format],
1000
993
  opts[:menu_final_divider])
1001
994
  .send(opts[:menu_divider_color].to_sym) }
1002
995
  )
@@ -1278,18 +1271,24 @@ module MarkdownExec
1278
1271
  }
1279
1272
  end
1280
1273
 
1274
+ ## insert exit option at head or tail
1275
+ #
1276
+ def prompt_menu_add_exit(_prompt_text, items, exit_option, _opts = {})
1277
+ if @options[:menu_exit_at_top]
1278
+ (@options[:menu_with_exit] ? [exit_option] : []) + items
1279
+ else
1280
+ items + (@options[:menu_with_exit] ? [exit_option] : [])
1281
+ end
1282
+ end
1283
+
1281
1284
  ## tty prompt to select
1282
1285
  # insert exit option at head or tail
1283
1286
  # return selected option or nil
1284
1287
  #
1285
1288
  def prompt_with_quit(prompt_text, items, opts = {})
1286
1289
  exit_option = '* Exit'
1287
- all_items = if @options[:menu_exit_at_top]
1288
- (@options[:menu_with_exit] ? [exit_option] : []) + items
1289
- else
1290
- items + (@options[:menu_with_exit] ? [exit_option] : [])
1291
- end
1292
- sel = @prompt.select(prompt_text, all_items, opts.merge(filter: true))
1290
+ sel = @prompt.select(prompt_text, prompt_menu_add_exit(prompt_text, items, exit_option, opts),
1291
+ opts.merge(filter: true))
1293
1292
  sel == exit_option ? nil : sel
1294
1293
  end
1295
1294
 
@@ -1421,8 +1420,6 @@ module MarkdownExec
1421
1420
  end.compact
1422
1421
  return nil if bm.count.zero?
1423
1422
 
1424
- # binding.pry
1425
-
1426
1423
  sel = prompt_with_quit pt, bm,
1427
1424
  per_page: opts[:select_page_height]
1428
1425
  return nil if sel.nil?
data/lib/menu.yml CHANGED
@@ -1,4 +1,4 @@
1
- # MDE - Markdown Executor (1.3.3.4)
1
+ # MDE - Markdown Executor (1.3.3.5)
2
2
  ---
3
3
  - :arg_name: NAME
4
4
  :compreply: false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3.4
4
+ version: 1.3.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fareed Stevenson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-04 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clipboard