lib/markdown_exec.rb in markdown_exec-2.4.0 vs lib/markdown_exec.rb in markdown_exec-2.5.0

- old
+ new

@@ -13,11 +13,11 @@ require 'tmpdir' require 'tty-prompt' require 'yaml' require_relative 'ansi_formatter' -require_relative 'block_label' +# require_relative 'block_label' require_relative 'cached_nested_file_reader' require_relative 'cli' require_relative 'color_scheme' require_relative 'colorize' require_relative 'directory_searcher' @@ -79,15 +79,26 @@ require 'pry-stack_explorer' end public -# convert regex match groups to a hash with symbol keys -# -# :reek:UtilityFunction -def extract_named_captures_from_option(str, option) - str.match(Regexp.new(option))&.named_captures&.sym_keys +# NamedCaptureExtractor is a utility class for extracting named groups from regular expressions +# and converting them into symbol-keyed hashes for flexible pattern matching and data extraction. +class NamedCaptureExtractor + # Extracts named groups from a regex match on the given string, converting them into a symbol-keyed hash. + # + # @param str [String] the string to match against the provided regex pattern + # @param pattern [Regexp, String] the regex pattern with named groups + # @return [Hash<Symbol, String>, nil] hash of named groups as symbols and their corresponding matches, + # or nil if no match is found + def self.extract_named_groups(str, pattern) + regexp = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern) + str&.match(regexp)&.named_captures&.transform_keys(&:to_sym) + end + def self.extract_named_group2(match_data) + match_data&.named_captures&.transform_keys(&:to_sym) + end end # execute markdown documents # module MarkdownExec @@ -243,11 +254,11 @@ def read_block_name(line, fenced_start_and_end_regex, block_name_match, block_name_nick_match) return unless line.match(fenced_start_and_end_regex) - bm = extract_named_captures_from_option(line, block_name_match) + bm = NamedCaptureExtractor::extract_named_groups(line, block_name_match) return if bm.nil? name = bm[:title] if block_name_nick_match.present? && line =~ Regexp.new(block_name_nick_match) @@ -326,11 +337,11 @@ end.compact.to_h end def calculated_options { - bash: true, # bash block parsing in get_block_summary() + bash: true, # bash block parsing in fcb_for_menu!() saved_script_filename: nil # calculated } end def choices_from_block_names(value, found_in_block_names) @@ -392,35 +403,48 @@ ## Executes the block specified in the options # def execute_block_with_error_handling finalize_cli_argument_processing - execute_code_block_based_on_options(@options, @options.run_state) + execute_initial_commands_and_main_loop(@options, @options.run_state) rescue FileMissingError warn "File missing: #{$!}" end # Main method to execute a block based on options and block_name - def execute_code_block_based_on_options(options, run_state) + def execute_initial_commands_and_main_loop(options, run_state) options = calculated_options.merge(options) update_options(options, over: false) # recognize commands with an opt_name, no procname - return if execute_simple_commands(options) + # !!b + return if execute_simple_commands(options, stage: 1) + # !!b mde_vux_main_loop(opts_prepare_file_list(options)) + # !!b return unless @options[:output_saved_script_filename] + # !!b @fout.fout "script_block_name: #{run_state.script_block_name}" @fout.fout "s_save_filespec: #{run_state.saved_filespec}" end # Executes command based on the provided option keys - def execute_simple_commands(options) - simple_commands(options).each do |key, proc| + def execute_simple_commands(options, stage: nil) + # !!p stage + simple_commands(options).each do |key, (cstage, proc)| if @options[key].is_a?(TrueClass) || @options[key].present? - proc.call - return true + # !!v key, 'cstage', cstage + if stage && stage == cstage + # !!b + proc.call + return true + else + # !!b + end + else + # !!b end end false end @@ -556,12 +580,13 @@ if @options[:sift].present? sift_regexp = Regexp.new(@options[:sift], Regexp::IGNORECASE) end - files_table_rows = @options.read_saved_assets_for_history_table - + files_table_rows = @options.read_saved_assets_for_history_table( + asset: @options[:filename] + ) # !!v files_table_rows if sift_regexp # Filter history to file names matching a pattern files_table_rows.select! { |item| sift_regexp.match(item[:file]) } end @@ -789,17 +814,25 @@ saved_script_glob, list_count) end # Reports and executes block logic def mde_vux_main_loop(files) + # !!b @options[:filename] = select_document_if_multiple(files) @options.vux_main_loop do |type, data| case type when :command_names + # !!b simple_commands(data).keys when :call_proc - simple_commands(data[0])[data[1]].call + # !!b + # simple_commands(data[0])[data[1]].call + simple_commands(data[0])[data[1]][1].call + + when :end_of_cli + # !!b + execute_simple_commands(options, stage: 2) else raise end end end @@ -997,32 +1030,33 @@ prompt_text, strings, opts )&.fetch(:selected) end def simple_commands(options) + # !!b { - doc_glob: -> { @fout.fout options[:md_filename_glob] }, - history: -> { history }, - list_blocks: -> { list_blocks }, - list_default_env: -> { @fout.fout_list list_default_env }, - list_default_yaml: -> { @fout.fout_list list_default_yaml }, - list_docs: -> { @fout.fout_list opts_prepare_file_list(options) }, - list_recent_output: -> { + doc_glob: [1, -> { @fout.fout options[:md_filename_glob] }], + history: [1, -> { history }], + list_blocks: [2, -> { list_blocks }], + list_default_env: [1, -> { @fout.fout_list list_default_env }], + list_default_yaml: [1, -> { @fout.fout_list list_default_yaml }], + list_docs: [1, -> { @fout.fout_list opts_prepare_file_list(options) }], + list_recent_output: [1, -> { @fout.fout_list list_recent_output( @options[:saved_stdout_folder], @options[:saved_stdout_glob], @options[:list_count] ) - }, - list_recent_scripts: -> { + }], + list_recent_scripts: [1, -> { @fout.fout_list list_recent_scripts( options[:saved_script_folder], options[:saved_script_glob], options[:list_count] ) - }, - menu_export: -> { @fout.fout menu_export }, - pwd: -> { @fout.fout File.expand_path('..', __dir__) }, - run_last_script: -> { run_last_script }, - tab_completions: -> { @fout.fout tab_completions } + }], + menu_export: [1, -> {@fout.fout menu_export }], + pwd: [1, -> {@fout.fout File.expand_path('..', __dir__) }], + run_last_script: [1, -> {run_last_script }], + tab_completions: [1, -> {@fout.fout tab_completions }] } end public