Module: HighCarb::SlidesController

Included in:
RackApp
Defined in:
lib/highcarb/slides_controller.rb

Instance Method Summary collapse

Instance Method Details

#load_asset(asset_name, css_class = []) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/highcarb/slides_controller.rb', line 97

def load_asset(asset_name, css_class = [])
  asset_path = assets_root.join(asset_name)
  asset_url = "/assets/#{ERB::Util.h asset_name}"
  asset_type = nil

  if not css_class.empty?
    # Check if the css_class list contains any of the valid classes
    asset_type = (%w(image style javascript) & css_class).first
  end

  if asset_type.nil?
    # If the class attribute has no known class, infer it with the MIME type
    mime_type = MIME::Types.type_for(asset_name).first
    asset_type =
      if (mime_type and mime_type.media_type == "image")
        "image"
      elsif mime_type.to_s == "text/css"
        "style"
      elsif mime_type.to_s == "application/javascript" or asset_path.extname == "coffee"
        "javascript"
      end
  end

  case asset_type
  when "image"
    %[<img class="asset" src="#{asset_url}">]

  when "style"
    %[<link href="#{asset_url}" rel="stylesheet">]

  when "javascript"
    %[<script src="#{asset_url}"></script>]

  else
    %[<a href="#{asset_url}" target="_blank">#{ERB::Util.h asset_name}</script>]
  end

end

#load_snippet(snippet_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/highcarb/slides_controller.rb', line 68

def load_snippet(snippet_name)
  snippet_path = root.join("snippets", snippet_name)
  snippet_html_cached = root.join("tmp", "snippets",
                                  Digest::MD5.new.tap {|digest| digest << snippet_path.read }.hexdigest + ".html")

  if snippet_html_cached.exist? and snippet_html_cached.mtime > snippet_path.mtime

    snippet_html_cached.read

  else

    content = begin
      IO.popen(["pygmentize", "-f", "html", "-O", "noclasses=true", snippet_path.to_s]).read
    rescue Errno::ENOENT
      if not @pygmentize_error_shown
        STDERR.puts "\033[31mpygmentize could not be used. You have to install it if you want to highlight the snippets."
        STDERR.puts "The snippets will be included with no format\033[m"
        @pygmentize_error_shown = true
      end

      %[<pre class="raw-snippet">#{ERB::Util.h snippet_path.read}</pre>]
    end

    snippet_html_cached.dirname.mkpath
    snippet_html_cached.open("w") {|f| f.write content }
    content
  end
end

#slidesObject



7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/highcarb/slides_controller.rb', line 7

def slides
  output = []

  # Load the content from the sources
  root.join("slides").children.sort.each do |slide_file|
    # Only use non-hidden files
    if slide_file.file? and slide_file.basename.to_s !~ /^\./
      case slide_file.extname.downcase
      when ".haml"
        output << Haml::Engine.new(slide_file.read).render

      when ".html"
        output << slide_file.read

      when ".md"
        output << Kramdown::Document.new(slide_file.read).to_html

      else
        STDERR.puts "\033[31mCan not parse #{slide_file}\033[m"
      end
    end
  end

  page = Nokogiri::HTML.parse(output.join)

  # Find the <snippet> tags and replace with the content from a
  # file located under the snippet/ directory
  page.search("snippet").each do |snippet_tag|
    snippet_tag.replace load_snippet(snippet_tag.inner_text.strip)
  end

  # Find the <asset> tags and replace with link, script or img,
  # depending on the MIME type
  page.search("asset").each do |asset_tag|
    asset_tag.replace load_asset(asset_tag.inner_text.strip, asset_tag.attributes["class"].to_s.split)
  end

  # Find the <external> tags and replace with <a>
  # The text shown will be reduced
  page.search("external").each do |external_tag|
    href = ERB::Util.h(external_tag.inner_text.strip)

    text = href.gsub(/\w+:\/+/, "")
    text = text[0,45] + "&hellip;" if text.length > 45

    external_tag.replace %[<a class="external" href="#{href}" target="_blank" title="Open #{href} in a new window">#{text}</a>]
  end

  # Append a server side generated identifier. This helps to identify them
  # both in presenter- and remote-control-mode
  last_slide_id = 0
  page.search(".slide").each do |slide_node|
    last_slide_id += 1
    slide_node["data-slide-id"] = last_slide_id.to_s
  end

  # Response with everything
  output = page.at("body").inner_html
  throw :response, [200, {'Content-Type' => 'text/html'}, output]
end