CHANGELOG.md in markdown_exec-1.3.3.4 vs CHANGELOG.md in markdown_exec-1.3.3.5
- old
+ new
@@ -104,9 +104,168 @@
- [ ] block into file; template
- [! ] option to use most recent named block if dupiclate, currently appends same-name blocks but lists twice in menu
+- [! ] improve error when imported file is not found
+
+- [ ] decorations for import block
+prefix line(s)
+inline replacements
+suffix line(s)
+
+```ruby
+##
+# Replace substrings in an input string based on a regular expression pattern
+# with named capture groups. The replacements are formatted using a provided
+# format string. Additional context can be provided to supplement or override
+# the named captures in the format string.
+#
+# @param input_str [String] The input string to process.
+# @param regex [Regexp] The regular expression pattern with named capture groups.
+# @param format_str [String] The format string for sprintf.
+# @param context [Hash] Additional context to supplement or override named captures.
+#
+# @return [String] The processed string after replacements.
+#
+# def gsub_format(input_str, regex, format_str, context: {})
+# input_str.gsub(regex) do
+# captures_hash = $~.names.each_with_object({}) do |name, hash|
+# hash[name.to_sym] = $~[name]
+# end
+
+# ### add import file name, line number, line, to captures_hash, chain
+# # $~ (MatchData) - MatchData object created from the match; thread-local and frame-local. - English - $LAST_MATCH_INFO.
+# # $& (Matched Substring) - The matched string. - English - $MATCH.
+# # $` (Pre-Match Substring) - The string to the left of the match. - English - $PREMATCH.
+# # $' (Post-Match Substring) - The string to the right of the match. - English - $POSTMATCH.
+# # $+ (Last Matched Group) - The last group matched. - English - $LAST_PAREN_MATCH.
+
+# sprintf(format_str, context.merge(captures_hash))
+# end
+# end
+
+class Regexp
+ def gsub_format(input_str, format_str, context: {})
+ input_str.gsub(self) do
+ captures_hash = $~.names.each_with_object({}) do |name, hash|
+ hash[name.to_sym] = $~[name]
+ end
+
+# ### add import file name, line number, line, to captures_hash, chain
+# # $~ (MatchData) - MatchData object created from the match; thread-local and frame-local. - English - $LAST_MATCH_INFO.
+# # $& (Matched Substring) - The matched string. - English - $MATCH.
+# # $` (Pre-Match Substring) - The string to the left of the match. - English - $PREMATCH.
+# # $' (Post-Match Substring) - The string to the right of the match. - English - $POSTMATCH.
+# # $+ (Last Matched Group) - The last group matched. - English - $LAST_PAREN_MATCH.
+
+ # # Add file name, line number, line to captures_hash
+ # captures_hash[:file_name] = $~.pre_match.split("\n").last
+ # captures_hash[:line_number] = $~.pre_match.count("\n") + 1
+ # captures_hash[:line] = $&
+
+ sprintf(format_str, context.merge(captures_hash))
+ end
+ end
+end
+
+# # Example usage:
+# str = "123 example"
+# re = /(?<foo>\d+) (?<bar>\w+)/
+# fmt = "%<foo>d : %<bar>s"
+# new_str = gsub_format(str, re, fmt)
+# puts new_str # Outputs: 123 : example
+
+require 'minitest/autorun'
+require_relative 'path_to_your_file' # Make sure to replace this with the path to the file containing the function
+
+class ReplaceWithFormatTest < Minitest::Test
+ def test_basic_replacement
+ input_str = "123 example"
+ re = /(?<foo>\d+) (?<bar>\w+)/
+ fmt = "%<foo>d : %<bar>s"
+
+ result = gsub_format(input_str, re, fmt)
+
+ assert_equal "123 : example", result
+ end
+
+ def test_no_match
+ input_str = "This is a test."
+ re = /(?<foo>\d+) (?<bar>\w+)/
+ fmt = "%<foo>d : %<bar>s"
+
+ result = gsub_format(input_str, re, fmt)
+
+ assert_equal "This is a test.", result
+ end
+
+ def test_multiple_matches
+ input_str = "123 example, 456 test"
+ re = /(?<foo>\d+) (?<bar>\w+)/
+ fmt = "[%<foo>d %<bar>s]"
+
+ result = gsub_format(input_str, re, fmt)
+
+ assert_equal "[123 example], [456 test]", result
+ end
+
+ def test_different_named_captures
+ input_str = "Jane is 25 years old."
+ re = /(?<name>\w+) is (?<age>\d+)/
+ fmt = "%<name>s's age is %<age>d"
+
+ result = gsub_format(input_str, re, fmt)
+
+ assert_equal "Jane's age is 25", result
+ end
+
+ def test_with_context
+ input_str = "Jane is 25 years old."
+ re = /(?<name>\w+) is (?<age>\d+)/
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
+
+ result = gsub_format(input_str, re, fmt, context: { city: "New York" })
+
+ assert_equal "Jane's age is 25 and she lives in New York", result
+ end
+end
+
+require 'minitest/autorun'
+require_relative 'path_to_your_file' # Ensure this path is correct
+
+class RegexpGsubFormatTest < Minitest::Test
+ def test_basic_replacement
+ input_str = "123 example"
+ re = /(?<foo>\d+) (?<bar>\w+)/
+ fmt = "%<foo>d : %<bar>s"
+
+ result = re.gsub_format(input_str, fmt)
+
+ assert_equal "123 : example", result
+ end
+
+ # ... [other tests remain mostly unchanged, just updating the method call]
+
+ def test_with_context
+ input_str = "Jane is 25 years old."
+ re = /(?<name>\w+) is (?<age>\d+)/
+ fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
+
+ result = re.gsub_format(input_str, fmt, context: { city: "New York" })
+
+ assert_equal "Jane's age is 25 and she lives in New York", result
+ end
+end
+
+```
+
+## [1.3.5] - 2023-10-05
+
+### Changed
+
+- Fix display of menu dividers.
+
## [1.3.3] - 2023-10-03
### Added
- Nest scripts by using an `import` directive.