Class: Middleman::Presentation::NewSlide

Inherits:
Object
  • Object
show all
Includes:
ComparableSlide
Defined in:
lib/middleman-presentation-core/new_slide.rb

Overview

A slide

Instance Method Summary collapse

Methods included from ComparableSlide

#<=>, #base_name?, #eql?, #group?, #hash, #match?, #similar?

Constructor Details

#initialize(input, base_path:) ⇒ NewSlide

Returns a new instance of NewSlide.



14
15
16
17
18
19
# File 'lib/middleman-presentation-core/new_slide.rb', line 14

def initialize(input, base_path:)
  @input                = input.split(/:/)
  @name                 = extract_name
  @slide_directory_path = Pathname.new(base_path)
  @base_path            = @slide_directory_path.dirname
end

Instance Method Details

#base_nameObject

Return basename of slide



45
46
47
# File 'lib/middleman-presentation-core/new_slide.rb', line 45

def base_name
  File.basename(name).scan(/^([^.]+)(?:\..+)?/).flatten.first
end

#content(**data) ⇒ Object

Generate slide content

It either uses previously set content or generates content by using a predefined template



81
82
83
# File 'lib/middleman-presentation-core/new_slide.rb', line 81

def content(**data)
  Erubis::Eruby.new(template.content).result(data)
end

#exist?Boolean

Does the slide exist

Returns:

  • (Boolean)


73
74
75
# File 'lib/middleman-presentation-core/new_slide.rb', line 73

def exist?
  path.exist?
end

#file_nameObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/middleman-presentation-core/new_slide.rb', line 49

def file_name
  path = if type? :erb
           Pathname.new "#{base_name}.html.erb"
         elsif type? :md
           Pathname.new "#{base_name}.html.md"
         elsif type? :liquid
           Pathname.new "#{base_name}.html.liquid"
         else
           Pathname.new("#{base_name}.html#{template.proposed_extname}")
         end

  Pathname.new(path)
end

#groupObject



40
41
42
# File 'lib/middleman-presentation-core/new_slide.rb', line 40

def group
  @group ||= extract_group
end

#pathObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/middleman-presentation-core/new_slide.rb', line 26

def path
  p = []
  p << Pathname.new(group) unless group.blank?
  p << file_name

  # rubocop:disable Style/EachWithObject
  p.reduce(slide_directory_path) do |a, e|
    # rubocop:enable Style/EachWithObject
    a += e

    a
  end
end

#relative_pathObject

Relative path of slide



86
87
88
# File 'lib/middleman-presentation-core/new_slide.rb', line 86

def relative_path
  path.relative_path_from(base_path)
end

#to_sObject

Return string representation of self



22
23
24
# File 'lib/middleman-presentation-core/new_slide.rb', line 22

def to_s
  path.to_s
end

#write(**data) ⇒ Object

Write slide content to file



64
65
66
67
68
69
70
# File 'lib/middleman-presentation-core/new_slide.rb', line 64

def write(**data)
  FileUtils.mkdir_p path.dirname

  File.open(path, 'wb') do |f|
    f.write(content(**data))
  end
end