Class: Deck::Slide

Inherits:
Erector::Widget
  • Object
show all
Includes:
Noko
Defined in:
lib/deck/slide.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Noko

#noko_doc, #noko_html

Constructor Details

#initialize(options = {}) ⇒ Slide

Returns a new instance of Slide.



58
59
60
61
62
63
# File 'lib/deck/slide.rb', line 58

def initialize options = {}
  super options

  @classes = process_classes
  @markdown_text = ""
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



53
54
55
# File 'lib/deck/slide.rb', line 53

def classes
  @classes
end

#markdown_textObject (readonly)

Returns the value of attribute markdown_text.



53
54
55
# File 'lib/deck/slide.rb', line 53

def markdown_text
  @markdown_text
end

Class Method Details

.from_file(markdown_file) ⇒ Object

todo: test this method on its own



11
12
13
14
# File 'lib/deck/slide.rb', line 11

def self.from_file markdown_file
  markdown_file = markdown_file.path if markdown_file.is_a? File   # fix for Ruby 1.8.7
  split File.read(markdown_file)
end

.split(content) ⇒ Object

given a chunk of Markdown text, splits it into an array of Slide objects todo: move into SlideDeck?



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deck/slide.rb', line 18

def self.split content
  unless content =~ /^\<?!SLIDE/m     # this only applies to files with no !SLIDEs at all, which is odd
    content = content.
      gsub(/^# /m, "<!SLIDE>\n# ").
      gsub(/^(.*)\n(===+)/, "<!SLIDE>\n\\1\n\\2")
  end

  lines = content.split("\n")
  slides = []
  slides << (slide = Slide.new)
  until lines.empty?
    line = lines.shift
    if line =~ /^<?!SLIDE(.*)>?/
      slides << (slide = Slide.new(:classes => $1))

    elsif (line =~ /^# / or lines.first =~ /^(===+)/) and !slide.empty?
      # every H1 defines a new slide, unless there's a !SLIDE before it
      slides << (slide = Slide.new)
      slide << line

    elsif line =~ /^\.notes/
      # don't include notes

    else
      slide << line
    end
  end

  slides.delete_if {|slide| slide.empty? }

  slides
end

Instance Method Details

#<<(s) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/deck/slide.rb', line 98

def <<(s)
  if s.strip =~ /^\s*<?!VIDEO +([^\s>]*)>?$/
    youtube_id = $1
    # see https://developers.google.com/youtube/player_parameters
    s = %Q(<iframe class="video youtube" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/#{youtube_id}" frameborder="0"></iframe>\n)
  end
  @markdown_text << s
  @markdown_text << "\n"
end

#==(other) ⇒ Object



65
66
67
68
69
# File 'lib/deck/slide.rb', line 65

def ==(other)
  Slide === other and
  @classes == other.classes and
  @markdown_text == other.markdown_text
end

#contentObject



124
125
126
127
128
129
130
131
# File 'lib/deck/slide.rb', line 124

def content
  section :class => @classes, :id => slide_id do
    text "\n" # markdown HTML should be left-aligned, in case of PRE blocks and other quirks
    html = markdown.render(markdown_text)
    html = munge(html)
    rawtext html
  end
end

#empty?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/deck/slide.rb', line 108

def empty?
  @markdown_text.strip == ""
end

#markdownObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deck/slide.rb', line 84

def markdown
  @@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
    :no_intra_emphasis => true,
    :tables => true,
    :fenced_code_blocks => true,
    :no_intra_emphasis => true,
    :autolink => true,
    :strikethrough => true,
    :lax_html_blocks => false,
    :space_after_headers => true,
    :superscript => false
  )
end

#process_classesObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/deck/slide.rb', line 71

def process_classes
  ["slide"] + case @classes
  when NilClass
    []
  when String
    @classes.strip.chomp('>').split
  when Array
    @classes
  else
    raise "can't deal with :classes => #{@classes.inspect}"
  end
end

#slide_idObject



118
119
120
121
122
# File 'lib/deck/slide.rb', line 118

def slide_id
  @slide_id ||= begin
    title.downcase.gsub(/[^\w\s]/, '').strip.gsub(/\s/, '_')
  end
end

#titleObject



112
113
114
115
116
# File 'lib/deck/slide.rb', line 112

def title
  lines = @markdown_text.strip.split("\n")
  raise "an empty slide has no id" if lines.empty?
  lines.first.gsub(/^[#=]*/, '').strip
end