class LiterateRuby::File
This class is the file class for literate ruby. It extends Ruby's File
class.
@attr base [String] the base name of the file
Attributes
base[RW]
Public Class Methods
new(*args)
click to toggle source
The constructor for LiterateRuby::File
@param *args the arguments that are passed to super.
Calls superclass method
# File lib/literate-ruby/file.rb, line 14 def initialize(*args) super(*args) @base = File.basename(path, '.*') end
Public Instance Methods
parse()
click to toggle source
This method parses a literate ruby file, (which should have the extension .lrb
), to a normal ruby file (which will have the extension .rb
).
@return [String] the file_path to the normal ruby file.
# File lib/literate-ruby/file.rb, line 24 def parse `touch #{base}.rb` file = read File.open("#{base}.rb", 'w') do |f| file.each_line do |line| f.write(line[2..-1]) if line[0..1] == '> ' end end "#{base}.rb" end
to_eval_markdown()
click to toggle source
# File lib/literate-ruby/file.rb, line 47 def to_eval_markdown f = File.new("#{base}.md", 'w+') tmp_f = File.new("#{base}_tmp.rb", 'w+') block = false read.each_line { |l| block = to_eval_helper(l, f, tmp_f, block) } `yes | rm #{base}_tmp.rb` "#{base}.md" end
to_markdown()
click to toggle source
This method parses a literate ruby file, (which should have the extension .lrb
), to a markdown file (which will have the extension .md
).
@return [String] the file_path to the markdown file.
# File lib/literate-ruby/file.rb, line 40 def to_markdown f = File.new("#{base}.md", 'w') block = false read.each_line { |l| block = to_markdown_helper(l, f, block) } "#{base}.md" end
Private Instance Methods
eval_string(tmp_f, f)
click to toggle source
# File lib/literate-ruby/file.rb, line 78 def eval_string(tmp_f, f) s = '' IO.popen('ruby', 'w+') do |i| i.write(tmp_f.read) i.close_write s = i.read end f.puts " #{s}" false end
to_eval_helper(l, f, tmp_f, block)
click to toggle source
# File lib/literate-ruby/file.rb, line 67 def to_eval_helper(l, f, tmp_f, block) if l[0..1] == '> ' && block then tmp_f.write(l[2..-1].to_s) elsif block block = eval_string(tmp_f, f) $stdout = STDOUT elsif l =~ /^\s+$/ then block = !f.write(l).nil? else f.write(l) end block end
to_markdown_helper(l, f, block)
click to toggle source
# File lib/literate-ruby/file.rb, line 58 def to_markdown_helper(l, f, block) if l[0..1] == '> ' && block then f.write(" #{l[2..-1]}") elsif block then block = !f.write("\n#{l}") elsif l =~ /^\s+$/ then block = !f.write(l).nil? else f.write(l) end block end