Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samrayner committed Oct 28, 2013
0 parents commit c9ed53d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Sam Rayner

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Jekyll Asset Path Tag
=====================

A liquid tag to output a relative URL for assets based on the post or page, allowing you to organise your assets into subdirectories.

Syntax: `{% asset_path [filename] %}`

##Installation
Copy asset_path_tag.rb into */_plugins* ([Jekyll][j]) or */plugins* ([Octopress][o])

##Examples

```
{% asset_path my-image.png %}
```
in post 2013-01-01-post-title would output:
```
/assets/posts/post-title/my-image.png
```
in page my-first-page would output:
```
/assets/my-first-page/my-image.png
```

Useful for images and links in Markdown or HTML:
```
[Download script]({% asset_path my-script.js %})
<img src="{% asset_path my-image.png %}" alt="My Image" />
```

[j]: http://jekyllrb.com/
[o]: http://octopress.org/
54 changes: 54 additions & 0 deletions asset_path_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Title: Asset path tag for Jekyll
# Author: Sam Rayner http://samrayner.com
# Description: Output a relative URL for assets based on the post or page
#
# Syntax {% asset_path [filename] %}
#
# Examples:
# {% asset_path kitten.png %} on post 2013-01-01-post-title
# {% asset_path pirate.mov %} on page page-title
#
# Output:
# /assets/posts/post-title/kitten.png
# /assets/page-title/pirate.mov
#

module Jekyll
class AssetPathTag < Liquid::Tag
@filename = nil

def initialize(tag_name, markup, tokens)
#strip leading and trailing quotes
@filename = markup.strip.gsub(/^("|')|("|')$/, '')
super
end

def render(context)
if @filename.empty?
return "Error processing input, expected syntax: {% asset_path [filename] %}"
end

path = ""
page = context.environments.first["page"]

#if a post
if page["id"]
#loop through posts to find match and get slug
context.registers[:site].posts.each do |post|
if post.id == page["id"]
path = "/posts/#{post.slug}"
end
end
else
path = page["url"].sub(/\/$/, "")
end

#strip filename
path = File.dirname(path) if path =~ /\.\w+$/

"/assets#{path}/#{@filename}"
end
end
end

Liquid::Template.register_tag('asset_path', Jekyll::AssetPathTag)

0 comments on commit c9ed53d

Please sign in to comment.