lib/markdown_exec.rb in markdown_exec-0.0.6 vs lib/markdown_exec.rb in markdown_exec-0.1.0

- old
+ new

@@ -1,53 +1,26 @@ -# frozen_string_literal: true - -require_relative 'markdown_exec/version' - #!/usr/bin/env ruby # frozen_string_literal: true # encoding=utf-8 + # rubocop:disable Style/GlobalVars +$pdebug = !(ENV['MARKDOWN_EXEC_DEBUG'] || '').empty? -env_debug = ENV['MARKDOWN_EXEC_DEBUG'] -$pdebug = !(env_debug || '').empty? - -# APP_NAME = 'MDExec' -# APP_DESC = 'Markdown block executor' -# VERSION = '0.0.6' - -# require 'markdown_exec' -# # puts MarkdownExec::MarkParse.echo(ARGV[0]) - +require 'open3' require 'optparse' require 'pathname' require 'tty-prompt' require 'yaml' +require_relative 'markdown_exec/version' -# require_relative 'mdlib' -# #!/usr/bin/env ruby -# # frozen_string_literal: true -# # encoding=utf-8 - -# env_debug = ENV['MARKDOWN_EXEC_DEBUG'] -# $pdebug = !(env_debug || '').empty? - -require 'open3' -# require 'tty-prompt' - BLOCK_SIZE = 1024 SELECT_PAGE_HEIGHT = 12 module MarkdownExec class Error < StandardError; end - # Markdown Exec - # class MarkParse - # def self.echo(str = '') - # "#{str}#{str}" - # end - # end ## # class MarkParse attr_accessor :options @@ -181,11 +154,11 @@ elsif current current += [line.chomp] end end blocks.tap { |ret| puts "get_blocks() ret: #{ret.inspect}" if $pdebug } - end + end # get_blocks def make_block_label(block, call_options = {}) opts = options.merge(call_options) puts "make_block_label() opts: #{opts.inspect}" if $pdebug puts "make_block_label() block: #{block.inspect}" if $pdebug @@ -301,11 +274,11 @@ end end end selected[:name] - end + end # select_block def select_md_file opts = options files = find_files if files.count == 1 @@ -385,152 +358,162 @@ .flatten(1) .tap { |_ret| puts "unroll() rem: #{rem.inspect}" if $pdebug } end all.tap { |ret| puts "unroll() ret: #{ret.inspect}" if $pdebug } end - end - # $stderr.sync = true - # $stdout.sync = true + # $stderr.sync = true + # $stdout.sync = true - def fout(str) - puts str # to stdout - end - - ## configuration file - # - def read_configuration!(options, configuration_path) - if Pathname.new(configuration_path).exist? - # rubocop:disable Security/YAMLLoad - options.merge!((YAML.load(File.open(configuration_path)) || {}) - .transform_keys(&:to_sym)) - # rubocop:enable Security/YAMLLoad + ## configuration file + # + def read_configuration!(options, configuration_path) + if Pathname.new(configuration_path).exist? + # rubocop:disable Security/YAMLLoad + options.merge!((YAML.load(File.open(configuration_path)) || {}) + .transform_keys(&:to_sym)) + # rubocop:enable Security/YAMLLoad + end + options end - options - end - def run - ## default configuration - # - options = { - mdheadings: true, - list_blocks: false, - list_docs: false, - mdfilename: 'README.md', - mdfolder: '.' - } + def run + ## default configuration + # + options = { + mdheadings: true, + list_blocks: false, + list_docs: false, + mdfilename: 'README.md', + mdfolder: '.' + } - def options_finalize!(options); end + def options_finalize!(options); end - # read local configuration file - # - read_configuration! options, ".#{APP_NAME.downcase}.yml" + # puts "MDE run() ARGV: #{ARGV.inspect}" - ## read current details for aws resources from app_data_file - # - # load_resources! options - # puts "q31 options: #{options.to_yaml}" if $pdebug - - # rubocop:disable Metrics/BlockLength - option_parser = OptionParser.new do |opts| - executable_name = File.basename($PROGRAM_NAME) - opts.banner = [ - "#{APP_NAME} - #{APP_DESC} (#{VERSION})".freeze, - "Usage: #{executable_name} [options]" - ].join("\n") - - ## menu top: on_head appear in reverse order added + # read local configuration file # - opts.on('--config PATH', 'Read configuration file') do |value| - read_configuration! options, value - end + read_configuration! options, ".#{MarkdownExec::APP_NAME.downcase}.yml" - ## menu body: items appear in order added + ## read current details for aws resources from app_data_file # - opts.on('-f RELATIVE', '--mdfilename', 'Name of document') do |value| - options[:mdfilename] = value - end + # load_resources! options + # puts "q31 options: #{options.to_yaml}" if $pdebug - opts.on('-p PATH', '--mdfolder', 'Path to documents') do |value| - options[:mdfolder] = value - end + # rubocop:disable Metrics/BlockLength + option_parser = OptionParser.new do |opts| + executable_name = File.basename($PROGRAM_NAME) + opts.banner = [ + "#{MarkdownExec::APP_NAME} - #{MarkdownExec::APP_DESC} (#{MarkdownExec::VERSION})", + "Usage: #{executable_name} [options]" + ].join("\n") - opts.on('--list-blocks', 'List blocks') do |_value| - options[:list_blocks] = true - end + ## menu top: on_head appear in reverse order added + # + opts.on('--config PATH', 'Read configuration file') do |value| + read_configuration! options, value + end - opts.on('--list-docs', 'List docs in current folder') do |_value| - options[:list_docs] = true - end + ## menu body: items appear in order added + # + opts.on('-f RELATIVE', '--mdfilename', 'Name of document') do |value| + options[:mdfilename] = value + end - ## menu bottom: items appear in order added - # - opts.on_tail('-h', '--help', 'App help') do |_value| - puts option_parser.help - exit - end + opts.on('-p PATH', '--mdfolder', 'Path to documents') do |value| + options[:mdfolder] = value + end - opts.on_tail('-v', '--version', 'App version') do |_value| - puts VERSION - exit - end + opts.on('--list-blocks', 'List blocks') do |_value| + options[:list_blocks] = true + end - opts.on_tail('-x', '--exit', 'Exit app') do |_value| - exit - end + opts.on('--list-docs', 'List docs in current folder') do |_value| + options[:list_docs] = true + end - opts.on_tail('-0', 'Show configuration') do |_v| - options_finalize! options - puts options.to_yaml - end - end - # rubocop:enable Metrics/BlockLength + ## menu bottom: items appear in order added + # + opts.on_tail('-h', '--help', 'App help') do |_value| + puts option_parser.help + exit + end - option_parser.load # filename defaults to basename of the program without suffix in a directory ~/.options - option_parser.environment # env defaults to the basename of the program. - option_parser.parse! # (into: options) - options_finalize! options + opts.on_tail('-v', '--version', 'App version') do |_value| + puts MarkdownExec::VERSION + exit + end - ## process - # - # rubocop:disable Metrics/BlockLength - loop do # once - mp = MarkParse.new options - options.merge!( - { - approve: true, - bash: true, - display: true, - exclude_expect_blocks: true, - execute: true, - prompt: 'Execute', - struct: true - } - ) + opts.on_tail('-x', '--exit', 'Exit app') do |_value| + exit + end - ## show - # - if options[:list_docs] - fout mp.find_files - break - end + opts.on_tail('-0', 'Show configuration') do |_v| + options_finalize! options + puts options.to_yaml + end + end # OptionParser + # rubocop:enable Metrics/BlockLength - if options[:list_blocks] - fout (mp.find_files.map do |file| - mp.make_block_labels(mdfilename: file, struct: true) - end).flatten(1).to_yaml - break - end + option_parser.load # filename defaults to basename of the program without suffix in a directory ~/.options + option_parser.environment # env defaults to the basename of the program. + option_parser.parse! # (into: options) + options_finalize! options ## process # - mp.select_block(bash: true, struct: true) if options[:mdfilename] + # rubocop:disable Metrics/BlockLength + loop do # once + mp = MarkParse.new options + options.merge!( + { + approve: true, + bash: true, + display: true, + exclude_expect_blocks: true, + execute: true, + prompt: 'Execute', + struct: true + } + ) - # rubocop:disable Style/BlockComments - # rubocop:enable Style/BlockComments + ## show + # + if options[:list_docs] + fout mp.find_files + break + end - break unless false # rubocop:disable Lint/LiteralAsCondition - end - end + if options[:list_blocks] + fout (mp.find_files.map do |file| + mp.make_block_labels(mdfilename: file, struct: true) + end).flatten(1).to_yaml + break + end + + ## process + # + mp.select_block(bash: true, struct: true) if options[:mdfilename] + +# rubocop:disable Style/BlockComments +=begin + # rescue ArgumentError => e + # puts "User abort: #{e}" + + # rescue StandardError => e + # puts "ERROR: #{e}" + # raise StandardError, e + + # ensure + # exit +=end + # rubocop:enable Style/BlockComments + + break unless false # rubocop:disable Lint/LiteralAsCondition + end # loop + end # run + end # class MarkParse + # rubocop:enable Metrics/BlockLength # rubocop:enable Style/GlobalVars -end +end # module MarkdownExec