Class: Showoff::Compiler::Notes

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

Overview

adds presenter notes processing to the compiler

Class Method Summary collapse

Class Method Details

.render!(doc, profile, options = {}) ⇒ Nokogiri::HTML::DocumentFragment

Note:

A ton of the functionality in the original method got refactored to its logical location

Generate the presenter notes sections, including personal notes

Parameters:

  • doc (Nokogiri::HTML::DocumentFragment)

    The slide document

  • profile (String)

    The markdown engine profile to use when rendering

  • options (Hash) (defaults to: {})

    Options used for rendering any embedded markdown

Options Hash (options):

  • :name (String)

    The markdown slide name

  • :seq (String)

    The sequence number for multiple slides in one file

Returns:

  • (Nokogiri::HTML::DocumentFragment)

    The slide DOM with all notes sections rendered.

See Also:



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/showoff/compiler/notes.rb', line 23

def self.render!(doc, profile, options = {})
  # Turn tags into classed divs.
  doc.search('p').select {|p| p.text.start_with?('~~~SECTION:') }.each do |p|
    klass = p.text.match(/~~~SECTION:([^~]*)~~~/)[1]

    # Don't bother creating this if we don't want to use it
    next unless Showoff::Config.includeNotes?(klass)

    notes = Nokogiri::XML::Node.new('div', doc)
    notes.add_class("notes-section #{klass}")
    nodes = []
    iter = p.next_sibling
    until iter.text == '~~~ENDSECTION~~~' do
      nodes << iter
      iter = iter.next_sibling

      # if the author forgot the closing tag, let's not crash, eh?
      break unless iter
    end
    iter.remove if iter # remove the extraneous closing ~~~ENDSECTION~~~ tag

    # We need to collect the list before moving or the iteration crashes since the iterator no longer has a sibling
    nodes.each {|n| n.parent = notes }

    p.replace(notes)
  end

  filename = [
    File.join(Showoff::Config.root, '_notes', "#{options[:name]}.#{options[:seq]}.md"),
    File.join(Showoff::Config.root, '_notes', "#{options[:name]}.md"),
  ].find {|path| File.file?(path) }

  if filename and Showoff::Config.includeNotes?('notes')
    # Make sure we've got a notes div to hang personal notes from
    doc.add_child '<div class="notes-section notes"></div>' if doc.search('div.notes-section.notes').empty?
    doc.search('div.notes-section.notes').each do |section|
      text = Tilt[:markdown].new(nil, nil, options[:profile]) { File.read(filename) }.render
      frag = "<div class=\"personal\"><h1>#{I18n.t('presenter.notes.personal')}</h1>#{text}</div>"
      section.prepend_child(frag)
    end
  end

  # return notes separately from content so that it can be rendered outside the slide
  # @see https://github.com/puppetlabs/showoff/blob/3f43754c84f97be4284bb34f9bc7c42175d45226/lib/showoff.rb#L726-L732
  notes = doc.search('div.notes-section')
  doc.search('div.notes-section').each {|n| n.remove }

  [doc, notes]
end