Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add <asset> tag

The slide can use this pseudo-tag to create links to the assets.

If the asset is an image, a <img> will be created.

If the asset is a javascript file, a <script> tag will be created.

If the asset is a CSS file, a <link> tag will be created.

If the type can not be determined, a simple link will be created.

The type can be forced adding a .style, .javascript or .image tag. Something
like

  %asset.image foo.apng
  • Loading branch information...
commit aaa3ebd88ca9735aebfcc24350d3d13cdbe3cb5d 1 parent 7a523e7
Ayose Cazorla authored
2  lib/highcarb/assets_generator.rb
View
@@ -6,7 +6,7 @@ def assets(asset)
plain_response! 403, "URL can not contain /../"
end
- asset_path = assets_root.join(asset)
+ asset_path = assets_root.join("./" + asset)
if not asset_path.exist?
not_found! asset
end
2  lib/highcarb/rack_app.rb
View
@@ -21,7 +21,7 @@ class RackApp
def initialize(command)
@command = command
@root = Pathname.new(command.args.first)
- @assets_root = @root.join("/assets")
+ @assets_root = @root.join("./assets")
end
def plain_response!(status, content)
47 lib/highcarb/slides_generator.rb
View
@@ -9,7 +9,7 @@ def slides
# 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.to_s !~ /^\./
+ 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
@@ -34,6 +34,12 @@ def slides
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
+
# Response with everything
output = page.at("body").inner_html
throw :response, [200, {'Content-Type' => 'text/html'}, output]
@@ -53,5 +59,44 @@ def load_snippet(snippet_name)
%[<pre class="raw-snippet">#{ERB::Util.h snippet_path.read}</pre>]
end
end
+
+ 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
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.