Class: EmbeddedRubyProcessor

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

Instance Method Summary collapse

Instance Method Details

#between(re1, re2, paras) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/notroff/embedded.rb', line 131

def between(re1, re2, paras)
  state = :before_first
  paras.each do |para|
    if state == :before_first and re1 =~ para
      state = :after_first
      break if para =~ re2
    elsif state == :after_first
      if para =~ re2
        state = :after_second
        break
      end
      para[:included] = true
    end
  end
  raise "Couldnt find first pattern" if state == :before_first
  raise "Couldnt find second pattern" unless state == :after_second
  paras
end

#definition(def_re, include_body, paras) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/notroff/embedded.rb', line 150

def definition(def_re, include_body, paras)
  state = :before_def
  end_re = nil
  paras.each do |para|
    Logger.log para, (def_re =~ para)
    if state == :before_def and def_re =~ para
      para[:included] = true
      end_re = Regexp.new( "^#{' ' * para.string.indent_depth}end" )
      state = :after_def
    elsif state == :after_def and end_re =~ para.string
      para[:included] = true
      break
    elsif state == :after_def and include_body
      para[:included] = true
    end
  end
  paras
end

#embed(type, inc_all = true, lines) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/notroff/embedded.rb', line 98

def embed(type, inc_all=true, lines)
  lines.map! do |line| 
    result = Text.new(line.rstrip, :type => type)
    result[:original_text] = line.rstrip
    result[:included] = inc_all
    result
  end
  Logger.warn("Zero lines included!!") if lines.empty?
  lines
end

#ex(ruby_command, type = :code) ⇒ Object



123
124
125
# File 'lib/notroff/embedded.rb', line 123

def ex(ruby_command, type=:code)
  embed(type, eval(ruby_command).to_s.split("\n"))
end

#inc(path, inc_all = true, type = :code) ⇒ Object



115
116
117
# File 'lib/notroff/embedded.rb', line 115

def inc(path, inc_all=true, type=:code)
  embed(type, inc_all, File.readlines(path))
end

#indent(delta_indent, paragraphs) ⇒ Object

def clazz(name, include_body=true)

  ClassFilter.new(name, include_body)
end

def mod(name, include_body=true)
  ModuleFilter.new(name, include_body)
end


184
185
186
187
188
189
190
191
192
193
# File 'lib/notroff/embedded.rb', line 184

def indent(delta_indent, paragraphs)
  paragraphs.map do |p| 
    if delta_indent > 0
      p.string = (" " * delta_indent) + p.string
    elsif delta_indent < 0
      p.string.sub!( ' ' * delta_indent.abs, '' )
    end
  end
  paragraphs
end

#meth(method_name, include_body, paras) ⇒ Object



170
171
172
# File 'lib/notroff/embedded.rb', line 170

def meth(method_name, include_body, paras)
  definition(/^ *def +#{method_name}(\(|$| )/, include_body, paras)
end

#process(paragraphs) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/notroff/embedded.rb', line 78

def process(paragraphs)
  new_paragraphs = []
  paragraphs.each do |p|
    if p[:type] == :x
      Logger.log p
      results = process_command(p.string)
      new_paragraphs << results if results
    else
      new_paragraphs << p
    end
  end
  new_paragraphs.flatten
end

#process_command(ruby_expression) ⇒ Object



92
93
94
95
96
# File 'lib/notroff/embedded.rb', line 92

def process_command(ruby_expression)
  Logger.log "Ruby expression: #{ruby_expression}"
  paras = eval(ruby_expression, binding)
  paras.keep_if {|p| p[:included]}
end

#run(shell_command, type = :code) ⇒ Object



119
120
121
# File 'lib/notroff/embedded.rb', line 119

def run(shell_command, type=:code)
  embed(type, File.popen(shell_command).readlines)
end

#skip(re, paras) ⇒ Object



109
110
111
112
113
# File 'lib/notroff/embedded.rb', line 109

def skip(re, paras)
  paras.each do |para|
    para[:included] = false if (para =~ re)
  end
end

#stdinc(path, re1 = /##A/, re2 = /##Z/, type = :code) ⇒ Object



127
128
129
# File 'lib/notroff/embedded.rb', line 127

def stdinc(path, re1=/##A/, re2=/##Z/, type=:code)
  skip(/##X/, between(re1, re2, inc(path, false, type)))
end