Class: Showoff::Presentation

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

Defined Under Namespace

Classes: Section, Slide

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Presentation

Returns a new instance of Presentation.



9
10
11
12
13
14
15
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
41
# File 'lib/showoff/presentation.rb', line 9

def initialize(options)
  @options  = options
  @sections = Showoff::Config.sections.map do |name, files|
    Showoff::Presentation::Section.new(name, files)
  end

  # weird magic variables the presentation expects
  @baseurl   = nil # this doesn't appear to have ever been used
  @title     = Showoff::Config.get('name')    || I18n.t('name')
  @favicon   = Showoff::Config.get('favicon') || 'favicon.ico'
  @feedback  = Showoff::Config.get('feedback') # note: the params check is obsolete
  @pause_msg = Showoff::Config.get('pause_msg')
  @language  = Showoff::Locale.translations
  @edit      = Showoff::Config.get('edit') if options[:review]

  # invert the logic to maintain backwards compatibility of interactivity on by default
  @interactive = ! options[:standalone]

  # Load up the default keymap, then merge in any customizations
  keymapfile   = File.expand_path(File.join('~', '.showoff', 'keymap.json'))
  @keymap      = Keymap.default
  @keymap.merge! JSON.parse(File.read(keymapfile)) rescue {}

  # map keys to the labels we're using
  @keycode_dictionary   = Keymap.keycodeDictionary
  @keycode_shifted_keys = Keymap.shiftedKeyDictionary

  @highlightStyle = Showoff::Config.get('highlight') || 'default'

  if Showoff::State.get(:supplemental)
    @wrapper_classes = ['supplemental']
  end
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



7
8
9
# File 'lib/showoff/presentation.rb', line 7

def sections
  @sections
end

Instance Method Details

#assetsObject

Generates a list of all image/font/etc files used by the presentation. This will only identify the sources of <img> tags and files referenced by the CSS url() function.



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
# File 'lib/showoff/presentation.rb', line 95

def assets
  # This singleton guard removes ordering coupling between assets() & static()
  @doc ||= compile

  # matches url(<path>) and returns the path as a capture group
  urlsrc = /url\([\"\']?(.*?)(?:[#\?].*)?[\"\']?\)/

  # get all image and url() sources
  files = @doc.search('img').map {|img| img[:src] }
  @doc.search('*').each do |node|
    next unless node[:style]
    next unless matches = node[:style].match(urlsrc)
    files << matches[1]
  end

  # add in images from css files too
  css_files.each do |css_path|
    data = File.read(File.join(Showoff::Config.root, css_path))

    # @todo: This isn't perfect. It will match commented out styles. But its
    # worst case behavior is displaying a warning message, so that's ok for now.
    data.scan(urlsrc).flatten.each do |path|
      # resolve relative paths in the stylesheet
      path = File.join(File.dirname(css_path), path) unless path.start_with? '/'
      files << path
    end
  end

  # also all user-defined styles and javascript files
  files.concat css_files
  files.concat js_files
  files.uniq
end

#compileObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/showoff/presentation.rb', line 43

def compile
  Showoff::State.reset([:slide_count, :section_major, :section_minor])

  # @todo For now, we reparse the html so that we can generate content via slide
  #       templates. This adds a bit of extra time, but not too much. Perhaps
  #       we'll change that at some point.
  html = @sections.map(&:render).join("\n")
  doc  = Nokogiri::HTML::DocumentFragment.parse(html)

  Showoff::Compiler::TableOfContents.generate!(doc)
  Showoff::Compiler::Glossary.generatePage!(doc)

  doc
end

#css_filesObject



133
134
135
136
137
# File 'lib/showoff/presentation.rb', line 133

def css_files
  base  = Dir.glob("#{Showoff::Config.root}/*.css").map { |path| File.basename(path) }
  extra = Array(Showoff::Config.get('styles'))
  base + extra
end

#erb(template) ⇒ Object



129
130
131
# File 'lib/showoff/presentation.rb', line 129

def erb(template)
  ERB.new(File.read(File.join(Showoff::GEMROOT, 'views', "#{template}.erb")), nil, '-').result(binding)
end

#indexObject

The index page does not contain content; just a placeholder div that’s dynamically loaded after the page is displayed. This increases perceived responsiveness.



61
62
63
# File 'lib/showoff/presentation.rb', line 61

def index
  ERB.new(File.read(File.join(Showoff::GEMROOT, 'views','index.erb')), nil, '-').result(binding)
end

#js_filesObject



139
140
141
142
143
# File 'lib/showoff/presentation.rb', line 139

def js_files
  base  = Dir.glob("#{Showoff::Config.root}/*.js").map { |path| File.basename(path) }
  extra = Array(Showoff::Config.get('scripts'))
  base + extra
end

#language_namesObject

@todo: backwards compatibility shim



165
166
167
# File 'lib/showoff/presentation.rb', line 165

def language_names
  Showoff::Locale.contentLanguages
end

#mapped_keys(action, klass = 'key') ⇒ Object

return a list of keys associated with a given action in the keymap



146
147
148
149
150
151
152
153
154
# File 'lib/showoff/presentation.rb', line 146

def mapped_keys(action, klass='key')
  list = @keymap.select { |key,value| value == action }.keys

  if klass
    list.map { |val| "<span class=\"#{klass}\">#{val}</span>" }.join
  else
    list.join ', '
  end
end

#master_presenter?Boolean

@todo: this should be part of the server. Move there with the least disruption.

Returns:

  • (Boolean)


171
172
173
# File 'lib/showoff/presentation.rb', line 171

def master_presenter?
  false
end

#slidesObject



65
66
67
# File 'lib/showoff/presentation.rb', line 65

def slides
  compile.to_html
end

#staticObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/showoff/presentation.rb', line 69

def static
  # This singleton guard removes ordering coupling between assets() & static()
  @doc  ||= compile
  @slides = @doc.to_html

  # All static snapshots should be non-interactive by definition
  @interactive = false

  case Showoff::State.get(:format)
  when 'web'
    template = 'index.erb'
  when 'print', 'supplemental', 'pdf'
    template = 'onepage.erb'
  end

  ERB.new(File.read(File.join(Showoff::GEMROOT, 'views', template)), nil, '-').result(binding)
end

#user_translationsObject

@todo: backwards compatibility shim



160
161
162
# File 'lib/showoff/presentation.rb', line 160

def user_translations
  Showoff::Locale.userTranslations
end

#valid_presenter_cookie?Boolean

@todo: this should be part of the server. Move there with the least disruption.

Returns:

  • (Boolean)


176
177
178
# File 'lib/showoff/presentation.rb', line 176

def valid_presenter_cookie?
  false
end