Class: MDRubyEval
- Inherits:
-
Object
- Object
- MDRubyEval
- Defined in:
- lib/md_ruby_eval.rb
Instance Method Summary collapse
- #evaluate(code, line) ⇒ Object
-
#initialize(input_path, output_path, environment, indentation, line_length) ⇒ MDRubyEval
constructor
A new instance of MDRubyEval.
- #process_file(input_path) ⇒ Object
- #process_ruby(part, start_line) ⇒ Object
- #stabilize_object_ids(output) ⇒ Object
Constructor Details
#initialize(input_path, output_path, environment, indentation, line_length) ⇒ MDRubyEval
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/md_ruby_eval.rb', line 7 def initialize(input_path, output_path, environment, indentation, line_length) @input_path = input_path @output_path = output_path @environment = environment @output = '' @indentation = indentation @line_length = line_length @last_id = 1 @known_ids = Hash.new { |h, k| h[k] = format('%06x', @last_id += 1) } process_file input_path end |
Instance Method Details
#evaluate(code, line) ⇒ Object
21 22 23 |
# File 'lib/md_ruby_eval.rb', line 21 def evaluate(code, line) eval(code, @environment, @input_path, line) end |
#process_file(input_path) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/md_ruby_eval.rb', line 76 def process_file(input_path) puts "evaluating: #{input_path}" input = File.read(input_path) parts = input.split(/^(```\w*\n)/) # pp parts.map(&:lines) code_block = nil line_count = 1 parts.each do |part| if part =~ /^```(\w+)$/ code_block = $1 @output << part line_count += 1 next end if part =~ /^```$/ code_block = nil @output << part line_count += 1 next end if code_block == 'ruby' @output << process_ruby(part, line_count) line_count += part.lines.size next end @output << part line_count += part.lines.size end puts "writing: #{@output_path}" File.write(@output_path, @output) rescue => ex puts "#{ex} (#{ex.class})\n#{ex.backtrace * "\n"}" end |
#process_ruby(part, start_line) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/md_ruby_eval.rb', line 25 def process_ruby(part, start_line) lines = part.lines chunks = [] line = '' while !lines.empty? line += lines.shift if Pry::Code.complete_expression? line chunks << line line = '' end end raise unless line.empty? chunk_lines = chunks.map { |chunk| [chunk, [chunk.split($/).size, 1].max] } line_count = start_line output = '' chunk_lines.each do |chunk, lines| result = evaluate(chunk, line_count) if chunk.strip.empty? || chunk.include?('#') output << (chunk.end_with?("#\n") ? chunk[0..-3] + "\n" : chunk) else pre_lines = chunk.lines.to_a last_line = pre_lines.pop output << pre_lines.join if last_line =~ /\#$/ output << last_line.gsub(/\#$/, '') else inspected_result = stabilize_object_ids result.inspect if last_line.size < @indentation && inspected_result.size < @indentation output << "%-#{@indentation}s %s" % [last_line.chomp, "# => #{inspected_result}\n"] else PP.pp result, (buf = ''), @line_length buf = stabilize_object_ids buf inspect_lines = buf.lines output << last_line << "# => #{inspect_lines[0]}" << inspect_lines[1..-1].map { |l| format '# %s', l }.join end end end line_count += lines end output end |
#stabilize_object_ids(output) ⇒ Object
72 73 74 |
# File 'lib/md_ruby_eval.rb', line 72 def stabilize_object_ids(output) output.gsub(/(#<[\w:_]+0x)([0-9a-f]{16})/) { $1 + @known_ids[$2] } end |