class PFM::CodeBlockInclude

Public Class Methods

new(options={}) click to toggle source
# File lib/pfm.rb, line 13
def initialize(options={})
  @regex = %r{^```([^\s#]+)(#L(\S+))?\s*```$}
end

Public Instance Methods

process(lines) click to toggle source
# File lib/pfm.rb, line 18
def process(lines)
  # TODO: handle URLs
  out = []
  lines.each do |line|
    if md = @regex.match(line)
      _full, source_path, badline, line_spec = md.to_a
      if line_spec
        start, stop = line_spec.split("-").map { |s| s.to_i}
      else
        start = 1
      end

      source_path = File.expand_path(source_path).strip
      extension = File.extname(source_path).slice(1..-1)
      out << "```#{extension}"

      embed = []
      File.open(source_path, "r") do |source|
        source.each_line do |line|
          embed << line
        end
      end
      start -= 1
      if stop
        stop -=1
      else
        stop = embed.size - 1
      end
      selection = embed.slice(start..stop)
      while selection.last =~ /^\s+$/
        selection.pop
      end
      out << selection.join()
      out << "```\n"
    else
      out << line
    end
  end
  out
end