Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#content_of(filename) ⇒ Object



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

def 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



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

def 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



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

def 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



10
11
12
13
14
15
16
# File 'lib/markdown_presenter.rb', line 10

def 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



57
58
59
# File 'lib/markdown_presenter.rb', line 57

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