lib/markdown_exec.rb in markdown_exec-1.5 vs lib/markdown_exec.rb in markdown_exec-1.6

- old
+ new

@@ -536,19 +536,28 @@ # @param opts [Hash] Options containing configuration for line processing. # @param use_chrome [Boolean] Indicates if the chrome styling should be applied. def create_and_add_chrome_blocks(blocks, fcb, opts, use_chrome) return unless use_chrome - if opts[:menu_note_match].present? && (mbody = fcb.body[0].match opts[:menu_note_match]) - create_and_add_chrome_block(blocks, fcb, mbody, opts[:menu_note_format], - opts[:menu_note_color].to_sym) - elsif opts[:menu_divider_match].present? && (mbody = fcb.body[0].match opts[:menu_divider_match]) - create_and_add_chrome_block(blocks, fcb, mbody, opts[:menu_divider_format], - opts[:menu_divider_color].to_sym) - elsif opts[:menu_task_match].present? && (mbody = fcb.body[0].match opts[:menu_task_match]) - create_and_add_chrome_block(blocks, fcb, mbody, opts[:menu_task_format], - opts[:menu_task_color].to_sym) + match_criteria = [ + { match: :menu_task_match, format: :menu_task_format, + color: :menu_task_color }, + { match: :menu_divider_match, format: :menu_divider_format, + color: :menu_divider_color }, + { match: :menu_note_match, format: :menu_note_format, + color: :menu_note_color } + ] + + match_criteria.each do |criteria| + unless opts[criteria[:match]].present? && + (mbody = fcb.body[0].match opts[criteria[:match]]) + next + end + + create_and_add_chrome_block(blocks, fcb, mbody, opts[criteria[:format]], + opts[criteria[:color]].to_sym) + break end end def create_and_write_file_with_permissions(file_path, content, chmod_value) @@ -728,10 +737,11 @@ block_name = rest.shift @options[:block_name] = block_name if block_name.present? end # Formats multiline body content as a title string. + # indents all but first line with two spaces so it displays correctly in menu # @param body_lines [Array<String>] The lines of body content. # @return [String] Formatted title. def format_multiline_body_as_title(body_lines) body_lines.map.with_index do |line, index| index.zero? ? line : " #{line}" @@ -891,10 +901,20 @@ opts[:filename] = data_file write_required_blocks_to_temp_file(mdoc, opts[:block_name], opts) ENV[MDE_HISTORY_ENV_NAME] = new_history end + # Indents all lines in a given string with a specified indentation string. + # @param body [String] A multi-line string to be indented. + # @param indent [String] The string used for indentation (default is an empty string). + # @return [String] A single string with each line indented as specified. + def indent_all_lines(body, indent = nil) + return body if !indent.present? + + body.lines.map { |line| indent + line.chomp }.join("\n") + end + ## Sets up the options and returns the parsed arguments # def initialize_and_parse_cli_options @options = base_options read_configuration_file!(@options, @@ -1130,35 +1150,10 @@ item.delete(:procname) item end.to_yaml end - def menu_for_blocks(menu_options) - options = calculated_options.merge menu_options - menu = [] - iter_blocks_in_file(options) do |btype, fcb| - case btype - when :filter - %i[blocks line] - when :line - if options[:menu_divider_match] && - (mbody = fcb.body[0].match(options[:menu_divider_match])) - menu.push FCB.new({ dname: mbody[:name], oname: mbody[:name], - disabled: '' }) - end - if options[:menu_note_match] && - (mbody = fcb.body[0].match(options[:menu_note_match])) - menu.push FCB.new({ dname: mbody[:name], oname: mbody[:name], - disabled: '' }) - end - when :blocks - menu += [fcb.oname] - end - end - menu - end - ## # Generates a menu suitable for OptionParser from the menu items defined in YAML format. # @return [Array<Hash>] The array of option hashes for OptionParser. def menu_for_optparse menu_from_yaml.map do |menu_item| @@ -1319,11 +1314,11 @@ # next unless fcb.fetch(:name, '').present? replace_consecutive_blanks(blocks_in_file).map do |fcb| next if Filter.prepared_not_in_menu?(opts, fcb) fcb.merge!( - name: fcb.dname, + name: indent_all_lines(fcb.dname, fcb.fetch(:indent, nil)), label: BlockLabel.make( body: fcb[:body], filename: opts[:filename], headings: fcb.fetch(:headings, []), menu_blocks_with_docname: opts[:menu_blocks_with_docname], @@ -1651,10 +1646,11 @@ rest = fcb_title_groups.fetch(:rest, '') fcb = FCB.new fcb.headings = headings fcb.oname = fcb.dname = fcb_title_groups.fetch(:name, '') + fcb.indent = fcb_title_groups.fetch(:indent, '') fcb.shell = fcb_title_groups.fetch(:shell, '') fcb.title = fcb_title_groups.fetch(:name, '') fcb.body = [] fcb.reqs, fcb.wraps = ArrayUtil.partition_by_predicate(rest.scan(/\+[^\s]+/).map do |req| @@ -1747,10 +1743,16 @@ start_fenced_block(opts, line, state[:headings], state[:fenced_start_extended_regex]) state[:in_fenced_block] = true end elsif state[:in_fenced_block] && state[:fcb].body - state[:fcb].body += [line.chomp] + ## add line to fenced code block + # remove fcb indent if possible + # + state[:fcb].body += [ + line.chomp.sub(/^#{state[:fcb].indent}/, '') + ] + else process_line(line, opts, selected_messages, &block) end end