wip: allow custom extensions

This commit is contained in:
Parker Moore 2016-01-04 16:10:11 -08:00
parent 10a1b9451a
commit b6c283a4ae
13 changed files with 73 additions and 17 deletions

View File

@ -298,11 +298,11 @@ module Jekyll
when :pretty when :pretty
"/:categories/:year/:month/:day/:title/" "/:categories/:year/:month/:day/:title/"
when :none when :none
"/:categories/:title.html" "/:categories/:title:output_ext"
when :date when :date
"/:categories/:year/:month/:day/:title.html" "/:categories/:year/:month/:day/:title:output_ext"
when :ordinal when :ordinal
"/:categories/:year/:y_day/:title.html" "/:categories/:year/:y_day/:title:output_ext"
else else
permalink_style.to_s permalink_style.to_s
end end

View File

@ -4,7 +4,7 @@ module Jekyll
class Document class Document
include Comparable include Comparable
attr_reader :path, :site, :extname, :output_ext, :collection attr_reader :path, :site, :extname, :collection
attr_accessor :content, :output attr_accessor :content, :output
YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
@ -23,7 +23,6 @@ module Jekyll
@site = relations[:site] @site = relations[:site]
@path = path @path = path
@extname = File.extname(path) @extname = File.extname(path)
@output_ext = Jekyll::Renderer.new(site, self).output_ext
@collection = relations[:collection] @collection = relations[:collection]
@has_yaml_header = nil @has_yaml_header = nil
@ -86,6 +85,13 @@ module Jekyll
@relative_path ||= Pathname.new(path).relative_path_from(Pathname.new(site.source)).to_s @relative_path ||= Pathname.new(path).relative_path_from(Pathname.new(site.source)).to_s
end end
# The output extension of the document.
#
# Returns the output extension
def output_ext
@output_ext ||= Jekyll::Renderer.new(site, self).output_ext
end
# The base filename of the document, without the file extname. # The base filename of the document, without the file extname.
# #
# Returns the basename without the file extname. # Returns the basename without the file extname.
@ -209,7 +215,7 @@ module Jekyll
dest = site.in_dest_dir(base_directory) dest = site.in_dest_dir(base_directory)
path = site.in_dest_dir(dest, URL.unescape_path(url)) path = site.in_dest_dir(dest, URL.unescape_path(url))
path = File.join(path, "index.html") if url.end_with?("/") path = File.join(path, "index.html") if url.end_with?("/")
path << output_ext unless path.end_with?(output_ext) path << output_ext unless path.end_with? output_ext
path path
end end

View File

@ -142,7 +142,7 @@ module Jekyll
def destination(dest) def destination(dest)
path = site.in_dest_dir(dest, URL.unescape_path(url)) path = site.in_dest_dir(dest, URL.unescape_path(url))
path = File.join(path, "index") if url.end_with?("/") path = File.join(path, "index") if url.end_with?("/")
path << output_ext unless path.end_with?(output_ext) path << output_ext unless path.end_with? output_ext
path path
end end

View File

@ -22,7 +22,11 @@ module Jekyll
# #
# Returns the output extname including the leading period. # Returns the output extname including the leading period.
def output_ext def output_ext
@output_ext ||= converters.first.output_ext(document.extname) @output_ext ||= if document.permalink
File.extname(document.permalink)
else
converters.first.output_ext(document.extname)
end
end end
###################### ######################

View File

@ -1,5 +1,8 @@
require 'mime/types'
module Jekyll module Jekyll
module Utils extend self module Utils
extend self
autoload :Platforms, 'jekyll/utils/platforms' autoload :Platforms, 'jekyll/utils/platforms'
autoload :Ansi, "jekyll/utils/ansi" autoload :Ansi, "jekyll/utils/ansi"

View File

@ -0,0 +1,6 @@
---
am_i_convertible: yes
permalink: /slides/example-slide-7.php
---
Am I convertible? {{ page.am_i_convertible }}

View File

@ -0,0 +1,4 @@
---
---
I'm a Jekyll file! I should be output as dynamic_file.php, no .html to be found.

View File

