markdown_exec 1.3.3.5 → 1.3.7

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.
@@ -54,22 +54,22 @@ if $PROGRAM_NAME == __FILE__
54
54
 
55
55
  class TestStringMethods < Minitest::Test
56
56
  def test_blank
57
- assert "".blank?
58
- assert " ".blank?
57
+ assert ''.blank?
58
+ assert ' '.blank?
59
59
  assert "\t\n\r".blank?
60
- refute "foo".blank?
60
+ refute 'foo'.blank?
61
61
  end
62
62
 
63
63
  def test_present
64
- assert "foo".present?
65
- refute "".present?
64
+ assert 'foo'.present?
65
+ refute ''.present?
66
66
  end
67
67
  end
68
68
 
69
69
  class TestObjectMethods < Minitest::Test
70
70
  def test_present
71
- assert "foo".present?
72
- refute "".present?
71
+ assert 'foo'.present?
72
+ refute ''.present?
73
73
  assert Object.new.present?
74
74
  assert 123.present?
75
75
  assert true.present?
@@ -77,4 +77,4 @@ if $PROGRAM_NAME == __FILE__
77
77
  refute nil.present?
78
78
  end
79
79
  end
80
- end
80
+ end
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+
6
+ module MarkdownExec
7
+ # OptionValue
8
+ #
9
+ # This class provides utilities to format option values for different contexts.
10
+ # The `for_hash` method prepares the value to be used as a default in `env_str()`.
11
+ # The `for_yaml` method prepares the value for output as a default in `list_default_yaml()`.
12
+ #
13
+ class OptionValue
14
+ # Formats the value for use in a hash.
15
+ def self.for_hash(value, default = nil)
16
+ return default if value.nil?
17
+
18
+ case value
19
+ when String, Integer
20
+ value
21
+ when TrueClass, FalseClass
22
+ value ? true : false
23
+ when ->(v) { v.respond_to?(:empty?) && v.empty? }
24
+ default
25
+ else
26
+ value.to_s
27
+ end
28
+ end
29
+
30
+ # Formats the value for output in YAML.
31
+ def self.for_yaml(value, default = nil)
32
+ return default if value.nil?
33
+
34
+ case value
35
+ when String
36
+ "'#{value}'"
37
+ when Integer
38
+ value
39
+ when TrueClass, FalseClass
40
+ value ? true : false
41
+ when ->(v) { v.respond_to?(:empty?) && v.empty? }
42
+ default
43
+ else
44
+ value.to_s
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ if $PROGRAM_NAME == __FILE__
51
+ require 'minitest/autorun'
52
+
53
+ class OptionValueTest < Minitest::Test
54
+ def test_for_hash_with_string
55
+ assert_equal 'sample', MarkdownExec::OptionValue.for_hash('sample')
56
+ end
57
+
58
+ def test_for_hash_with_integer
59
+ assert_equal 42, MarkdownExec::OptionValue.for_hash(42)
60
+ end
61
+
62
+ def test_for_hash_with_boolean
63
+ assert_equal true, MarkdownExec::OptionValue.for_hash(true)
64
+ assert_equal false, MarkdownExec::OptionValue.for_hash(false)
65
+ end
66
+
67
+ def test_for_hash_with_empty_value
68
+ assert_equal 'default', MarkdownExec::OptionValue.for_hash([], 'default')
69
+ end
70
+
71
+ def test_for_yaml_with_string
72
+ assert_equal "'sample'", MarkdownExec::OptionValue.for_yaml('sample')
73
+ end
74
+
75
+ def test_for_yaml_with_integer
76
+ assert_equal 42, MarkdownExec::OptionValue.for_yaml(42)
77
+ end
78
+
79
+ def test_for_yaml_with_boolean
80
+ assert_equal true, MarkdownExec::OptionValue.for_yaml(true)
81
+ assert_equal false, MarkdownExec::OptionValue.for_yaml(false)
82
+ end
83
+
84
+ def test_for_yaml_with_empty_value
85
+ assert_equal 'default', MarkdownExec::OptionValue.for_yaml([], 'default')
86
+ end
87
+ end
88
+ end
data/lib/regexp.rb ADDED
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Replace substrings in an input string based on a regular expression pattern
5
+ # with named capture groups. The replacements are formatted using a provided
6
+ # format string. Additional context can be provided to supplement or override
7
+ # the named captures in the format string.
8
+ #
9
+ # @param input_str [String] The input string to process.
10
+ # @param regex [Regexp] The regular expression pattern with named capture groups.
11
+ # @param format_str [String] The format string for sprintf.
12
+ # @param context [Hash] Additional context to supplement or override named captures.
13
+ #
14
+ # @return [String] The processed string after replacements.
15
+ #
16
+
17
+ # ### add import file name, line number, line, to captures_hash, chain
18
+ # # $~ (MatchData) - MatchData object created from the match; thread-local and frame-local. - English - $LAST_MATCH_INFO.
19
+ # # $& (Matched Substring) - The matched string. - English - $MATCH.
20
+ # # $` (Pre-Match Substring) - The string to the left of the match. - English - $PREMATCH.
21
+ # # $' (Post-Match Substring) - The string to the right of the match. - English - $POSTMATCH.
22
+ # # $+ (Last Matched Group) - The last group matched. - English - $LAST_PAREN_MATCH.
23
+
24
+ # # Add file name, line number, line to captures_hash
25
+ # captures_hash[:file_name] = $~.pre_match.split("\n").last
26
+ # captures_hash[:line_number] = $~.pre_match.count("\n") + 1
27
+ # captures_hash[:line] = $&
28
+
29
+ require 'English'
30
+ class Regexp
31
+ def gsub_format(input_str, format_str, context: {})
32
+ input_str.gsub(self) do
33
+ format(
34
+ format_str,
35
+ context.merge($LAST_MATCH_INFO.names.each_with_object({}) do |name, hash|
36
+ hash[name.to_sym] = $LAST_MATCH_INFO[name]
37
+ end)
38
+ )
39
+ end
40
+ end
41
+ end
42
+
43
+ if $PROGRAM_NAME == __FILE__
44
+ require 'bundler/setup'
45
+ Bundler.require(:default)
46
+
47
+ require 'minitest/autorun'
48
+
49
+ class RegexpGsubFormatTest < Minitest::Test
50
+ def test_basic_replacement
51
+ input_str = '123 example'
52
+ re = /(?<foo>\d+) (?<bar>\w+)/
53
+ fmt = '%<foo>d : %<bar>s'
54
+
55
+ result = re.gsub_format(input_str, fmt)
56
+
57
+ assert_equal '123 : example', result
58
+ end
59
+
60
+ def test_no_match
61
+ input_str = 'This is a test.'
62
+ re = /(?<foo>\d+) (?<bar>\w+)/
63
+ fmt = '%<foo>d : %<bar>s'
64
+
65
+ result = re.gsub_format(input_str, fmt)
66
+
67
+ assert_equal 'This is a test.', result
68
+ end
69
+
70
+ def test_multiple_matches
71
+ input_str = '123 example, 456 test'
72
+ re = /(?<foo>\d+) (?<bar>\w+)/
73
+ fmt = '[%<foo>d %<bar>s]'
74
+
75
+ result = re.gsub_format(input_str, fmt)
76
+
77
+ assert_equal '[123 example], [456 test]', result
78
+ end
79
+
80
+ def test_different_named_captures
81
+ input_str = 'Jane is 25 years old.'
82
+ re = /^(?<name>\w+) is (?<age>\d+).*$/
83
+ fmt = "%<name>s's age is %<age>d"
84
+
85
+ result = re.gsub_format(input_str, fmt)
86
+
87
+ assert_equal "Jane's age is 25", result
88
+ end
89
+
90
+ def test_with_context
91
+ input_str = 'Jane is 25 years old.'
92
+ re = /^(?<name>\w+) is (?<age>\d+).*$/
93
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
94
+
95
+ result = re.gsub_format(input_str, re, fmt, context: { city: 'New York' })
96
+
97
+ assert_equal "Jane's age is 25 and she lives in New York", result
98
+ end
99
+
100
+ def test_with_context
101
+ input_str = 'Jane is 25 years old.'
102
+ re = /^(?<name>\w+) is (?<age>\d+).*$/
103
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
104
+
105
+ result = re.gsub_format(input_str, fmt, context: { city: 'New York' })
106
+
107
+ assert_equal "Jane's age is 25 and she lives in New York", result
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+
6
+ module MarkdownExec
7
+ # SavedAsset
8
+ #
9
+ # This class provides utilities to format and derive asset names based on
10
+ # given parameters. The `script_name` method derives a name for a script
11
+ # based on filename, prefix, time, and blockname. Similarly, the `stdout_name`
12
+ # method derives a name for stdout redirection.
13
+ #
14
+ class SavedAsset
15
+ FNR11 = /pattern1/.freeze # TODO: Replace with actual pattern
16
+ FNR12 = 'replacement_string' # TODO: Replace with actual replacement string
17
+
18
+ # Generates a formatted script name based on the provided parameters.
19
+ def self.script_name(filename:, prefix:, time:, blockname:)
20
+ fne = filename.gsub(FNR11, FNR12)
21
+ "#{[prefix, time.strftime('%F-%H-%M-%S'), fne, ',', blockname].join('_')}.sh"
22
+ end
23
+
24
+ # Generates a formatted stdout name based on the provided parameters.
25
+ def self.stdout_name(filename:, prefix:, time:, blockname:)
26
+ "#{[prefix, time.strftime('%F-%H-%M-%S'), filename, blockname].join('_')}.out.txt"
27
+ end
28
+ end
29
+ end
30
+
31
+ if $PROGRAM_NAME == __FILE__
32
+ require 'minitest/autorun'
33
+
34
+ class SavedAssetTest < Minitest::Test
35
+ def test_script_name
36
+ filename = 'sample.txt'
37
+ prefix = 'test'
38
+ time = Time.new(2023, 1, 1, 12, 0, 0) # Sample date-time for consistency in testing
39
+ blockname = 'block1'
40
+
41
+ expected_name = 'test_2023-01-01-12-00-00_sample.txt_,_block1.sh'
42
+ assert_equal expected_name,
43
+ MarkdownExec::SavedAsset.script_name(filename: filename, prefix: prefix, time: time,
44
+ blockname: blockname)
45
+ end
46
+
47
+ def test_stdout_name
48
+ filename = 'sample.txt'
49
+ prefix = 'test'
50
+ time = Time.new(2023, 1, 1, 12, 0, 0)
51
+ blockname = 'block1'
52
+
53
+ expected_name = 'test_2023-01-01-12-00-00_sample.txt_block1.out.txt'
54
+ assert_equal expected_name,
55
+ MarkdownExec::SavedAsset.stdout_name(filename: filename, prefix: prefix, time: time,
56
+ blockname: blockname)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+
6
+ module MarkdownExec
7
+ # SavedFilesMatcher
8
+ #
9
+ # This class is responsible for matching saved files based on the given pattern.
10
+ # It can list all matching files, retrieve the most recent file, or a list of
11
+ # most recent files.
12
+ #
13
+ class SavedFilesMatcher
14
+ # Lists all files in the specified folder that match the given glob pattern
15
+ def self.list_all(folder, glob)
16
+ Dir.glob(File.join(folder, glob))
17
+ end
18
+
19
+ # Retrieves the most recent file from the specified folder that matches the given glob pattern
20
+ def self.most_recent(folder, glob, arr = nil)
21
+ arr = list_all(folder, glob) if arr.nil?
22
+ return if arr.count < 1
23
+
24
+ arr.max
25
+ end
26
+
27
+ # Retrieves a list of the most recent files (up to list_count) from the specified folder
28
+ # that match the given glob pattern
29
+ def self.most_recent_list(folder, glob, list_count, arr = nil)
30
+ arr = list_all(folder, glob) if arr.nil?
31
+ return if arr.empty?
32
+
33
+ arr.sort[-[arr.count, list_count].min..].reverse
34
+ end
35
+ end
36
+ end
37
+
38
+ if $PROGRAM_NAME == __FILE__
39
+ require 'minitest/autorun'
40
+
41
+ class SavedFilesMatcherTest < Minitest::Test
42
+ def setup
43
+ @folder = 'fixtures'
44
+ @glob = '*.md'
45
+ end
46
+
47
+ def test_list_all
48
+ assert_kind_of Array, MarkdownExec::SavedFilesMatcher.list_all(@folder, @glob)
49
+ end
50
+
51
+ def test_most_recent
52
+ assert_match(/\.md$/, MarkdownExec::SavedFilesMatcher.most_recent(@folder, @glob))
53
+ end
54
+
55
+ def test_most_recent_list
56
+ result = MarkdownExec::SavedFilesMatcher.most_recent_list(@folder, @glob, 5)
57
+ assert_kind_of Array, result
58
+ assert_operator result.size, :<=, 16
59
+ end
60
+ end
61
+ end
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.5
4
+ version: 1.3.7
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-05 00:00:00.000000000 Z
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clipboard
@@ -110,17 +110,29 @@ files:
110
110
  - bin/setup
111
111
  - bin/tab_completion.sh
112
112
  - bin/tab_completion.sh.erb
113
+ - lib/block_label.rb
113
114
  - lib/cached_nested_file_reader.rb
114
115
  - lib/cli.rb
115
116
  - lib/colorize.rb
116
117
  - lib/env.rb
117
118
  - lib/env_opts.rb
118
119
  - lib/environment_opt_parse.rb
120
+ - lib/fcb.rb
121
+ - lib/filter.rb
122
+ - lib/markdown_block_manager.rb
119
123
  - lib/markdown_exec.rb
120
124
  - lib/markdown_exec/version.rb
125
+ - lib/mdoc.rb
126
+ - lib/menu.src.yml
121
127
  - lib/menu.yml
128
+ - lib/menu_options.rb
129
+ - lib/menu_options.yml
122
130
  - lib/object_present.rb
131
+ - lib/option_value.rb
132
+ - lib/regexp.rb
123
133
  - lib/rspec_helpers.rb
134
+ - lib/saved_assets.rb
135
+ - lib/saved_files_matcher.rb
124
136
  - lib/shared.rb
125
137
  - lib/tap.rb
126
138
  homepage: https://rubygems.org/gems/markdown_exec