class Snippeteer::Writer

Filesystem interface for the document reader. Reads files and writes/runs extracted snippets.

Attributes

path[R]
scanners[R]

Public Class Methods

new(path, scanners) click to toggle source

Takes a filename and a list of scanners. Scanners are expected to have a `scan' method that takes a string and returns a list of Snippets.

# File lib/snippeteer.rb, line 13
def initialize(path, scanners)
  @path, @scanners = path, scanners
end

Public Instance Methods

run_sources(cleanup = true) click to toggle source

Write extracted snippets, then execute them if an execution command is given for their language. By default, removes the written files after execution. Returns a list of filename, exit status pairs.

# File lib/snippeteer.rb, line 32
def run_sources(cleanup = true)
  srcs = write_sources.each_with_object([]) do |written, run|
    lang, srcfile = written
    if lang.exec
      run << [srcfile, system("#{lang.exec} #{srcfile}")]
      FileUtils.rm srcfile if cleanup
    end
  end
end
sources() click to toggle source
# File lib/snippeteer.rb, line 42
def sources
  Reader.new(File.read(@path), @scanners).sources
end
write_sources() click to toggle source

Write extracted snippets to files according to their language. Returns a hash mapping language to filename.

# File lib/snippeteer.rb, line 19
def write_sources
  basename = File.basename path
  sources.each_with_object({}) do |snip, written|
    lang, src = snip
    srcname = basename + lang.ext
    File.open(srcname, 'w') {|srcfile| srcfile.write src}
    written[lang] = srcname
  end
end