markdown_exec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 631d15d315eeb3a40ef3458f9b0577d4b2f5f9eeddbcdd3ad26c9d2cb1e28c3a
4
+ data.tar.gz: f1fdebc4461fb7b5f3b37ebd2f8d369cbe804621cc42ca8c672baec5ea0c4f74
5
+ SHA512:
6
+ metadata.gz: f597613ff0bd0c556e0c205168d32653b4a4cd71cd93c24549b0b9f3f18dd8551ca301ac618f191fa005dfdbfaceef6263e4fe18cb229244c20c989b8fb99ac4
7
+ data.tar.gz: c890e7187a1394e37f56a81e7f3a2a64dbd3ec0fe8f6d9b904089b9d7b3d10a89c7df476aac2ff3bc437c50137d89f28bea8f4711995f2b1ed8f660785dd76ae
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: single_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: single_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-06
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at atlass32@com.go.xhome.us. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in markdown_exec.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Fareed Stevenson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # MarkdownExec
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/markdown_exec`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'markdown_exec'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install markdown_exec
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/markdown_exec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/markdown_exec/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the MarkdownExec project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/markdown_exec/blob/master/CODE_OF_CONDUCT.md).
44
+
45
+ # 2022-03-06
46
+ ```
47
+ gem build markdown_exec.gemspec
48
+ ```
49
+ ```expect
50
+ Successfully built RubyGem
51
+ Name: markdown_exec
52
+ Version: 0.0.1
53
+ File: markdown_exec-0.0.1.gem
54
+ ```
55
+ ```
56
+ gem install ./markdown_exec-0.0.1.gem
57
+ ```
58
+ ```
59
+ gem list | grep exec
60
+ ```
61
+ ```
62
+ gem uninstall markdown_exec
63
+ ```
64
+ pusher api key: rubygems_8c4b38ef685484719858c53c3161a124c15d7a9bec6cb65a
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
17
+
18
+ GEM_NAME = "pagekey"
19
+ GEM_VERSION = "0.0.0"
20
+
21
+ # task :default => :build
22
+
23
+ task :build do
24
+ system "gem build " + GEM_NAME + ".gemspec"
25
+ end
26
+
27
+ task :install => :build do
28
+ system "gem install " + GEM_NAME + "-" + GEM_VERSION + ".gem"
29
+ end
30
+
31
+ task :publish => :build do
32
+ system 'gem push ' + GEM_NAME + "-" + GEM_VERSION + ".gem"
33
+ end
34
+
35
+ task :clean do
36
+ system "rm *.gem"
37
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "markdown_exec"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/mde ADDED
@@ -0,0 +1,521 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+
6
+ env_debug = ENV["MARKDOWN_EXEC_DEBUG"]
7
+ $pdebug = !(env_debug || "").empty?
8
+
9
+ APP_NAME = "MDExec"
10
+ APP_DESC = "Markdown block executor"
11
+ VERSION = "0.0.2"
12
+
13
+ require "markdown_exec"
14
+ # puts MarkdownExec::MDExec.echo(ARGV[0])
15
+
16
+ require "optparse"
17
+ require "pathname"
18
+ require "tty-prompt"
19
+ require "yaml"
20
+
21
+ # require_relative 'mdlib'
22
+ # #!/usr/bin/env ruby
23
+ # # frozen_string_literal: true
24
+ # # encoding=utf-8
25
+
26
+ # env_debug = ENV['MARKDOWN_EXEC_DEBUG']
27
+ # $pdebug = !(env_debug || '').empty?
28
+
29
+ require "open3"
30
+ require "tty-prompt"
31
+ # require 'yaml'
32
+ # require_relative 'deep_merge'
33
+ # require_relative 'object_odig'
34
+ # require_relative 'present'
35
+
36
+ BLOCK_SIZE = 1024
37
+ SELECT_PAGE_HEIGHT = 12
38
+
39
+ ##
40
+ #
41
+ class MarkParse
42
+ attr_accessor :options
43
+
44
+ def initialize(options)
45
+ @options = options
46
+ end
47
+
48
+ def count_blocks
49
+ cnt = 0
50
+ File.readlines(options[:mdfilename]).each do |line|
51
+ cnt += 1 if line.match(/^```/)
52
+ end
53
+ cnt / 2
54
+ end
55
+
56
+ def find_files
57
+ puts "pwd: #{`pwd`}" if $pdebug
58
+ # `ls -1 *.md`.split("\n").tap { |ret| puts "find_files() ret: #{ret.inspect}" if $pdebug }
59
+ `ls -1 #{File.join options[:mdfolder], "*.md"}`.split("\n").tap do |ret|
60
+ puts "find_files() ret: #{ret.inspect}" if $pdebug
61
+ end
62
+ end
63
+
64
+ def fout(str)
65
+ puts str # to stdout
66
+ end
67
+
68
+ def copts(call_options = {}, options_block = nil)
69
+ class_call_options = options.merge(call_options || {})
70
+ if options_block
71
+ options_block.call class_call_options
72
+ else
73
+ class_call_options
74
+ end.tap { |ret| puts "copts() ret: #{ret.inspect}" if $pdebug }
75
+ end
76
+
77
+ def bsr(headings, title)
78
+ # puts "bsr() headings: #{headings.inspect}"
79
+ { headings: headings, name: title, title: title }
80
+ end
81
+
82
+ def block_summary(opts, headings, block_title, current)
83
+ puts "block_summary() block_title: #{block_title.inspect}" if $pdebug
84
+ return [current] unless opts[:struct]
85
+
86
+ # return [{ body: current, name: block_title, title: block_title }] unless opts[:bash]
87
+ return [bsr(headings, block_title).merge({ body: current })] unless opts[:bash]
88
+
89
+ bm = block_title.match(/:(\S+)( |$)/)
90
+ reqs = block_title.scan(/\+\S+/).map { |s| s[1..] }
91
+
92
+ if $pdebug
93
+ puts ["block_summary() bm: #{bm.inspect}",
94
+ "block_summary() reqs: #{reqs.inspect}"]
95
+ end
96
+
97
+ if bm && bm[1]
98
+ # [{ body: current, name: bm[1], reqs: reqs, title: bm[1] }]
99
+ [bsr(headings, bm[1]).merge({ body: current, reqs: reqs })]
100
+ else
101
+ # [{ body: current, name: block_title, reqs: reqs, title: block_title }]
102
+ [bsr(headings, block_title).merge({ body: current, reqs: reqs })]
103
+ end
104
+ end
105
+
106
+ def get_blocks(call_options = {}, &options_block)
107
+ opts = copts call_options, options_block
108
+
109
+ blocks = []
110
+ current = nil
111
+ in_block = false
112
+ block_title = ""
113
+
114
+ headings = []
115
+ File.readlines(opts[:mdfilename]).each do |line|
116
+ puts "get_blocks() line: #{line.inspect}" if $pdebug
117
+ continue unless line
118
+
119
+ if opts[:mdheadings]
120
+ if lm = line.match(/^### *(.+?) *$/)
121
+ headings = [headings[0], headings[1], lm[1]]
122
+ elsif lm = line.match(/^## *([^#]*?) *$/)
123
+ headings = [headings[0], lm[1]]
124
+ elsif lm = line.match(/^# *([^#]*?) *$/)
125
+ headings = [lm[1]]
126
+ end
127
+ puts "get_blocks() headings: #{headings.inspect}" if $pdebug
128
+ end
129
+
130
+ if line.match(/^`{3,}/)
131
+ if in_block
132
+ puts "get_blocks() in_block" if $pdebug
133
+ if current
134
+
135
+ # block_title ||= current.join(' ').gsub(/ +/, ' ')[0..64]
136
+ block_title = current.join(" ").gsub(/ +/, " ")[0..64] if block_title.nil? || block_title.empty?
137
+
138
+ blocks += block_summary opts, headings, block_title, current
139
+ current = nil
140
+ end
141
+ in_block = false
142
+ block_title = ""
143
+ else
144
+ ## new block
145
+ #
146
+
147
+ # lm = line.match(/^`{3,}([^`\s]+)( .+)?$/)
148
+ lm = line.match(/^`{3,}([^`\s]*) *(.*)$/)
149
+
150
+ do1 = false
151
+ if opts[:bash_only]
152
+ do1 = true if lm && (lm[1] == "bash")
153
+ elsif opts[:exclude_expect_blocks]
154
+ do1 = true unless lm && (lm[1] == "expect")
155
+ else
156
+ do1 = true
157
+ end
158
+ if $pdebug
159
+ puts ["get_blocks() lm: #{lm.inspect}",
160
+ "get_blocks() opts: #{opts.inspect}",
161
+ "get_blocks() do1: #{do1}"]
162
+ end
163
+
164
+ if do1 && (!opts[:title_match] || (lm && lm[2] && lm[2].match(opts[:title_match])))
165
+ current = []
166
+ in_block = true
167
+ block_title = (lm && lm[2])
168
+ end
169
+
170
+ end
171
+ elsif current
172
+ current += [line.chomp]
173
+ end
174
+ end
175
+ blocks.tap { |ret| puts "get_blocks() ret: #{ret.inspect}" if $pdebug }
176
+ end
177
+
178
+ def make_block_label(block, call_options = {})
179
+ opts = options.merge(call_options)
180
+ puts "make_block_label() opts: #{opts.inspect}" if $pdebug
181
+ puts "make_block_label() block: #{block.inspect}" if $pdebug
182
+ if opts[:mdheadings]
183
+ heads = block.fetch(:headings, []).compact.join(" # ")
184
+ "#{block[:title]} [#{heads}] (#{opts[:mdfilename]})"
185
+ else
186
+ "#{block[:title]} (#{opts[:mdfilename]})"
187
+ end
188
+ end
189
+
190
+ def make_block_labels(call_options = {})
191
+ opts = options.merge(call_options)
192
+ get_blocks(opts).map do |block|
193
+ make_block_label block, opts
194
+ end
195
+ end
196
+
197
+ def select_block(call_options = {}, &options_block)
198
+ opts = copts call_options, options_block
199
+
200
+ blocks = get_blocks(opts.merge(struct: true))
201
+ puts "select_block() blocks: #{blocks.to_yaml}" if $pdebug
202
+
203
+ prompt = TTY::Prompt.new(interrupt: :exit)
204
+ pt = "#{opts.fetch(:prompt, nil) || "Pick one"}:"
205
+ puts "select_block() pt: #{pt.inspect}" if $pdebug
206
+
207
+ blocks.each { |block| block.merge! label: make_block_label(block, opts) }
208
+ block_labels = blocks.map { |block| block[:label] }
209
+ puts "select_block() block_labels: #{block_labels.inspect}" if $pdebug
210
+
211
+ if opts[:preview_options]
212
+ select_per_page = 3
213
+ block_labels.each do |bn|
214
+ fout " - #{bn}"
215
+ end
216
+ else
217
+ select_per_page = SELECT_PAGE_HEIGHT
218
+ end
219
+
220
+ return nil if block_labels.count.zero?
221
+
222
+ sel = prompt.select(pt, block_labels, per_page: select_per_page)
223
+ puts "select_block() sel: #{sel.inspect}" if $pdebug
224
+ # catch
225
+ # # catch TTY::Reader::InputInterrupt
226
+ # puts "InputInterrupt"
227
+ # end
228
+
229
+ block = blocks.select { |block| block[:label] == sel }.fetch(0, nil)
230
+ puts "select_block() block: #{block.inspect}" if $pdebug
231
+ sel = block[:name]
232
+ puts "select_block() sel: #{sel.inspect}" if $pdebug
233
+
234
+ cbs = code_blocks(blocks, sel)
235
+ puts "select_block() cbs: #{cbs.inspect}" if $pdebug
236
+
237
+ ## display code blocks for approval
238
+ #
239
+ cbs.each { |cb| fout cb } if opts[:display] || opts[:approve]
240
+
241
+ allow = true
242
+ allow = prompt.yes? "Process?" if opts[:approve]
243
+
244
+ selected = table_block blocks, sel
245
+ puts "select_block() selected: #{selected.inspect}" if $pdebug
246
+ if allow && opts[:execute]
247
+
248
+ ## process in script, to handle line continuations
249
+ #
250
+ cmd2 = cbs.flatten.join("\n")
251
+ fout "$ #{cmd2.to_yaml}"
252
+ cnt = 0
253
+
254
+ # Open3.popen3(cmd2) do |stdin, stdout, stderr, wait_thr|
255
+ # cnt += 1
256
+ # # stdin.puts "This is sent to the command"
257
+ # # stdin.close # we're done
258
+ # stdout_str = stdout.read # read stdout to string. note that this will block until the command is done!
259
+ # stderr_str = stderr.read # read stderr to string
260
+ # status = wait_thr.value # will block until the command finishes; returns status that responds to .success? etc
261
+ # fout "#{stdout_str}"
262
+ # fout "#{cnt}: err: #{stderr_str}" if stderr_str != ''
263
+ # # fout "#{cnt}: stt: #{status}"
264
+ # end
265
+
266
+ Open3.popen3(cmd2) do |stdin, stdout, stderr|
267
+ stdin.close_write
268
+ begin
269
+ files = [stdout, stderr]
270
+
271
+ until all_eof(files)
272
+ ready = IO.select(files)
273
+
274
+ next unless ready
275
+
276
+ readable = ready[0]
277
+ # writable = ready[1]
278
+ # exceptions = ready[2]
279
+
280
+ readable.each do |f|
281
+ fileno = f.fileno
282
+
283
+ begin
284
+ data = f.read_nonblock(BLOCK_SIZE)
285
+ # fout "- fileno: #{fileno}\n#{data}"
286
+ fout data
287
+ rescue EOFError => e
288
+ # fout "fileno: #{fileno} EOF"
289
+ end
290
+ end
291
+ end
292
+ rescue IOError => e
293
+ fout "IOError: #{e}"
294
+ end
295
+ end
296
+ end
297
+
298
+ selected[:name]
299
+ end
300
+
301
+ def select_md_file
302
+ opts = options
303
+ files = find_files
304
+ if files.count == 1
305
+ sel = files[0]
306
+ elsif files.count >= 2
307
+
308
+ if opts[:preview_options]
309
+ select_per_page = 3
310
+ files.each do |file|
311
+ fout " - #{file}"
312
+ end
313
+ else
314
+ select_per_page = SELECT_PAGE_HEIGHT
315
+ end
316
+
317
+ prompt = TTY::Prompt.new
318
+ sel = prompt.select("#{opts.fetch(:prompt, "Pick one")}:", files, per_page: select_per_page)
319
+ end
320
+
321
+ sel
322
+ end
323
+
324
+ # Returns true if all files are EOF
325
+ #
326
+ def all_eof(files)
327
+ files.find { |f| !f.eof }.nil?
328
+ end
329
+
330
+ def code(table, block)
331
+ puts "code() table: #{table.inspect}" if $pdebug
332
+ puts "code() block: #{block.inspect}" if $pdebug
333
+ all = [block[:name]] + unroll(table, block[:reqs])
334
+ puts "code() all: #{all.inspect}" if $pdebug
335
+ all.reverse.map do |req|
336
+ puts "code() req: #{req.inspect}" if $pdebug
337
+ table_block(table, req).fetch(:body, "")
338
+ end.flatten(1).tap { |ret| puts "code() ret: #{ret.inspect}" if $pdebug }
339
+ end
340
+
341
+ def table_block(table, name, default = {})
342
+ table.select { |block| block[:name] == name }.fetch(0, default)
343
+ end
344
+
345
+ def code_blocks(table, name)
346
+ puts "code_blocks() table: #{table.inspect}" if $pdebug
347
+ puts "code_blocks() name: #{name.inspect}" if $pdebug
348
+ block = table_block(table, name)
349
+ puts "code_blocks() block: #{block.inspect}" if $pdebug
350
+ all = [block[:name]] + unroll(table, block[:reqs])
351
+ puts "code_blocks() all: #{all.inspect}" if $pdebug
352
+ all.reverse.map do |req|
353
+ puts "code_blocks() req: #{req.inspect}" if $pdebug
354
+ table_block(table, req).fetch(:body, "")
355
+ end.flatten(1).tap { |ret| puts "code_blocks() ret: #{ret.inspect}" if $pdebug }
356
+ end
357
+
358
+ def unroll(table, reqs)
359
+ puts "unroll() table: #{table.inspect}" if $pdebug
360
+ puts "unroll() reqs: #{reqs.inspect}" if $pdebug
361
+ all = []
362
+ rem = reqs
363
+ while rem.count.positive?
364
+ puts "unrol() rem: #{rem.inspect}" if $pdebug
365
+ rem = rem.map do |req|
366
+ puts "unrol() req: #{req.inspect}" if $pdebug
367
+ next if all.include? req
368
+
369
+ all += [req]
370
+ puts "unrol() all: #{all.inspect}" if $pdebug
371
+ table_block(table, req).fetch(:reqs, [])
372
+ end.compact.flatten(1).tap { |_ret| puts "unroll() rem: #{rem.inspect}" if $pdebug }
373
+ end
374
+ all.tap { |ret| puts "unroll() ret: #{ret.inspect}" if $pdebug }
375
+ end
376
+ end
377
+
378
+ $stderr.sync = true
379
+ $stdout.sync = true
380
+
381
+ def fout(str)
382
+ puts str # to stdout
383
+ end
384
+
385
+ ## configuration file
386
+ #
387
+ def read_configuration!(options, configuration_path)
388
+ if Pathname.new(configuration_path).exist?
389
+ # rubocop:disable Security/YAMLLoad
390
+ options.merge!((YAML::load(open(configuration_path)) || {})
391
+ .transform_keys(&:to_sym))
392
+ # rubocop:enable Security/YAMLLoad
393
+ end
394
+ options
395
+ end
396
+
397
+ ## default configuration
398
+ #
399
+ options = {
400
+ mdheadings: true,
401
+ list_blocks: false,
402
+ list_docs: false,
403
+ mdfilename: "README.md",
404
+ mdfolder: "."
405
+ }
406
+
407
+ def options_finalize!(options); end
408
+
409
+ # read local configuration file
410
+ #
411
+ read_configuration! options, ".#{APP_NAME.downcase}.yml"
412
+
413
+ ## read current details for aws resources from app_data_file
414
+ #
415
+ # load_resources! options
416
+ # puts "q31 options: #{options.to_yaml}" if $pdebug
417
+
418
+ option_parser = OptionParser.new do |opts|
419
+ executable_name = File.basename($PROGRAM_NAME)
420
+ opts.banner = [
421
+ APP_VDESC = "#{APP_NAME} - #{APP_DESC} (#{VERSION})",
422
+ "Usage: #{executable_name} [options]"
423
+ ].join("\n")
424
+
425
+ ## menu top: on_head appear in reverse order added
426
+ #
427
+ opts.on("--config PATH", "Read configuration file") do |value|
428
+ read_configuration! options, value
429
+ end
430
+
431
+ ## menu body: items appear in order added
432
+ #
433
+ opts.on("-f RELATIVE", "--mdfilename", "Name of document") do |value|
434
+ options[:mdfilename] = value
435
+ end
436
+
437
+ opts.on("-p PATH", "--mdfolder", "Path to documents") do |value|
438
+ options[:mdfolder] = value
439
+ end
440
+
441
+ opts.on("--list-blocks", "List blocks") do |_value|
442
+ options[:list_blocks] = true
443
+ end
444
+
445
+ opts.on("--list-docs", "List docs in current folder") do |_value|
446
+ options[:list_docs] = true
447
+ end
448
+
449
+ ## menu bottom: items appear in order added
450
+ #
451
+ opts.on_tail("-h", "--help", "App help") do |_value|
452
+ puts option_parser.help
453
+ exit
454
+ end
455
+
456
+ opts.on_tail("-v", "--version", "App version") do |_value|
457
+ puts VERSION
458
+ exit
459
+ end
460
+
461
+ opts.on_tail("-x", "--exit", "Exit app") do |_value|
462
+ exit
463
+ end
464
+
465
+ opts.on_tail("-0", "Show configuration") do |_v|
466
+ options_finalize! options
467
+ puts options.to_yaml
468
+ end
469
+ end
470
+
471
+ option_parser.load # filename defaults to basename of the program without suffix in a directory ~/.options
472
+ option_parser.environment # env defaults to the basename of the program.
473
+ option_parser.parse! # (into: options)
474
+ options_finalize! options
475
+
476
+ ## process
477
+ #
478
+ loop do # once
479
+ mp = MarkParse.new options
480
+ options.merge!(
481
+ {
482
+ approve: true,
483
+ bash: true,
484
+ display: true,
485
+ execute: true,
486
+ prompt: "Execute",
487
+ struct: true
488
+ }
489
+ )
490
+
491
+ ## show
492
+ #
493
+ if options[:list_docs]
494
+ fout mp.find_files
495
+ break
496
+ end
497
+
498
+ if options[:list_blocks]
499
+ fout (mp.find_files.map do |file|
500
+ mp.make_block_labels(mdfilename: file, struct: true)
501
+ end).flatten(1).to_yaml
502
+ break
503
+ end
504
+
505
+ ## process
506
+ #
507
+ mp.select_block(bash: true, struct: true) if options[:mdfilename]
508
+
509
+ =begin
510
+ # rescue ArgumentError => e
511
+ # puts "User abort: #{e}"
512
+
513
+ # rescue StandardError => e
514
+ # puts "ERROR: #{e}"
515
+ # raise StandardError, e
516
+
517
+ # ensure
518
+ # exit
519
+ =end
520
+ break unless false
521
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownExec
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "markdown_exec/version"
4
+
5
+ module MarkdownExec
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+
9
+ class MDExec
10
+ def self.echo(str = "")
11
+ "#{str}#{str}"
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_exec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fareed Stevenson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Execute shell blocks in markdown files. Name blocks and require named
14
+ blocks.
15
+ email:
16
+ - fareed@phomento.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/mde
30
+ - bin/setup
31
+ - lib/markdown_exec.rb
32
+ - lib/markdown_exec/version.rb
33
+ homepage: https://rubygems.org/gems/markdown_executor
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://rubygems.org/gems/markdown_executor
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.2.32
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Execute shell blocks in markdown files.
57
+ test_files: []