Skip to content
Browse files

Added noslash and prewrite options for creating permalinks that lack …

….html but don't map to index.html

If 'noslash' is set to true (via command line or in _config.yml) then a
permalink like /:title will result in some-title.html rather than
/some-title/index.html
On the server you'd use rewrite rules to perform the mapping from
/some-title to /some-title.html.  When testing locally, pass the
--prewrite option to generate permalinks that end in .html so that it
works in your browser
  • Loading branch information...
1 parent 4499df8 commit 4b039b2804bc34382add7462f4e0f47c255e2151 @azsromej committed Mar 3, 2012
Showing with 22 additions and 3 deletions.
  1. +9 −1 bin/jekyll
  2. +11 −1 lib/jekyll/post.rb
  3. +2 −1 lib/jekyll/site.rb
View
10 bin/jekyll
@@ -106,7 +106,15 @@ opts = OptionParser.new do |opts|
opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style|
options['permalink'] = style unless style.nil?
end
-
+
+ opts.on("--noslash", "makes a-url map to a-url.html instead of a-url/index.html") do
+ options['noslash'] = true
+ end
+
+ opts.on("--prewrite", "Used to rewrite noslash urls for testing locally") do
+ options['prewrite'] = true
+ end
+
opts.on("--paginate [POSTS_PER_PAGE]", "Paginate a blog's posts") do |per_page|
begin
options['paginate'] = per_page.to_i
View
12 lib/jekyll/post.rb
@@ -143,6 +143,9 @@ def url
# sanitize url
@url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/')
@url += "/" if url =~ /\/$/
+ if self.site.noslash and site.config['prewrite']
+ @url = @url + ".html"
+ end
@url
end
@@ -198,7 +201,14 @@ def render(layouts, site_payload)
def destination(dest)
# The url needs to be unescaped in order to preserve the correct filename
path = File.join(dest, CGI.unescape(self.url))
- path = File.join(path, "index.html") if template[/\.html$/].nil?
+ if self.site.noslash
+ # if noslash, need to make url into file rather than directory/index.html
+ # note that if also running with --prewrite the url will already have the .html
+ # ending so that local testing will work (nginx handles rewrite on server)
+ path = path + ".html" unless path.end_with?(".html")
+ else
+ path = File.join(path, "index.html") if template[/\.html$/].nil?
+ end
path
end
View
3 lib/jekyll/site.rb
@@ -5,7 +5,7 @@ module Jekyll
class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files,
:categories, :exclude, :include, :source, :dest, :lsi, :pygments,
- :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts
+ :permalink_style, :noslash, :tags, :time, :future, :safe, :plugins, :limit_posts
attr_accessor :converters, :generators
@@ -22,6 +22,7 @@ def initialize(config)
self.lsi = config['lsi']
self.pygments = config['pygments']
self.permalink_style = config['permalink'].to_sym
+ self.noslash = config['noslash']
self.exclude = config['exclude'] || []
self.include = config['include'] || []
self.future = config['future']

0 comments on commit 4b039b2

Please sign in to comment.
Something went wrong with that request. Please try again.