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
24
# 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 = :unused) ⇒ Object



84
85
86
# File 'lib/slippery/rake_tasks.rb', line 84

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

#asset_packer(infile) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/slippery/rake_tasks.rb', line 65

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

#defineObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/slippery/rake_tasks.rb', line 88

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
            if `which pup` =~ /pup/
              print `bash -c 'diff -u <(cat #{tmpfile.path} | pup) <(cat #{name}.html | pup) | cut -c1-180'`
            else
              print `diff -u #{tmpfile.path} #{name}.html | cut -c1-180`
            end
          end
        end
      end
    end
  end
end

#js_options(options) ⇒ Object



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

def js_options(options)
  @options.merge!(options)
end

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



39
40
41
42
43
44
# 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_assetsObject Also known as: self_contained



56
57
58
# File 'lib/slippery/rake_tasks.rb', line 56

def pack_assets
  @options[:include_assets] = true
end

#pack_assets?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/slippery/rake_tasks.rb', line 61

def pack_assets?
  !!@options[:include_assets]
end

#presentation_namesObject



35
36
37
# File 'lib/slippery/rake_tasks.rb', line 35

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

#processor(selector, &blk) ⇒ Object Also known as: replace, transform



46
47
48
49
50
51
52
# 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

#title(title) ⇒ Object



78
79
80
81
82
# File 'lib/slippery/rake_tasks.rb', line 78

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

#type(type) ⇒ Object Also known as: type=



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

def type(type)
  @options[:type] = type
end

#watch(name, files, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/slippery/rake_tasks.rb', line 137

def watch(name, files, &block)
  listener = Listen.to('.', :only => /#{files.join('|')}/, &block)

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