Allow placeholders in permalinks #3031

Merged
merged 3 commits into from Oct 29, 2014

Conversation

Projects
None yet
4 participants
@alfredxing
Member

alfredxing commented Oct 27, 2014

Allow the regular placeholders (basename, path, ext) in permalinks. This is useful for setting permalink rules in frontmatter defaults (for example if I want my SCSS files in /assets/scss/ to output to /assets/css/).

Changes will only break functionality for people currently using :path, :ext, :basename, etc. in their permalinks. Permalinks without any of these special symbols will remain the same.

Ref. #2475, #1712.

lib/jekyll/url.rb
+ @placeholders.inject(@permalink) do |result, token|
+ break result if result.index(':').nil?
+ result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
+ end

This comment has been minimized.

@parkr

parkr Oct 27, 2014

Member

This smells of duplication!

@parkr

parkr Oct 27, 2014

Member

This smells of duplication!

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Oct 27, 2014

Member

👍

Member

parkr commented Oct 27, 2014

👍

@alfredxing

This comment has been minimized.

Show comment
Hide comment
@alfredxing

alfredxing Oct 27, 2014

Member

That should be better.

Member

alfredxing commented Oct 27, 2014

That should be better.

lib/jekyll/url.rb
#
# Returns the _unsanitizied_ String URL
def generate_url
- @placeholders.inject(@template) do |result, token|
+ @placeholders.inject(@permalink || @template) do |result, token|

This comment has been minimized.

@parkr

parkr Oct 27, 2014

Member

Let's restructure this. #generate_url should take a parameter – a template – that it uses as the first argument to #inject. That way we can have #generated_permalink and #generated_url use and memoize the permalink and template, respectively.

def to_s
  generated_permalink || generated_url
end

def generated_permalink
  (@generated_permalink ||= generate_url(@permalink)) if @permalink
end

def generated_url
  @generated_url ||= generate_url(@template)
end

def generate_url(template)
  @placeholders.inject(template) do |result, token|
    # etc...
  end
end
@parkr

parkr Oct 27, 2014

Member

Let's restructure this. #generate_url should take a parameter – a template – that it uses as the first argument to #inject. That way we can have #generated_permalink and #generated_url use and memoize the permalink and template, respectively.

def to_s
  generated_permalink || generated_url
end

def generated_permalink
  (@generated_permalink ||= generate_url(@permalink)) if @permalink
end

def generated_url
  @generated_url ||= generate_url(@template)
end

def generate_url(template)
  @placeholders.inject(template) do |result, token|
    # etc...
  end
end
@alfredxing

This comment has been minimized.

Show comment
Hide comment
@alfredxing

alfredxing Oct 28, 2014

Member

Done!

Member

alfredxing commented Oct 28, 2014

Done!

@parkr parkr merged commit 8dcf7a6 into jekyll:master Oct 29, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details

parkr added a commit that referenced this pull request Oct 29, 2014

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Oct 29, 2014

Member

/cc @benbalter – would love your 👀

Member

parkr commented Oct 29, 2014

/cc @benbalter – would love your 👀

@kleinfreund

This comment has been minimized.

Show comment
Hide comment
@kleinfreund

kleinfreund Nov 11, 2014

Contributor

In the 2.5.0 post what is :name front matter permalinks referring to? Atleast there is nothing on the permalink page regarding this.

Contributor

kleinfreund commented Nov 11, 2014

In the 2.5.0 post what is :name front matter permalinks referring to? Atleast there is nothing on the permalink page regarding this.

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Nov 11, 2014

Member

what is :name front matter permalinks referring to

Depends. For Page, nothing. For Post, nothing. For Document, it's a slugified version of the file basename without the extension.

We can use title or something more universal in the release post if you prefer?

Member

parkr commented Nov 11, 2014

what is :name front matter permalinks referring to

Depends. For Page, nothing. For Post, nothing. For Document, it's a slugified version of the file basename without the extension.

We can use title or something more universal in the release post if you prefer?

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.