Class: MarkdownIrbRunner
- Inherits:
-
MarkdownHelper
- Object
- MarkdownHelper
- MarkdownIrbRunner
- Defined in:
- lib/markdown_helper/markdown_irb_runner.rb
Constant Summary collapse
- CommentPrefix =
'#run_irb '
Constants inherited from MarkdownHelper
Instance Attribute Summary
Attributes inherited from MarkdownHelper
Instance Method Summary collapse
-
#make_irb_input(template_file_path) ⇒ Object
Comment out all lines except the irb snippets.
-
#make_irb_output(irb_input) ⇒ Object
Run irb over the prepared file.
-
#make_markdown(irb_output) ⇒ Object
Uncomment the text.
-
#run_irb(template_file_path, markdown_file_path) ⇒ Object
Run Ruby Interactive Shell (irb) for each embedded snippet that begins with ‘“`#run_irb’ and end with ‘“`’.
Methods inherited from MarkdownHelper
comment, #generate_file, git_clone_dir_path, #include, #initialize, path_in_project
Constructor Details
This class inherits a constructor from MarkdownHelper
Instance Method Details
#make_irb_input(template_file_path) ⇒ Object
Comment out all lines except the irb snippets.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/markdown_helper/markdown_irb_runner.rb', line 21 def make_irb_input(template_file_path) irb_lines = [] in_irb_block = false source_lines = File.readlines(template_file_path) source_lines.each do |source_line| source_line.chomp! if source_line == '```#run_irb' # Begin irb snippet; put commented-out markdown pragma. in_irb_block = true irb_lines.push(CommentPrefix + '```ruby') elsif source_line == '```' # End irb snippet; put commented-out markdown pragma. in_irb_block = false irb_lines.push(CommentPrefix + '```') else if in_irb_block irb_lines.push(source_line) else irb_lines.push(CommentPrefix + source_line) end end end irb_lines.push('') unless irb_lines.last.empty? irb_lines.join("\n") end |
#make_irb_output(irb_input) ⇒ Object
Run irb over the prepared file.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/markdown_helper/markdown_irb_runner.rb', line 49 def make_irb_output(irb_input) Dir.mktmpdir do |dir| Dir.chdir(dir) do File.write('irb_input', irb_input) command = 'irb --noecho --noprompt irb_input > irb_output' system(command) File.read('irb_output') end end end |
#make_markdown(irb_output) ⇒ Object
Uncomment the text.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/markdown_helper/markdown_irb_runner.rb', line 62 def make_markdown(irb_output) output_lines = [] irb_lines = irb_output.split("\n") irb_lines.each_with_index do |irb_line, i| irb_line.chomp! next if (i == 0) && (irb_line == 'Switch to inspect mode.') output_line = irb_line.sub(CommentPrefix, '') output_lines.push(output_line) end output_lines.push('') unless output_lines.last.nil? || output_lines.last.empty? output_lines.join("\n") end |
#run_irb(template_file_path, markdown_file_path) ⇒ Object
Run Ruby Interactive Shell (irb) for each embedded snippet that begins with ‘“`#run_irb’ and end with ‘“`’. The irb output replaces the entire sequence, and is preceded by ‘“`ruby’ and followed by ‘“`’.
12 13 14 15 16 17 |
# File 'lib/markdown_helper/markdown_irb_runner.rb', line 12 def run_irb(template_file_path, markdown_file_path) irb_input = make_irb_input(template_file_path) irb_output = make_irb_output(irb_input) markdown = make_markdown(irb_output) File.write(markdown_file_path, markdown) end |