Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

support public_root configuration to help organization static files #474

Closed
wants to merge 2 commits into from

3 participants

@karlseguin

The idea is that any file or folder inside of the specific public_root folder will get copied to the root of the generated site. I wasn't sure how files with yaml headers should be treated...I decided to not apply this logic to them. Can't decide which way is more consistent with what a user would expect

Instead of having your site look like:

index.html
resume.pdf
assets
images
unicorns.jpg
assets
css
main.css

You can put all the static and non-yaml files you want under the public_root => '/public':

index.html
public
resume.pdf
assets
images
unicorns.jpg
assets
css
main.css

Karl Seguin added some commits
@mojombo
Owner

I'm not sure I understand why this is useful. You can already put static files in any directory structure you want and they'll be copied over without modification. Why would you want a bunch of files copied from a subdirectory into the root?

@karlseguin

Basically: to preserve existing links while keeping my root clean.

There are a bunch of links which already point to http://openmymind.net/mongodb.pdf (Google, ...) I certainly don't want to break those links, but I'd also like my jekyll project to be tidier than having 20 files in the root.

I imagine a lot of people who migrate from rails (which kinda does the same thing with its public folder), who didn't have the foresight to put things in a subfolder (/public/files) would have the same mess.

@mojombo
Owner

Is this really a big enough problem to warrant complicating the docs and code? I'd wager the number of people migrating to Jekyll from Rails would be rather low.

@karlseguin

Your call (obviously). I won't cry more than once if you reject it!

@parkr
Owner

Closing for stagnancy and because @mojombo didn't seem too enthusiastic about the idea.

@parkr parkr closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2012
  1. added support for public_root config which copies files/folders from …

    Karl Seguin authored
    …the specific path into the root of the site
  2. fixed documentation and removed unecessary site being passed to stati…

    Karl Seguin authored
    …c_file constructor
This page is out of date. Refresh to see the latest.
View
8 features/create_sites.feature
@@ -92,3 +92,11 @@ Feature: Create sites
When I debug jekyll
Then the _site directory should exist
And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
+
+ Scenario: Basic site with static file in public root
+ Given I have a configuration file with "public_root" set to "/public"
+ And I have a public directory
+ And I have an "public/more.html" file that contains "To learn more spend time on wiki"
+ When I run jekyll
+ Then the _site directory should exist
+ And I should see "To learn more spend time on wiki" in "_site/more.html"
View
1  lib/jekyll.rb
@@ -59,6 +59,7 @@ module Jekyll
'source' => Dir.pwd,
'destination' => File.join(Dir.pwd, '_site'),
'plugins' => File.join(Dir.pwd, '_plugins'),
+ 'public_root' => nil,
'future' => true,
'lsi' => false,
View
6 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, :tags, :time, :future, :safe, :plugins, :public_root, :limit_posts
attr_accessor :converters, :generators
@@ -26,6 +26,7 @@ def initialize(config)
self.include = config['include'] || []
self.future = config['future']
self.limit_posts = config['limit_posts'] || nil
+ self.public_root = config['public_root'] || nil
self.reset
self.setup
@@ -140,8 +141,9 @@ def read_directories(dir = '')
# file appears to have a YAML header so process it as a page
pages << Page.new(self, self.source, dir, f)
else
+ destination_dir = self.public_root.nil? ? dir : dir.sub(/^#{Regexp.escape(self.public_root)}/, '')
# otherwise treat it as a static file
- static_files << StaticFile.new(self, self.source, dir, f)
+ static_files << StaticFile.new(self.source, dir, destination_dir, f)
end
end
end
View
14 lib/jekyll/static_file.rb
@@ -6,20 +6,20 @@ class StaticFile
# Initialize a new StaticFile.
#
- # site - The Site.
# base - The String path to the <source>.
- # dir - The String path between <source> and the file.
+ # source_dir - The String path between <source> and the file.
+ # destination_dir - The String path between <destinations> and the file.
# name - The String filename of the file.
- def initialize(site, base, dir, name)
- @site = site
+ def initialize(base, source_dir, destination_dir, name)
@base = base
- @dir = dir
+ @source_dir = source_dir
+ @destination_dir = destination_dir
@name = name
end
# Returns source file path.
def path
- File.join(@base, @dir, @name)
+ File.join(@base, @source_dir, @name)
end
# Obtain destination path.
@@ -28,7 +28,7 @@ def path
#
# Returns destination file path.
def destination(dest)
- File.join(dest, @dir, @name)
+ File.join(dest, @destination_dir, @name)
end
# Returns last modification time for this file.
View
1  test/source/public/learnmore.html
@@ -0,0 +1 @@
+to learn more ...
View
6 test/test_generated_site.rb
@@ -5,7 +5,7 @@ class TestGeneratedSite < Test::Unit::TestCase
setup do
clear_dest
stub(Jekyll).configuration do
- Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
+ Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'public_root' => '/public'})
end
@site = Site.new(Jekyll.configuration)
@@ -40,6 +40,10 @@ class TestGeneratedSite < Test::Unit::TestCase
assert File.exists?(dest_dir('/about/index.html'))
assert File.exists?(dest_dir('/contacts.html'))
end
+
+ should "process static file in public folder" do
+ assert File.exists?(dest_dir('/learnmore.html'))
+ end
end
context "generating limited posts" do
View
32 test/test_site.rb
@@ -4,7 +4,7 @@ class TestSite < Test::Unit::TestCase
context "creating sites" do
setup do
stub(Jekyll).configuration do
- Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
+ Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'public_folder' => '/public'})
end
@site = Site.new(Jekyll.configuration)
end
@@ -92,6 +92,36 @@ class TestSite < Test::Unit::TestCase
assert_equal mtime3, mtime4 # no modifications, so must be the same
end
+ should "write static modified static files from the public root" do
+ clear_dest
+ StaticFile.reset_cache
+
+ @site.process
+ some_static_file = @site.static_files.find{|f| f.path.end_with?('learnmore.html')}
+
+ dest = File.expand_path(some_static_file.destination(@site.dest))
+ mtime1 = File.stat(dest).mtime.to_i # first run must generate dest file
+
+ # need to sleep because filesystem timestamps have best resolution in seconds
+ sleep 1
+ @site.process
+ mtime2 = File.stat(dest).mtime.to_i
+ assert_equal mtime1, mtime2
+
+ # simulate file modification by user
+ FileUtils.touch some_static_file.path
+
+ sleep 1
+ @site.process
+ mtime3 = File.stat(dest).mtime.to_i
+ assert_not_equal mtime2, mtime3 # must be regenerated!
+
+ sleep 1
+ @site.process
+ mtime4 = File.stat(dest).mtime.to_i
+ assert_equal mtime3, mtime4 # no modifications, so must be the same
+ end
+
should "read layouts" do
@site.read_layouts
assert_equal ["default", "simple"].sort, @site.layouts.keys.sort
Something went wrong with that request. Please try again.