Class: MDRubyEval

Inherits:
Object
  • Object
show all
Defined in:
lib/md_ruby_eval.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_path, output_path, environment, indentation, line_length, verbose) ⇒ MDRubyEval

Returns a new instance of MDRubyEval.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/md_ruby_eval.rb', line 7

def initialize(input_path, output_path, environment, indentation, line_length, verbose)
  @input_path  = input_path
  @output_path = output_path
  @environment = environment
  @output      = ''
  @indentation = indentation
  @line_length = line_length
  @verbose     = verbose

  @last_id   = 1
  @known_ids = Hash.new { |h, k| h[k] = format('%06x', @last_id += 1) }
  @too_long  = []

  process_file input_path
end

Instance Method Details

#evaluate(code, line) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/md_ruby_eval.rb', line 23

def evaluate(code, line)
  puts code if @verbose
  start = Time.now
  eval(code, @environment, @input_path, line)
ensure
  took   = Time.now - start
  output = format "\e[1m== %5.2f seconds   %s:%d\e[22m", took, @input_path, line
  puts output if @verbose
  @too_long << [took, code + output] if took > 0.1
end

#process_file(input_path) ⇒ Object



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/md_ruby_eval.rb', line 85

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

  to_print = @too_long.sort_by { |took, _| -took }[0..10]
  if to_print.size > 0
    puts "#{to_print.size} longest evaluations:"
    to_print.each { |_, out| puts out }
  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



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
70
71
72
73
74
75
76
77
78
# File 'lib/md_ruby_eval.rb', line 34

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.lines.last.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



81
82
83
# File 'lib/md_ruby_eval.rb', line 81

def stabilize_object_ids(output)
  output.gsub(/(#<[\w:_]+0x)([0-9a-f]{16})/) { $1 + @known_ids[$2] }
end