forked from samrayner/jekyll-asset-path-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset_path_tag.rb
120 lines (107 loc) · 3.34 KB
/
asset_path_tag.rb
1
2
3
4
5
6
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
67
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Title: Asset path tag for Jekyll
# Authors:
# Sam Rayner http://samrayner.com
# Otto Urpelainen http://koti.kapsi.fi/oturpe/projects/
#
# Description: Output a relative URL for assets based on the post or page
#
# Syntax
# {% asset_path filename post_id %}
# {% asset_path "filename with whitespace" post_id %}
#
# Examples:
# {% asset_path kitten.png %} on post 2013-01-01-post-title
# {% asset_path pirate.mov %} on page page-title
# {% asset_path document.pdf /2012/05/25/another-post-title %}
# {% asset_path "document with spaces in name.pdf" /2012/05/25/another-post-title %}
#
# Output:
# /assets/posts/post-title/kitten.png
# /assets/page-title/pirate.mov
# /assets/posts/another-post-title/document.pdf
# /assets/posts/another-post-title/document with spaces in name.pdf
#
# Looping example using a variable for the pathname:
#
# File _data/image.csv contains:
# file
# image_one.png
# image_two.png
#
# {% for image in site.data.images %}{% asset_path {{ image.file }} %}{% endfor %} on post 2015-03-21-post-title
#
# Output:
# /assets/posts/post-title/image_one.png
# /assets/posts/post-title/image_two.png
#
# Looping example over posts:
#
# Site contains posts:
# post-title
# another-post-title
#
# {% for post in site.posts %}{% asset_path cover.jpg {{post.id}} %}{% endfor %} on index.html
#
# Output:
# /assets/posts/post-title/cover.jpg
# /assets/posts/another-post-title/cover.jpg
module Jekyll
def self.get_post_path(page_id, posts)
#check for Jekyll version
if Jekyll::VERSION < '3.0.0'
#loop through posts to find match and get slug
posts.each do |post|
if post.id == page_id
return "posts/#{post.slug}"
end
end
else
#loop through posts to find match and get slug, method calls for Jekyll 3
posts.docs.each do |post|
if post.id == page_id
return "posts/#{post.data['slug']}"
end
end
end
return ""
end
class AssetPathTag < Liquid::Tag
@markup = nil
def initialize(tag_name, markup, tokens)
#strip leading and trailing spaces
@markup = markup.strip
super
end
def render(context)
if @markup.empty?
return "Error processing input, expected syntax: {% asset_path filename post_id %}"
end
#render the markup
parameters = Liquid::Template.parse(@markup).render context
parameters.strip!
if ['"', "'"].include? parameters[0]
# Quoted filename, possibly followed by post id
last_quote_index = parameters.rindex(parameters[0])
filename = parameters[1 ... last_quote_index]
post_id = parameters[(last_quote_index + 1) .. -1].strip
else
# Unquoted filename, possibly followed by post id
filename, post_id = parameters.split(/\s+/)
end
page = context.environments.first["page"]
post_id = page["id"] if post_id == nil or post_id.empty?
if post_id
#if a post
posts = context.registers[:site].posts
path = Jekyll.get_post_path(post_id, posts)
else
path = page["url"]
end
#strip filename
path = File.dirname(path) if path =~ /\.\w+$/
#fix double slashes
"#{context.registers[:site].config['baseurl']}/assets/#{path}/#{filename}".gsub(/\/{2,}/, '/')
end
end
end
Liquid::Template.register_tag('asset_path', Jekyll::AssetPathTag)