class Unobtainium::Kramdown::Driver

Driver implementation using kramdown (and open-uri).

Attributes

content[R]

@return (String) The raw page content, or nil

meta[R]

@return (Hash) Any page metadata, or an empty Hash

options[RW]

@return (::Kramdown::Options) parser options for all subsequent

requests.

Public Class Methods

create(_, _) click to toggle source

Create and return a driver instance

# File lib/unobtainium-kramdown/driver.rb, line 41
def create(_, _)
  driver = ::Unobtainium::Kramdown::Driver.new
  return driver
end
ensure_preconditions(_, _) click to toggle source

Ensure that the driver's preconditions are fulfilled.

# File lib/unobtainium-kramdown/driver.rb, line 30
def ensure_preconditions(_, _)
  require 'kramdown'
  require 'open-uri'
rescue LoadError => err
  raise LoadError, "#{err.message}: you need to add "\
        "'kramdown' to your Gemfile to use this driver!",
        err.backtrace
end
matches?(label) click to toggle source

Return true if the given label matches this driver implementation, false otherwise.

# File lib/unobtainium-kramdown/driver.rb, line 24
def matches?(label)
  return :kramdown == label.to_sym
end
new() click to toggle source

Private initialize to force use of Driver#create.

# File lib/unobtainium-kramdown/driver.rb, line 117
def initialize
  reset

  @options = ::Kramdown::Options.defaults
end

Public Instance Methods

goto(uri) click to toggle source

“Go to” the given URI, i.e. open it and retain contents.

# File lib/unobtainium-kramdown/driver.rb, line 49
def goto(uri)
  reset

  # Fetch content
  open(uri) do |f|
    @meta[:uri] = uri.dup
    if f.respond_to?(:meta)
      @meta[:headers] = f.meta.dup
      @meta[:status] = f.status.dup
      @meta[:base_uri] = f.base_uri.dup
    end

    if f.respond_to?(:metas)
      @meta[:split_headers] = f.metas.dup
    end

    @content = f.read
  end

  # Pass content to Kramdown
  @parsed = ::Kramdown::Document.new(@content, @options)
end
method_missing(meth, *args, &block) click to toggle source

Map any missing method to kramdown

Calls superclass method
# File lib/unobtainium-kramdown/driver.rb, line 104
def method_missing(meth, *args, &block)
  if not @parsed.nil?
    if @parsed.respond_to?(meth) or meth.to_s =~ /^to_.+/
      return @parsed.send(meth.to_s, *args, &block)
    end
  end
  return super
end
respond_to_missing?(meth, include_private = false) click to toggle source

Map any missing method to kramdown

Calls superclass method
# File lib/unobtainium-kramdown/driver.rb, line 87
def respond_to_missing?(meth, include_private = false)
  if not @parsed.nil?
    # Kramdown::Document does not implement respond_to*?, so we can't
    # be entirely certain this works...
    if @parsed.respond_to?(meth, include_private)
      return true
    end
    # ... therefore assume to_* to be handled by Kramdown::Document
    if meth.to_s =~ /^to_.+/
      return true
    end
  end
  return super
end

Private Instance Methods

reset() click to toggle source

Clear all data

# File lib/unobtainium-kramdown/driver.rb, line 125
def reset
  @content = nil
  @meta = {}
  @parsed = nil
end