@ -47,9 +47,7 @@ class TestDocument < JekyllUnitTest
context "with YAML ending in three dots" do context "with YAML ending in three dots" do
setup do setup do
@site = fixture_site({ @site = fixture_site({"collections" => ["methods"]})
"collections" => ["methods"],
})
@site.process @site.process
@document = @site.collections["methods"].docs.last @document = @site.collections["methods"].docs.last
end end
@ -195,6 +193,7 @@ class TestDocument < JekyllUnitTest
"permalink" => "/slides/test/:name" "permalink" => "/slides/test/:name"
} }
}, },
"permalink" => "pretty"
}) })
@site.process @site.process
@document = @site.collections["slides"].docs[0] @document = @site.collections["slides"].docs[0]
@ -245,7 +244,7 @@ class TestDocument < JekyllUnitTest
}) })
@site.permalink_style = :pretty @site.permalink_style = :pretty
@site.process @site.process
@document = @site.collections["slides"].docs[6] @document = @site.collections["slides"].docs[7]
@dest_file = dest_dir("slides/example-slide-Upper-Cased/index.html") @dest_file = dest_dir("slides/example-slide-Upper-Cased/index.html")
end end
@ -254,6 +253,29 @@ class TestDocument < JekyllUnitTest
end end
end end
context "a document in a collection with cased file name" do
setup do
@site = fixture_site({
"collections" => {
"slides" => {
"output" => true
}
}
})
@site.process
@document = @site.collections["slides"].docs[6]
@dest_file = dest_dir("slides/example-slide-7.php")
end
should "produce the permalink as the url" do
assert_equal "/slides/example-slide-7.php", @document.url
end
should "be written to the proper directory" do
assert_equal @dest_file, @document.destination(dest_dir)
end
end
context "documents in a collection with custom title permalinks" do context "documents in a collection with custom title permalinks" do
setup do setup do
@site = fixture_site({ @site = fixture_site({
@ -273,6 +295,7 @@ class TestDocument < JekyllUnitTest
should "produce the right URL if they have a slug" do should "produce the right URL if they have a slug" do
assert_equal "/slides/so-what-is-jekyll-exactly", @document.url assert_equal "/slides/so-what-is-jekyll-exactly", @document.url
end end
should "produce the right destination file if they have a slug" do should "produce the right destination file if they have a slug" do
dest_file = dest_dir("slides/so-what-is-jekyll-exactly.html") dest_file = dest_dir("slides/so-what-is-jekyll-exactly.html")
assert_equal dest_file, @document.destination(dest_dir) assert_equal dest_file, @document.destination(dest_dir)

View File

@ -257,7 +257,7 @@ class TestFilters < JekyllUnitTest
assert_equal 2, g["items"].size assert_equal 2, g["items"].size
when "" when ""
assert g["items"].is_a?(Array), "The list of grouped items for '' is not an Array." assert g["items"].is_a?(Array), "The list of grouped items for '' is not an Array."
assert_equal 11, g["items"].size assert_equal 12, g["items"].size
end end
end end
end end

View File

@ -39,8 +39,9 @@ class TestGeneratedSite < JekyllUnitTest
end end
should "process other static files and generate correct permalinks" do should "process other static files and generate correct permalinks" do
assert File.exist?(dest_dir('/about/index.html')) assert File.exist?(dest_dir('/about/index.html')), "about/index.html should exist"
assert File.exist?(dest_dir('/contacts.html')) assert File.exist?(dest_dir('/contacts.html')), "contacts.html should exist"
assert File.exist?(dest_dir('/dynamic_file.php')), "dynamic_file.php should exist"
end end
should "print a nice list of static files" do should "print a nice list of static files" do

View File

@ -55,6 +55,15 @@ class TestPage < JekyllUnitTest
assert_equal ".html", @page.ext assert_equal ".html", @page.ext
end end
should "deal properly with non-html extensions" do
@page = setup_page('dynamic_page.php')
@dest_file = dest_dir("dynamic_page.php")
assert_equal ".php", @page.ext
assert_equal "dynamic_page", @page.basename
assert_equal "/dynamic_page.php", @page.url
assert_equal @dest_file, @page.destination(dest_dir)
end
should "deal properly with dots" do should "deal properly with dots" do
@page = setup_page('deal.with.dots.html') @page = setup_page('deal.with.dots.html')
@dest_file = dest_dir("deal.with.dots.html") @dest_file = dest_dir("deal.with.dots.html")

View File

@ -174,6 +174,7 @@ class TestSite < JekyllUnitTest
coffeescript.coffee coffeescript.coffee
contacts.html contacts.html
deal.with.dots.html deal.with.dots.html
dynamic_file.php
environment.html environment.html
exploit.md exploit.md
foo.md foo.md

View File

@ -276,5 +276,4 @@ class TestUtils < JekyllUnitTest
refute Utils.has_yaml_header?(file) refute Utils.has_yaml_header?(file)
end end
end end
end end