Class: Slippery::RakeTasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/slippery/rake_tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :slippery, &blk) ⇒ RakeTasks

Returns a new instance of RakeTasks.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/slippery/rake_tasks.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/slippery/rake_tasks.rb', line 7

def options
  @options
end

#presentationsObject

Returns the value of attribute presentations.



7
8
9
# File 'lib/slippery/rake_tasks.rb', line 7

def presentations
  @presentations
end

#processorsObject

Returns the value of attribute processors.



7
8
9
# File 'lib/slippery/rake_tasks.rb', line 7

def processors
  @processors
end

Instance Method Details

#add_highlighting(style = Slippery::Processors::AddHighlight::DEFAULT_STYLE, version = Slippery::Processors::AddHighlight::DEFAULT_VERSION) ⇒ Object



64
65
66
# File 'lib/slippery/rake_tasks.rb', line 64

def add_highlighting(style = Slippery::Processors::AddHighlight::DEFAULT_STYLE, version = Slippery::Processors::AddHighlight::DEFAULT_VERSION)
  processors << Slippery::Processors::AddHighlight.new(style, version)
end

#call_asset_packer(doc) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/slippery/rake_tasks.rb', line 50

def call_asset_packer(doc)
  AssetPacker::Processor::Local.new(
    @infile.to_s,
    @infile.dirname.join('assets'),
    @infile
  ).(doc)
end

#defineObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/slippery/rake_tasks.rb', line 68

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", markdown_to_hexp(path).to_html)
        end
      end
    end

    namespace :watch do
      presentation_names.each do |name, path|
        files = markdown_to_hexp(path, skip_self_contained: true).select('link,script').map {|link| link.attr('href') || link.attr('src')}.compact
        files = files.select {|f| File.exist?(f)}

        desc "watch #{name} for changes"
        WatchTask.new(name, [path.to_s, *files]) do
          dest = Tempfile.new("#{name}.html")
          File.open("#{name}.html", 'w+') { |src| FileUtils.copy_stream(src, dest) }
          dest.close
          Rake::Task["#{@name}:build:#{name}"].execute
          puts "="*60
          print `diff -u #{dest.path} #{name}.html | cut -c1-150` if File.exist? "#{name}.html"
        end
      end
    end
  end
end

#include_assetsObject Also known as: self_contained



45
46
47
# File 'lib/slippery/rake_tasks.rb', line 45

def include_assets
  processors << method(:call_asset_packer)
end

#markdown_to_hexp(infile, options = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/slippery/rake_tasks.rb', line 29

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

#presentation_namesObject



25
26
27
# File 'lib/slippery/rake_tasks.rb', line 25

def presentation_names
  presentations.map {|path| [ path.basename(path.extname), path ] }
end

#processor(selector, &blk) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/slippery/rake_tasks.rb', line 37

def processor(selector, &blk)
  processors << ->(node) do
    node.replace(selector) do |node|
      instance_exec(node, &blk)
    end
  end
end

#title(title) ⇒ Object



58
59
60
61
62
# File 'lib/slippery/rake_tasks.rb', line 58

def title(title)
  processor 'head' do |head|
    head <<= H[:title, title]
  end
end