Class: MagicReveal::ProjectConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_reveal/project_config.rb

Overview

A Project’s configuration

Constant Summary collapse

DEFAULT_TEMPLATE =
File.expand_path('../template-config.json', __FILE__)
DEPENDENCY_ENABLER_JS =
{
  'highlight' => '{ src: "plugin/highlight/highlight.js", async: true, callback: function() { hljs.initHighlightingOnLoad(); } }',
  'zoom'      => '{ src: "plugin/zoom-js/zoom.js", async: true, condition: function() { return !!document.body.classList; } }',
  'notes'     => '{ src: "plugin/notes/notes.js", async: true, condition: function() { return !!document.body.classList; } }'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_path) ⇒ ProjectConfig

Returns a new instance of ProjectConfig.



16
17
18
19
# File 'lib/magic_reveal/project_config.rb', line 16

def initialize(io_or_path)
  io = io_or_path.respond_to?(:read) ? io_or_path : Pathname.new(io_or_path)
  @json = JSON.load(io.read)
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



14
15
16
# File 'lib/magic_reveal/project_config.rb', line 14

def json
  @json
end

Instance Method Details

#dependenciesObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/magic_reveal/project_config.rb', line 21

def dependencies
  out = []
  # you always want this
  out << '{ src: "lib/js/classList.js", condition: function() { return !document.body.classList; } }'

  DEPENDENCY_ENABLER_JS.keys.each do |plugin|
    out << DEPENDENCY_ENABLER_JS[plugin] if json['plugins'].include?(plugin)
  end

  "\"dependencies\": [\n#{out.join(",\n")}\n]"
end

#to_jsObject

rubocop:disable MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/magic_reveal/project_config.rb', line 33

def to_js # rubocop:disable MethodLength
  var = []
  keys = json.keys.reject { |k| %w{ 'dependencies', 'github' }.include?(k) }
  keys.each do |key|
    value = json[key]
    var << "  #{key}: #{value.to_json}"
  end

  out = []
  out << "/* Generated at #{Time.now} */"
  out << 'var config = {'
  out << "#{var.join(",\n")},\n#{dependencies}"
  out << "\n};"
  out << 'Reveal.initialize(config);'
  out.join("\n")
end