class Bookwatch::CodeExampleReader

Attributes

fs[R]
out[R]

Public Class Methods

new(streams, fs) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 9
def initialize(streams, fs)
  @out = streams[:out]
  @fs = fs
end

Public Instance Methods

get_snippet_and_language_at(marker, working_copy) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 14
def get_snippet_and_language_at(marker, working_copy)
  if working_copy.available?
    process_snippet(marker, working_copy)
  else
    out.puts("  skipping (not found) #{working_copy.full_name}")
    ''
  end
end

Private Instance Methods

cannot_scan() click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 91
def cannot_scan
  []
end
find_text(start_path, pattern) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 67
def find_text(start_path, pattern)
  fs.find_files_recursively(start_path).
    lazy.
    select {|path| fs.file_exist?(path) }.
    map {|path| fs.read(path) }.
    map {|contents|
      begin
        contents.scan(pattern)
      rescue ArgumentError => e
        cannot_scan
      end
    }.
    map(&:first).
    detect ->{""} {|text| text}
end
language_pattern_for(marker) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 87
def language_pattern_for(marker)
  /code_snippet #{Regexp.escape(marker)} start (\w+)/
end
pattern_for(marker) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 83
def pattern_for(marker)
  /code_snippet #{Regexp.escape(marker)} start.*code_snippet #{Regexp.escape(marker)} end/m
end
process_snippet(marker, working_copy) click to toggle source
# File lib/bookwatch/code_example_reader.rb, line 55
def process_snippet(marker, working_copy)
  snippet = Snippet.new(
    find_text(working_copy.path, pattern_for(marker)),
    language_pattern_for(marker)
  )
  if snippet.valid?
    [snippet.content, snippet.language]
  else
    raise InvalidSnippet.new(working_copy.full_name, marker)
  end
end