Module: LiterateRuby

Defined in:
lib/literate_ruby.rb,
lib/literate_ruby/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object

See comment in the test file def self.require(path)

full_path = path + ".lrb"
if $".include?(full_path)
  false
else
  if load(full_path)
    $" << full_path
  else
    raise LoadError
  end
end

end



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/literate_ruby.rb', line 16

def self.load(path)
  lines = parse(File.new(path))

  lines.find_all {|(type, line_group)| type == :code && !line_group.empty?}.each do |(_, line_group)|
    Kernel.eval(
      line_group.map {|(line_no, line)| line}.join(""), 
      TOPLEVEL_BINDING, 
      path, 
      line_group[0][0]
    )
  end

  lines
end

.parse(io) ⇒ Object



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
70
71
72
73
74
75
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
118
119
120
121
122
123
# File 'lib/literate_ruby.rb', line 31

def self.parse(io)
  state = :open
  lines = []
  buffer = nil
  
  io.readlines.each_with_index do |line, i|
    case line
    when /^>/
      clean_line = line.sub(/^>/, "")
      case state
      when :open
        state = :in_code
        buffer = []
        buffer << [i + 1, clean_line]
      when :in_code
        buffer << [i + 1, clean_line]
      when :in_code_block
        buffer << [i + 1, clean_line]
      when :in_data
        lines << [:data, buffer]
        buffer = []
        
        state = :in_code
        buffer << [i + 1, clean_line]
      end
    when /^=begin_code/
      case state
      when :open
        buffer = []
      when :in_code
        lines << [:code, buffer]
        buffer = []
      when :in_code_block
        buffer << [i + 1, line]
      when :in_data
        lines << [:data, buffer]
        buffer = []
      end
      state = :in_code_block
    when /^=end_code/
      case state
      when :open
        buffer = []
        state = :in_data
      when :in_code
        lines << [:code, buffer]
        buffer = []
        
        state = :in_data
      when :in_code_block
        lines << [:code, buffer]
        buffer = []
        
        state = :in_data
      when :in_data
        # no-op
      end
    else
      case state
      when :open
        buffer = []
        state = :in_data
        buffer << [i + 1, line]
      when :in_code
        lines << [:code, buffer]
        buffer = []
        
        state = :in_data
        buffer << [i + 1, line]
      when :in_code_block
        buffer << [i + 1, line]
      when :in_data
        buffer << [i + 1, line]
      end
    end
  end

  case state
  when :open
    # no-op
  when :in_code
    lines << [:code, buffer] unless buffer.empty?
    buffer = nil
  when :in_code_block
    lines << [:code, buffer] unless buffer.empty?
    buffer = nil
  when :in_data
    lines << [:data, buffer] unless buffer.empty?
    buffer = nil
  end

  lines
end