md-ruby-eval 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dec15b3f48af5f743b96c4a8c8afceee6f6f6819cc16fad51a91dfd06e531462
4
- data.tar.gz: ab6707c2f223488c986bac361c597e496123b5af8f935af8567971af0cb83e35
3
+ metadata.gz: 1056df9d4b2215292650e16439ab7d8455e5df82c804a93c38e419613bd301c7
4
+ data.tar.gz: 7f9b54997a435172f312f88773558bcc71c687d02f2c9013271774a9ea53b1e7
5
5
  SHA512:
6
- metadata.gz: bf6f4607c2f0dafcc31cce0e44753fafaa511cf6101ff93e3cc4b2cbadc136bd0765805efc65437bfc14cd2512a14cc24981669a68961b0aedd509cb457a3848
7
- data.tar.gz: 01d08f8118ec12274801803b2be8591e472af988849a4b13c6966131e229f2089b721cd8db760eb918fe3d1567718cf83aeaa01a2e7fde3b8706782295c4b789
6
+ metadata.gz: fcdce3c13297d80b29945804b71b3ca3663a2c922ebf5b585cbda351a3c3d11ff0c30c2bb4b4274d0d36ec32dbfdfca78fba07896058237b2636fd1241a13e73
7
+ data.tar.gz: f6a973e077e4016d385e5b90ca8bdeb3085b1685748e9a518a0afde6da8a268a9edebbc9fffcd3a93a0a6fa66ab2bc479ec0128c17671c4f8dac802d9c4d982f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -7,6 +7,7 @@ to_require = []
7
7
  indentation = 40
8
8
  line_length = 79
9
9
  auto = false
10
+ verbose = false
10
11
  dir = Dir.pwd
11
12
 
12
13
  OptionParser.new do |opts|
@@ -36,6 +37,11 @@ OptionParser.new do |opts|
36
37
  'Automatic mode. Finds `*.in.md` files outputting `*.out.md` files') do |v|
37
38
  auto = true
38
39
  end
40
+
41
+ opts.on('--[no-]verbose',
42
+ 'Print code chunks right before their execution') do |v|
43
+ verbose = true
44
+ end
39
45
  end.parse!
40
46
 
41
47
  Dir.chdir dir do
@@ -50,7 +56,7 @@ Dir.chdir dir do
50
56
  puts "using: #{init_path}"
51
57
  load init_path
52
58
  end
53
- MDRubyEval.new input_path, input_path.gsub(/(\.in)?\.md$/, '.out.md'), binding, indentation, line_length
59
+ MDRubyEval.new input_path, input_path.gsub(/(\.in)?\.md$/, '.out.md'), binding, indentation, line_length, verbose
54
60
  end
55
61
 
56
62
  Process.wait pid
@@ -58,7 +64,7 @@ Dir.chdir dir do
58
64
  else
59
65
  input_path, output_path = ARGV.map { |p| File.expand_path p }
60
66
  raise 'no input path' unless input_path && File.exist?(input_path)
61
- MDRubyEval.new input_path, output_path, binding, indentation, line_length
67
+ MDRubyEval.new input_path, output_path, binding, indentation, line_length, verbose
62
68
  end
63
69
  end
64
70
 
@@ -4,22 +4,31 @@ require 'optparse'
4
4
 
5
5
  class MDRubyEval
6
6
 
7
- def initialize(input_path, output_path, environment, indentation, line_length)
7
+ def initialize(input_path, output_path, environment, indentation, line_length, verbose)
8
8
  @input_path = input_path
9
9
  @output_path = output_path
10
10
  @environment = environment
11
11
  @output = ''
12
12
  @indentation = indentation
13
13
  @line_length = line_length
14
+ @verbose = verbose
14
15
 
15
16
  @last_id = 1
16
17
  @known_ids = Hash.new { |h, k| h[k] = format('%06x', @last_id += 1) }
18
+ @too_long = []
17
19
 
18
20
  process_file input_path
19
21
  end
20
22
 
21
23
  def evaluate(code, line)
24
+ puts code if @verbose
25
+ start = Time.now
22
26
  eval(code, @environment, @input_path, line)
27
+ ensure
28
+ took = Time.now - start
29
+ output = format "\e[1m== %5.2f seconds %s:%d\e[22m", took, @input_path, line
30
+ puts output if @verbose
31
+ @too_long << [took, code + output] if took > 0.1
23
32
  end
24
33
 
25
34
  def process_ruby(part, start_line)
@@ -42,7 +51,7 @@ class MDRubyEval
42
51
  output = ''
43
52
  chunk_lines.each do |chunk, lines|
44
53
  result = evaluate(chunk, line_count)
45
- if chunk.strip.empty? || chunk.include?('#')
54
+ if chunk.strip.empty? || chunk.lines.last.include?('#')
46
55
  output << (chunk.end_with?("#\n") ? chunk[0..-3] + "\n" : chunk)
47
56
  else
48
57
  pre_lines = chunk.lines.to_a
@@ -109,6 +118,12 @@ class MDRubyEval
109
118
  line_count += part.lines.size
110
119
  end
111
120
 
121
+ to_print = @too_long.sort_by { |took, _| -took }[0..10]
122
+ if to_print.size > 0
123
+ puts "#{to_print.size} longest evaluations:"
124
+ to_print.each { |_, out| puts out }
125
+ end
126
+
112
127
  puts "writing: #{@output_path}"
113
128
  File.write(@output_path, @output)
114
129
  rescue => ex
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md-ruby-eval
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petr Chalupa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-18 00:00:00.000000000 Z
11
+ date: 2019-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry