Skip to content

Commit a4e41cb

Browse files
committed
Merge asset_path and page_asset_path together
Merged new page_asset_path to original asset_path as per samrayner's comments. Also update readme and add a test site that checks that old and new functionality works.
1 parent 56217d7 commit a4e41cb

18 files changed

+204
-50
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_site/_site

README.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ Jekyll Asset Path Tag
55

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

8-
Syntax: `{% asset_path [filename] %}`
9-
`{% page_asset_path [page_id] [filename] %}`
8+
Syntax:
9+
```
10+
{% asset_path filename [post_id] %}
11+
{% asset_path "filename with whitespace" [post_id] %}
12+
```
1013

1114
##Installation
1215
Copy asset_path_tag.rb into */_plugins* ([Jekyll][j]) or */plugins* ([Octopress][o])
@@ -26,7 +29,7 @@ in page my-first-page would output:
2629
```
2730

2831
```
29-
{% page_asset_path /2012/05/25/another-post-title document.pdf %}
32+
{% asset_path document.pdf /2012/05/25/another-post-title %}
3033
```
3134
would output:
3235
```
@@ -57,14 +60,14 @@ then the tag will output:
5760
/assets/posts/another-post-title/image_two.png
5861
```
5962

60-
Tag `page-asset-path` is useful for showing asset from each page. Given the site contains pages:
63+
When used with `post_id` parameter, the tag is useful for showing asset from each page. Given the site contains pages:
6164
```
6265
post-title
6366
another-post-title
6467
```
6568
then
6669
```
67-
{% for post in site.posts %}{% page_asset_path {{post.id}} cover.jpg %}{% endfor %} on index.html
70+
{% for post in site.posts %}{% asset_path cover.jpg {{post.id}} %}{% endfor %} on index.html
6871
```
6972
will output:
7073
```
@@ -74,3 +77,12 @@ will output:
7477

