class Slippery::RakeTasks

Attributes

name[R]
options[RW]
presentations[RW]
processors[RW]

Public Class Methods

new(name = :slippery) { |self| ... } click to toggle source
# File lib/slippery/rake_tasks.rb, line 9
def initialize(name = :slippery, &blk)
  @name = name
  @presentations = Pathname.glob('*.md')
  @options = {}
  @processors = []

  if block_given?
    if blk.arity == 0
      self.instance_eval(&blk)
    else
      yield self
    end
  end

  define
end

Public Instance Methods

add_highlighting(style = Slippery::Processors::AddHighlight::DEFAULT_STYLE, version = Slippery::Processors::AddHighlight::DEFAULT_VERSION) click to toggle source
# File lib/slippery/rake_tasks.rb, line 83
def add_highlighting(style = Slippery::Processors::AddHighlight::DEFAULT_STYLE, version = Slippery::Processors::AddHighlight::DEFAULT_VERSION)
  processors << Slippery::Processors::AddHighlight.new(style, version)
end
asset_packer(infile) click to toggle source
# File lib/slippery/rake_tasks.rb, line 64
def asset_packer(infile)
  infile = Pathname(infile)
  if pack_assets?
    AssetPacker::Processor::Local.new(
      infile.to_s,
      infile.dirname.join('assets'),
      infile
    )
  else
    ->(i) { i }
  end
end
define() click to toggle source
# File lib/slippery/rake_tasks.rb, line 87
def define
  namespace @name do
    desc "build all"
    task :build => presentation_names.map(&:first).map {|name| "#{@name}:build:#{name}"}

    namespace :build do
      presentation_names.each do |name, path|
        desc "build #{name}"
        task name do
          File.write("#{name}.html", asset_packer(path).((markdown_to_hexp(path))).to_html(html5: true))
        end
      end
    end

    namespace :watch do
      presentation_names.each do |name, path|

        desc "watch #{name} for changes"
        task name do
          asset_files = markdown_to_hexp(path)
            .select('link,script')
            .map {|link| link.attr('href') || link.attr('src')}
            .compact
            .select {|uri| URI(uri).scheme == 'file' || URI(uri).scheme == '' || URI(uri).scheme.nil? }
            .map {|uri| Pathname(URI(uri).path) }
            .select(&:exist?)
            .map(&:to_s)

          watch(name, [path.to_s, *asset_files]) do
            target = Pathname("#{name}.html")
            before = target.exist? ? target.read : ''
            Rake::Task["#{@name}:build:#{name}"].execute
            puts "="*60

            tmpfile = Tempfile.new("#{name}.html")
            tmpfile << before
            tmpfile.close
            print `diff -u #{tmpfile.path} #{name}.html | cut -c1-150`
          end
        end
      end
    end
  end
end
js_options(options) click to toggle source
# File lib/slippery/rake_tasks.rb, line 31
def js_options(options)
  @options.merge!(options)
end
markdown_to_hexp(infile, options = {}) click to toggle source
# File lib/slippery/rake_tasks.rb, line 39
def markdown_to_hexp(infile, options = {})
  @infile = infile
  doc = Slippery::Document.new(infile.read)
  doc = Slippery::Presentation.new(doc, @options.merge(options))
  doc.process(*processors)
end
pack_assets() click to toggle source
# File lib/slippery/rake_tasks.rb, line 55
def pack_assets
  @options[:include_assets] = true
end
Also aliased as: self_contained
pack_assets?() click to toggle source
# File lib/slippery/rake_tasks.rb, line 60
def pack_assets?
  !!@options[:include_assets]
end
presentation_names() click to toggle source
# File lib/slippery/rake_tasks.rb, line 35
def presentation_names
  presentations.map {|path| [ path.basename(path.extname), path ] }
end
processor(selector, &blk) click to toggle source
# File lib/slippery/rake_tasks.rb, line 46
def processor(selector, &blk)
  processors << ->(node) do
    node.replace(selector) do |node|
      instance_exec(node, &blk)
    end
  end
end
Also aliased as: replace
replace(selector, &blk)
Alias for: processor
self_contained()
Alias for: pack_assets
title(title) click to toggle source
# File lib/slippery/rake_tasks.rb, line 77
def title(title)
  processor 'head' do |head|
    head <<= H[:title, title]
  end
end
type(type) click to toggle source
# File lib/slippery/rake_tasks.rb, line 26
def type(type)
  @options[:type] = type
end
Also aliased as: type=
type=(type)
Alias for: type
watch(name, files, &block) click to toggle source
# File lib/slippery/rake_tasks.rb, line 132
def watch(name, files, &block)
  listener = Listen.to('.', :only => /#{files.join('|')}/, &block)

  at_exit do
    block.call
    listener.start # not blocking
    sleep
  end
end