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



54
55
56
# File 'lib/slippery/rake_tasks.rb', line 54

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

#defineObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/slippery/rake_tasks.rb', line 58

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") { |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

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



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

def markdown_to_hexp(infile, options = {})
  doc = Slippery::Document.new(infile.read)
  doc = Slippery::Presentation.new(doc, @options.merge(options))

  doc.process(*processors.reject {|pr| options[:skip_self_contained] && pr == Slippery::Processors::SelfContained})
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



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

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

#self_containedObject



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

def self_contained
  processors << Slippery::Processors::SelfContained
end

#title(title) ⇒ Object



48
49
50
51
52
# File 'lib/slippery/rake_tasks.rb', line 48

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