Class: MagicReveal::SlideRenderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
Redcarpet::Render::SmartyPants
Defined in:
lib/magic_reveal/slide_renderer.rb

Overview

Custom renderer so the HTML is the right format for Reveal.js.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSlideRenderer

Returns a new instance of SlideRenderer.



12
13
14
# File 'lib/magic_reveal/slide_renderer.rb', line 12

def initialize
  super(no_styles: true)
end

Instance Attribute Details

#has_shown_slidesObject

Returns the value of attribute has_shown_slides.



10
11
12
# File 'lib/magic_reveal/slide_renderer.rb', line 10

def has_shown_slides
  @has_shown_slides
end

Class Method Details

.markdown_rendererObject



16
17
18
19
20
21
22
23
24
# File 'lib/magic_reveal/slide_renderer.rb', line 16

def self.markdown_renderer
  Redcarpet::Markdown.new(
    new,
    space_after_headers: true,
    filter_html: true,
    fenced_code_blocks: true,
    no_intra_emphasis: true
  )
end

Instance Method Details

#block_code(code, language) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/magic_reveal/slide_renderer.rb', line 54

def block_code(code, language)
  [].tap do |output|
    output << (language ? "<pre class=\"#{language}\">"  : '<pre>')
    output << '<code>'

    output << CGI.escapeHTML(prepare_code code)
    output << '</code>'
    output << '</pre>'
  end.join('')
end


31
32
33
# File 'lib/magic_reveal/slide_renderer.rb', line 31

def doc_footer
  has_shown_slides ? '</section>' : ''
end

#doc_headerObject



26
27
28
29
# File 'lib/magic_reveal/slide_renderer.rb', line 26

def doc_header
  @has_shown_slides = false
  "<!-- generated by magic_reveal v#{VERSION} on #{Time.now} -->"
end

#header(text, header_level, anchor = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/magic_reveal/slide_renderer.rb', line 35

def header(text, header_level, anchor = nil)
  [].tap do |output|
    output << '</section>' if has_shown_slides
    @has_shown_slides = true
    anchor_html = anchor ? " id=\"#{anchor}\"" : ''

    output << '<section>'
    output << "<h#{header_level}#{anchor_html}>#{text}</h#{header_level}>"
  end.join("\n")
end

#prepare_code(code) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/magic_reveal/slide_renderer.rb', line 46

def prepare_code(code)
  if code =~ /\A\s*@@source\s*=\s*(?<filename>.*)\s*\Z/
    File.read Regexp.last_match[:filename]
  else
    code.sub(/\A\s*/, '').sub(/\s*\Z/, '')
  end
end