Class: Showoff::Compiler::Downloads

Inherits:
Object
  • Object
show all
Defined in:
lib/showoff/compiler/downloads.rb

Overview

adds file download link processing

Class Method Summary collapse

Class Method Details

.enableFiles(index) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/showoff/compiler/downloads.rb', line 73

def self.enableFiles(index)
  record = Showoff::State.getAtIndex(:downloads, index)
  return unless record

  record[:enabled] = true
  Showoff::State.setAtIndex(:downloads, index, record)
end

.getFiles(index) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/showoff/compiler/downloads.rb', line 81

def self.getFiles(index)
  record = Showoff::State.getAtIndex(:downloads, index)

  if (record and record[:enabled])
    record[:slides]
  else
    []
  end
end

.pushFile(index, current, source, file) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/showoff/compiler/downloads.rb', line 64

def self.pushFile(index, current, source, file)
  record = Showoff::State.getAtIndex(:downloads, index) || {}
  record[:enabled] ||= false
  record[:slides]  ||= []
  record[:slides] << {:slidenum => current, :source => source, :file => file}

  Showoff::State.setAtIndex(:downloads, index, record)
end

.scanForFiles!(doc, options) ⇒ Nokogiri::HTML::DocumentFragment

TODO:

Should .download change meaning to ‘make available on this slide’?

Scan for file download links and move them to the state storage.

Parameters:

  • doc (Nokogiri::HTML::DocumentFragment)

    The slide document

Returns:

  • (Nokogiri::HTML::DocumentFragment)

    The slide DOM with download links removed.

See Also:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/showoff/compiler/downloads.rb', line 16

def self.scanForFiles!(doc, options)
  current = Showoff::State.get(:slide_count)
  doc.search('p.download').each do |container|
    links = container.text.gsub(/^\.download ?/, '')
    links.split("\n").each do |line|
      file, modifier = line.split
      modifier ||= 'next' # @todo Is this still the proper default?

      case modifier
      when 'a', 'all', 'always', 'now'
        self.pushFile(0, current, options[:name], file)
      when 'p', 'prev', 'previous'
        self.pushFile(current-1, current, options[:name], file)
      when 'c', 'curr', 'current'
        self.pushFile(current, current, options[:name], file)
      when 'n', 'next'
        self.pushFile(current+1, current, options[:name], file)
      end
    end

    container.remove
  end

  doc
end