Skip to content

Instantly share code, notes, and snippets.

@kevinoid
Created July 17, 2012 20:16

Revisions

  1. kevinoid revised this gist Jul 17, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions content-with-post.rb
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,8 @@ def copy_post_content(post)
    sitesrcdir = site.source
    contents = Dir.glob(File.join(postdir, '**', '*')) do |filepath|
    if filepath != postpath
    filedir, filename = File.split(filedir[sitesrcdir.length..-1])
    post.site.static_files <<
    filedir, filename = File.split(filepath[sitesrcdir.length..-1])
    site.static_files <<
    StaticPostFile.new(site, sitesrcdir, filedir, filename, destdir)
    end
    end
  2. kevinoid created this gist Jul 17, 2012.
    62 changes: 62 additions & 0 deletions content-with-post.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    module Jekyll

    class StaticPostFile < StaticFile
    # Initialize a new StaticPostFile.
    #
    # site - The Site.
    # base - The String path to the <source>.
    # sdir - The String path of the source directory of the file (rel <source>).
    # name - The String filename of the file.
    # ddir - The String path of the destination directory of the file.
    def initialize(site, base, sdir, name, ddir)
    super(site, base, sdir, name)
    @base = base
    @sdir = sdir
    @name = name
    @ddir = ddir || sdir
    end

    # Obtain destination path.
    #
    # dest - The String path to the destination dir.
    #
    # Returns destination file path.
    def destination(dest)
    File.join(dest, @ddir, @name)
    end
    end

    class PostContentGenerator < Generator
    # Copy the content associated with a specified post.
    #
    # post - A Post which may have associated content.
    def copy_post_content(post)
    if post.name !~ /\/index\.[^.\/]+$/
    return
    end

    # FIXME: Ick, hack, any alternative?
    postbase = post.instance_eval { @base }
    postpath = File.join(postbase, post.name)
    postdir = File.dirname(postpath)
    destdir = File.dirname(post.destination(""))

    site = post.site
    sitesrcdir = site.source
    contents = Dir.glob(File.join(postdir, '**', '*')) do |filepath|
    if filepath != postpath
    filedir, filename = File.split(filedir[sitesrcdir.length..-1])
    post.site.static_files <<
    StaticPostFile.new(site, sitesrcdir, filedir, filename, destdir)
    end
    end
    end

    # Generate content by copying files associated with each post.
    def generate(site)
    site.posts.each do |post|
    copy_post_content(post)
    end
    end
    end
    end