7578
[j]: http://jekyllrb.com/
7679
[o]: http://octopress.org/
80+
81+
##Testing
82+
The plugin can be tested by using the Jekyll test site in `test_site` directory. Generate the site with
83+
```
84+
bundle exec jekyll serve
85+
```
86+
then check the test results by browsing to [http://localhost:4000][test_site].
87+
88+
[test_site]: http://localhost:4000

asset_path_tag.rb

+27-45
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# Title: Asset path tag for Jekyll
2-
# Author: Sam Rayner http://samrayner.com
2+
# Authors:
3+
# Sam Rayner http://samrayner.com
4+
# Otto Urpelainen http://koti.kapsi.fi/oturpe/projects/
5+
#
36
# Description: Output a relative URL for assets based on the post or page
47
#
58
# Syntax
6-
# {% asset_path [filename] %}
7-
# {% page_asset_path [page_id] [filename] %}
9+
# {% asset_path filename [post_id] %}
10+
# {% asset_path "filename with whitespace" [post_id] %}
811
#
912
# Examples:
1013
# {% asset_path kitten.png %} on post 2013-01-01-post-title
1114
# {% asset_path pirate.mov %} on page page-title
12-
# {% page_asset_path /2012/05/25/another-post-title document.pdf %}
15+
# {% asset_path document.pdf /2012/05/25/another-post-title %}
16+
# {% asset_path "document with long name.pdf" /2012/05/25/another-post-title %}
1317
#
1418
# Output:
1519
# /assets/posts/post-title/kitten.png
1620
# /assets/page-title/pirate.mov
1721
# /assets/posts/another-post-title/document.pdf
22+
# /assets/posts/another-post-title/document with long name.pdf
1823
#
1924
# Looping example using a variable for the pathname:
2025
#
@@ -35,7 +40,7 @@
3540
# post-title
3641
# another-post-title
3742
#
38-
# {% for post in site.posts %}{% page_asset_path {{post.id}} cover.jpg %}{% endfor %} on index.html
43+
# {% for post in site.posts %}{% asset_path cover.jpg {{post.id}} %}{% endfor %} on index.html
3944
#
4045
# Output:
4146
# /assets/posts/post-title/cover.jpg
@@ -75,21 +80,30 @@ def initialize(tag_name, markup, tokens)
7580

7681
def render(context)
7782
if @markup.empty?
78-
return "Error processing input, expected syntax: {% asset_path [filename] %}"
83+
return "Error processing input, expected syntax: {% asset_path filename [post id] %}"
7984
end
8085

8186
#render the markup
82-
rawFilename = Liquid::Template.parse(@markup).render context
83-
84-
#strip leading and trailing quotes
85-
filename = rawFilename.gsub(/^("|')|("|')$/, '')
87+
parameters = Liquid::Template.parse(@markup).render context
88+
parameters.strip!
89+
90+
if ['"', "'"].include? parameters[0]
91+
# Quoted filename, possibly followed by post id
92+
last_quote_index = parameters.rindex(parameters[0])
93+
filename = parameters[1 ... last_quote_index]
94+
post_id = parameters[(last_quote_index + 1) .. -1].strip
95+
else
96+
# Unquoted filename, possibly followed by post id
97+
filename, post_id = parameters.split(/\s+/)
98+
end
8699

87100
page = context.environments.first["page"]
88101

89-
#if a post
90-
if page["id"]
102+
post_id = page["id"] if post_id == nil or post_id.empty?
103+
if post_id
104+
#if a post
91105
posts = context.registers[:site].posts
92-
path = Jekyll.get_post_path(page["id"], posts)
106+
path = Jekyll.get_post_path(post_id, posts)
93107
else
94108
path = page["url"]
95109
end
@@ -101,38 +115,6 @@ def render(context)
101115
"#{context.registers[:site].config['baseurl']}/assets/#{path}/#{filename}".gsub(/\/{2,}/, '/')
102116
end
103117
end
104-
105-
class PageAssetPathTag < Liquid::Tag
106-
@markup = nil
107-
108-
def initialize(tag_name, markup, tokens)
109-
#strip leading and trailing spaces
110-
@markup = markup.strip
111-
super
112-
end
113-
114-
def render(context)
115-
if @markup.empty?
116-
return "Error processing input, expected syntax: {% page_asset_path [page_id] [filename] %}"
117-
end
118-
119-
# render the page markup
120-
parsed = Liquid::Template.parse(@markup).render context
121-
id, filename = parsed.split(' ', 2).each do |param|
122-
unquoted = param.gsub(/^("|')|("|')$/, '')
123-
unquoted.strip
124-
end
125-
126-
posts = context.registers[:site].posts
127-
path = Jekyll.get_post_path(id, posts)
128-
path = File.dirname(path) if path =~ /\.\w+$/
129-
130-
root = context.registers[:site].config['baseurl']
131-
#fix double slashes
132-
"#{root}/assets/#{path}/#{filename}".gsub(/\/{2,}/, '/')
133-
end
134-
end
135118
end
136119

137120
Liquid::Template.register_tag('asset_path', Jekyll::AssetPathTag)
138-
Liquid::Template.register_tag('page_asset_path', Jekyll::PageAssetPathTag)

test_site/Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'jekyll', '3.1.2'
4+
gem 'json'
5+
gem 'stringex'
6+
gem 'rmagick', '~>2.16.0'

test_site/Gemfile.lock

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
colorator (0.1)
5+
ffi (1.9.14)
6+
jekyll (3.1.2)
7+
colorator (~> 0.1)
8+
jekyll-sass-converter (~> 1.0)
9+
jekyll-watch (~> 1.1)
10+
kramdown (~> 1.3)
11+
liquid (~> 3.0)
12+
mercenary (~> 0.3.3)
13+
rouge (~> 1.7)
14+
safe_yaml (~> 1.0)
15+
jekyll-sass-converter (1.4.0)
16+
sass (~> 3.4)
17+
jekyll-watch (1.5.0)
18+
listen (~> 3.0, < 3.1)
19+
json (1.8.3)
20+
kramdown (1.11.1)
21+
liquid (3.0.6)
22+
listen (3.0.8)
23+
rb-fsevent (~> 0.9, >= 0.9.4)
24+
rb-inotify (~> 0.9, >= 0.9.7)
25+
mercenary (0.3.6)
26+
rb-fsevent (0.9.7)
27+
rb-inotify (0.9.7)
28+
ffi (>= 0.5.0)
29+
rmagick (2.16.0)
30+
rouge (1.11.1)
31+
safe_yaml (1.0.4)
32+
sass (3.4.22)
33+
stringex (2.6.0)
34+
35+
PLATFORMS
36+
ruby
37+
38+
DEPENDENCIES
39+
jekyll (= 3.1.2)
40+
json
41+
rmagick (~> 2.16.0)
42+
stringex
43+
44+
BUNDLED WITH
45+
1.12.5

test_site/_config.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Welcome to Jekyll!
2+
#
3+
# This config file is meant for settings that affect your whole blog, values
4+
# which you are expected to set up once and rarely edit after that. If you find
5+
# yourself editing these this file very often, consider using Jekyll's data files
6+
# feature for the data you need to update frequently.
7+
#
8+
# For technical reasons, this file is *NOT* reloaded automatically when you use
9+
# 'jekyll serve'. If you change this file, please restart the server process.
10+
11+
# Site settings
12+
# These are used to personalize your new site. If you look in the HTML files,
13+
# you will see them accessed via {{ site.title }}, {{ site.email }}, and so on.
14+
# You can create any custom variable you would like, and they will be accessible
15+
# in the templates via {{ site.myvariable }}.
16+
title: jekyll-asset-path-plugin test site
17+
18+
description: > # this means to ignore newlines until "baseurl:"
19+
Test site for jekyll-asset-path-plugin
20+
baseurl: "" # the subpath of your site, e.g. /blog
21+
url: "http://example.com" # the base hostname & protocol for your site
22+
23+
# Build settings
24+
markdown: kramdown

test_site/_plugins/asset_path_tag.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../asset_path_tag.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: "Another page"
3+
date: 2000-01-02 10:50:12 +0200
4+
---
5+
Another page to reference to.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "jekyll-thumbnail-generator tests"
3+
date: 2017-01-14 10:50:12 +0200
4+
---
5+
Page tests.
6+
7+
## This page
8+
9+
### Unquoted: this-page.png
10+
![Failure]({% asset_path this-page.png %})
11+
12+
### Double quotes: "this-page.png"
13+
![Failure]({% asset_path "this-page.png" %})
14+
15+
### Single quotes: 'this-page.png'
16+
![Failure]({% asset_path 'this-page.png' %})
17+
18+
### Quoted with spaces: "this page with space.png"
19+
![Failure]({% asset_path "this page with space.png" %})
20+
21+
### Single quotes with spaces: 'this page with space.png'
22+
![Failure]({% asset_path 'this page with space.png' %})
23+
24+
## Other page
25+
26+
### Unquoted: another-page.png /2000/01/02/Another_page
27+
![Failure]({% asset_path another-page.png /2000/01/02/Another_page %})
28+
29+
### Double quotes: "another-page.png" /2000/01/02/Another_page
30+
![Failure]({% asset_path "another-page.png" /2000/01/02/Another_page %})
31+
32+
### Single quotes: 'another-page.png' /2000/01/02/Another_page
33+
![Failure]({% asset_path 'another-page.png' /2000/01/02/Another_page %})
34+
35+
### Double quotes with space: "another page with space.png" /2000/01/02/Another_page
36+
![Failure]({% asset_path "another page with space.png" /2000/01/02/Another_page %})
37+
38+
### Single quotes with space: 'another page with space.png' /2000/01/02/Another_page
39+
![Failure]({% asset_path 'another page with space.png' /2000/01/02/Another_page %})
40+
41+
<!-- TODO: More cases needed? -->

test_site/assets/error.png

5.19 KB
Loading
Loading
4.88 KB
Loading
Loading
Loading
Loading
4.88 KB
Loading

test_site/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
---
3+
4+
<div class="home">
5+
6+
<h1>jekyll-asset-path-plugin tests</h1>
7+
This is a test page for
8+
<a href="https://github.com/samrayner/jekyll-asset-path-plugin">jekyll-asset-path-plugin</a>.
9+
On each of the following pages, there is a set of test cases listed. A successful test shows a green
10+
cicle. A failed test shows <i>Failure</i>.
11+
<p>
12+
<a href="nonpost_tests/index.html">Non-post tests</a>
13+
<p>
14+
<a href="2017/01/14/tests.html">Post tests</a>
15+
</div>

test_site/nonpost_tests.markdown

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: "jekyll-thumbnail-generator non-post tests"
3+
permalink: /nonpost_tests/
4+
---
5+
Nonpost tests
6+
7+
## This page
8+
9+
### Unquoted: nonpost.png
10+
![Failure]({% asset_path nonpost.png %})
11+
12+
### Double quotes: "nonpost.png"
13+
![Failure]({% asset_path "nonpost.png" %})
14+
15+
### Single quotes: 'nonpost.png'
16+
![Failure]({% asset_path 'nonpost.png' %})
17+
18+
### Quoted with spaces: "nonpost with space.png"
19+
![Failure]({% asset_path "nonpost with space.png" %})
20+
21+
### Single quotes with spaces: 'nonpost with space.png'
22+
![Failure]({% asset_path 'nonpost with space.png' %})

0 commit comments

Comments
 (0)