Module: MarkdownPresenter

Defined in:
lib/markdown_presenter.rb

Class Method Summary collapse

Class Method Details

.content_of(filename) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/markdown_presenter.rb', line 20

def self.content_of filename
  if filename.end_with? ".md"
    document = Kramdown::Document.new(File.read(filename), parse_block_html: true, parse_span_html: true)
    document.to_html

  elsif filename.end_with? ".html"
    require 'nokogiri'
    doc = Nokogiri::HTML(File.read(filename))
    doc.search('body')[0].inner_html

  else
    $stderr.puts "Format not supported: #{File.extname(filename)}"
    exit 1
  end
end

.data_uri_for(url) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/markdown_presenter.rb', line 36

def self.data_uri_for url
  begin
    response = open(url)
    encoded_image = Base64.encode64 response.read
    content_type = response.respond_to?(:meta) ? response.meta['content-type'] : MIME::Types.type_for(url[-3..url.size]).first.content_type
    "data:#{content_type};base64,#{encoded_image}"
  rescue
    $stderr.puts "WARNING: An exception occurred trying to construct a data URI. We will fall back to the original URL.", $!, $!.backtrace
    url
  end
end

.embed(content) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/markdown_presenter.rb', line 48

def self.embed content
  doc = Nokogiri::HTML(content)
  tags_and_attributes = [['img', 'src'], ['link','href'], ['script', 'src']]
  tags_and_attributes.each do |tag_name, src|
    doc.css(tag_name).each do |tag|
      tag[src] = data_uri_for tag[src]
    end
  end
  doc.css("body").inner_html
end

.present(title, content) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/markdown_presenter.rb', line 12

def self.present title, content
  template = File.read(File.join(File.dirname(__FILE__), "../presenter.html.haml"))
  haml_engine = Haml::Engine.new(template)
  haml_engine.render(Object.new, {slides: content.split(/(?=<h1)/).reject {|s| s.chomp.empty?}, title: title}) do |filename|
    File.read File.join(File.dirname(__FILE__), "../", filename)
  end
end

.process(filename) ⇒ Object



59
60
61
# File 'lib/markdown_presenter.rb', line 59

def self.process filename
  puts present(filename, embed(content_of(filename)))
end