indirect-jekyll 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/History.txt +151 -0
- data/README.textile +42 -0
- data/Rakefile +90 -0
- data/VERSION.yml +4 -0
- data/bin/jekyll +150 -0
- data/features/create_sites.feature +46 -0
- data/features/embed_filters.feature +60 -0
- data/features/pagination.feature +40 -0
- data/features/permalinks.feature +65 -0
- data/features/post_data.feature +153 -0
- data/features/site_configuration.feature +63 -0
- data/features/site_data.feature +82 -0
- data/features/step_definitions/jekyll_steps.rb +136 -0
- data/features/support/env.rb +16 -0
- data/indirect-jekyll.gemspec +139 -0
- data/jekyll.gemspec +135 -0
- data/lib/jekyll.rb +85 -0
- data/lib/jekyll/albino.rb +122 -0
- data/lib/jekyll/converters/csv.rb +26 -0
- data/lib/jekyll/converters/mephisto.rb +79 -0
- data/lib/jekyll/converters/mt.rb +59 -0
- data/lib/jekyll/converters/textpattern.rb +50 -0
- data/lib/jekyll/converters/typo.rb +49 -0
- data/lib/jekyll/converters/wordpress.rb +54 -0
- data/lib/jekyll/convertible.rb +84 -0
- data/lib/jekyll/core_ext.rb +30 -0
- data/lib/jekyll/filters.rb +47 -0
- data/lib/jekyll/layout.rb +36 -0
- data/lib/jekyll/page.rb +112 -0
- data/lib/jekyll/pager.rb +45 -0
- data/lib/jekyll/post.rb +268 -0
- data/lib/jekyll/site.rb +265 -0
- data/lib/jekyll/tags/highlight.rb +56 -0
- data/lib/jekyll/tags/include.rb +31 -0
- data/test/helper.rb +27 -0
- data/test/source/_includes/sig.markdown +3 -0
- data/test/source/_layouts/default.html +27 -0
- data/test/source/_layouts/simple.html +1 -0
- data/test/source/_posts/2008-02-02-not-published.textile +8 -0
- data/test/source/_posts/2008-02-02-published.textile +8 -0
- data/test/source/_posts/2008-10-18-foo-bar.textile +8 -0
- data/test/source/_posts/2008-11-21-complex.textile +8 -0
- data/test/source/_posts/2008-12-03-permalinked-post.textile +9 -0
- data/test/source/_posts/2008-12-13-include.markdown +8 -0
- data/test/source/_posts/2009-01-27-array-categories.textile +10 -0
- data/test/source/_posts/2009-01-27-categories.textile +7 -0
- data/test/source/_posts/2009-01-27-category.textile +7 -0
- data/test/source/_posts/2009-03-12-hash-#1.markdown +6 -0
- data/test/source/_posts/2009-05-18-tag.textile +6 -0
- data/test/source/_posts/2009-05-18-tags.textile +9 -0
- data/test/source/_posts/2009-06-22-empty-yaml.textile +3 -0
- data/test/source/_posts/2009-06-22-no-yaml.textile +1 -0
- data/test/source/about.html +6 -0
- data/test/source/category/_posts/2008-9-23-categories.textile +6 -0
- data/test/source/contacts.html +5 -0
- data/test/source/css/screen.css +76 -0
- data/test/source/foo/_posts/bar/2008-12-12-topical-post.textile +8 -0
- data/test/source/index.html +22 -0
- data/test/source/sitemap.xml +23 -0
- data/test/source/win/_posts/2009-05-24-yaml-linebreak.markdown +7 -0
- data/test/source/z_category/_posts/2008-9-23-categories.textile +6 -0
- data/test/suite.rb +9 -0
- data/test/test_configuration.rb +29 -0
- data/test/test_filters.rb +49 -0
- data/test/test_generated_site.rb +40 -0
- data/test/test_page.rb +98 -0
- data/test/test_pager.rb +47 -0
- data/test/test_post.rb +302 -0
- data/test/test_site.rb +85 -0
- data/test/test_tags.rb +116 -0
- metadata +194 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
# Convertible provides methods for converting a pagelike item
|
2
|
+
# from a certain type of markup into actual content
|
3
|
+
#
|
4
|
+
# Requires
|
5
|
+
# self.site -> Jekyll::Site
|
6
|
+
module Jekyll
|
7
|
+
module Convertible
|
8
|
+
# Return the contents as a string
|
9
|
+
def to_s
|
10
|
+
self.content || ''
|
11
|
+
end
|
12
|
+
|
13
|
+
# Read the YAML frontmatter
|
14
|
+
# +base+ is the String path to the dir containing the file
|
15
|
+
# +name+ is the String filename of the file
|
16
|
+
#
|
17
|
+
# Returns nothing
|
18
|
+
def read_yaml(base, name)
|
19
|
+
self.content = File.read(File.join(base, name))
|
20
|
+
|
21
|
+
if self.content =~ /^(---\s*\n.*?\n?)(---.*?\n)/m
|
22
|
+
self.content = self.content[($1.size + $2.size)..-1]
|
23
|
+
|
24
|
+
self.data = YAML.load($1)
|
25
|
+
end
|
26
|
+
|
27
|
+
self.data ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Transform the contents based on the file extension.
|
31
|
+
#
|
32
|
+
# Returns nothing
|
33
|
+
def transform
|
34
|
+
case self.content_type
|
35
|
+
when 'textile'
|
36
|
+
self.ext = ".html"
|
37
|
+
self.content = self.site.textile(self.content)
|
38
|
+
when 'markdown'
|
39
|
+
self.ext = ".html"
|
40
|
+
self.content = self.site.markdown(self.content)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Determine which formatting engine to use based on this convertible's
|
45
|
+
# extension
|
46
|
+
#
|
47
|
+
# Returns one of :textile, :markdown or :unknown
|
48
|
+
def content_type
|
49
|
+
case self.ext[1..-1]
|
50
|
+
when /textile/i
|
51
|
+
return 'textile'
|
52
|
+
when /markdown/i, /mkdn/i, /md/i
|
53
|
+
return 'markdown'
|
54
|
+
end
|
55
|
+
return 'unknown'
|
56
|
+
end
|
57
|
+
|
58
|
+
# Add any necessary layouts to this convertible document
|
59
|
+
# +layouts+ is a Hash of {"name" => "layout"}
|
60
|
+
# +site_payload+ is the site payload hash
|
61
|
+
#
|
62
|
+
# Returns nothing
|
63
|
+
def do_layout(payload, layouts)
|
64
|
+
info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
|
65
|
+
|
66
|
+
# render and transform content (this becomes the final content of the object)
|
67
|
+
payload["content_type"] = self.content_type
|
68
|
+
self.content = Liquid::Template.parse(self.content).render(payload, info)
|
69
|
+
self.transform
|
70
|
+
|
71
|
+
# output keeps track of what will finally be written
|
72
|
+
self.output = self.content
|
73
|
+
|
74
|
+
# recursively render layouts
|
75
|
+
layout = layouts[self.data["layout"]]
|
76
|
+
while layout
|
77
|
+
payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
|
78
|
+
self.output = Liquid::Template.parse(layout.content).render(payload, info)
|
79
|
+
|
80
|
+
layout = layouts[layout.data["layout"]]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Hash
|
2
|
+
# Merges self with another hash, recursively.
|
3
|
+
#
|
4
|
+
# This code was lovingly stolen from some random gem:
|
5
|
+
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
|
6
|
+
#
|
7
|
+
# Thanks to whoever made it.
|
8
|
+
def deep_merge(hash)
|
9
|
+
target = dup
|
10
|
+
|
11
|
+
hash.keys.each do |key|
|
12
|
+
if hash[key].is_a? Hash and self[key].is_a? Hash
|
13
|
+
target[key] = target[key].deep_merge(hash[key])
|
14
|
+
next
|
15
|
+
end
|
16
|
+
|
17
|
+
target[key] = hash[key]
|
18
|
+
end
|
19
|
+
|
20
|
+
target
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Thanks, ActiveSupport!
|
25
|
+
class Date
|
26
|
+
# Converts datetime to an appropriate format for use in XML
|
27
|
+
def xmlschema
|
28
|
+
strftime("%Y-%m-%dT%H:%M:%S%Z")
|
29
|
+
end if RUBY_VERSION < '1.9'
|
30
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Jekyll
|
2
|
+
|
3
|
+
module Filters
|
4
|
+
def textilize(input)
|
5
|
+
RedCloth.new(input).to_html
|
6
|
+
end
|
7
|
+
|
8
|
+
def date_to_string(date)
|
9
|
+
date.strftime("%d %b %Y")
|
10
|
+
end
|
11
|
+
|
12
|
+
def date_to_long_string(date)
|
13
|
+
date.strftime("%d %B %Y")
|
14
|
+
end
|
15
|
+
|
16
|
+
def date_to_xmlschema(date)
|
17
|
+
date.xmlschema
|
18
|
+
end
|
19
|
+
|
20
|
+
def xml_escape(input)
|
21
|
+
CGI.escapeHTML(input)
|
22
|
+
end
|
23
|
+
|
24
|
+
def cgi_escape(input)
|
25
|
+
CGI::escape(input)
|
26
|
+
end
|
27
|
+
|
28
|
+
def number_of_words(input)
|
29
|
+
input.split.length
|
30
|
+
end
|
31
|
+
|
32
|
+
def array_to_sentence_string(array)
|
33
|
+
connector = "and"
|
34
|
+
case array.length
|
35
|
+
when 0
|
36
|
+
""
|
37
|
+
when 1
|
38
|
+
array[0].to_s
|
39
|
+
when 2
|
40
|
+
"#{array[0]} #{connector} #{array[1]}"
|
41
|
+
else
|
42
|
+
"#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Jekyll
|
2
|
+
|
3
|
+
class Layout
|
4
|
+
include Convertible
|
5
|
+
|
6
|
+
attr_accessor :site
|
7
|
+
attr_accessor :ext
|
8
|
+
attr_accessor :data, :content
|
9
|
+
|
10
|
+
# Initialize a new Layout.
|
11
|
+
# +site+ is the Site
|
12
|
+
# +base+ is the String path to the <source>
|
13
|
+
# +name+ is the String filename of the post file
|
14
|
+
#
|
15
|
+
# Returns <Page>
|
16
|
+
def initialize(site, base, name)
|
17
|
+
@site = site
|
18
|
+
@base = base
|
19
|
+
@name = name
|
20
|
+
|
21
|
+
self.data = {}
|
22
|
+
|
23
|
+
self.process(name)
|
24
|
+
self.read_yaml(base, name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Extract information from the layout filename
|
28
|
+
# +name+ is the String filename of the layout file
|
29
|
+
#
|
30
|
+
# Returns nothing
|
31
|
+
def process(name)
|
32
|
+
self.ext = File.extname(name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/jekyll/page.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
module Jekyll
|
2
|
+
|
3
|
+
class Page
|
4
|
+
include Convertible
|
5
|
+
|
6
|
+
attr_accessor :site
|
7
|
+
attr_accessor :name, :ext, :basename
|
8
|
+
attr_accessor :data, :content, :output
|
9
|
+
|
10
|
+
# Initialize a new Page.
|
11
|
+
# +site+ is the Site
|
12
|
+
# +base+ is the String path to the <source>
|
13
|
+
# +dir+ is the String path between <source> and the file
|
14
|
+
# +name+ is the String filename of the file
|
15
|
+
#
|
16
|
+
# Returns <Page>
|
17
|
+
def initialize(site, base, dir, name)
|
18
|
+
@site = site
|
19
|
+
@base = base
|
20
|
+
@dir = dir
|
21
|
+
@name = name
|
22
|
+
|
23
|
+
self.process(name)
|
24
|
+
self.read_yaml(File.join(base, dir), name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# The generated directory into which the page will be placed
|
28
|
+
# upon generation. This is derived from the permalink or, if
|
29
|
+
# permalink is absent, set to '/'
|
30
|
+
#
|
31
|
+
# Returns <String>
|
32
|
+
def dir
|
33
|
+
url[-1, 1] == '/' ? url : File.dirname(url)
|
34
|
+
end
|
35
|
+
|
36
|
+
# The full path and filename of the post.
|
37
|
+
# Defined in the YAML of the post body
|
38
|
+
# (Optional)
|
39
|
+
#
|
40
|
+
# Returns <String>
|
41
|
+
def permalink
|
42
|
+
self.data && self.data['permalink']
|
43
|
+
end
|
44
|
+
|
45
|
+
def template
|
46
|
+
if self.site.permalink_style == :pretty && !index?
|
47
|
+
"/:name/"
|
48
|
+
else
|
49
|
+
"/:name.html"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# The generated relative url of this page
|
54
|
+
# e.g. /about.html
|
55
|
+
#
|
56
|
+
# Returns <String>
|
57
|
+
def url
|
58
|
+
return permalink if permalink
|
59
|
+
|
60
|
+
@url ||= (ext == '.html') ? template.gsub(':name', basename) : "/#{name}"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Extract information from the page filename
|
64
|
+
# +name+ is the String filename of the page file
|
65
|
+
#
|
66
|
+
# Returns nothing
|
67
|
+
def process(name)
|
68
|
+
self.ext = File.extname(name)
|
69
|
+
self.basename = name.split('.')[0..-2].first
|
70
|
+
end
|
71
|
+
|
72
|
+
# Add any necessary layouts to this post
|
73
|
+
# +layouts+ is a Hash of {"name" => "layout"}
|
74
|
+
# +site_payload+ is the site payload hash
|
75
|
+
#
|
76
|
+
# Returns nothing
|
77
|
+
def render(layouts, site_payload)
|
78
|
+
payload = {"page" => self.data}.deep_merge(site_payload)
|
79
|
+
do_layout(payload, layouts)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Write the generated page file to the destination directory.
|
83
|
+
# +dest_prefix+ is the String path to the destination dir
|
84
|
+
# +dest_suffix+ is a suffix path to the destination dir
|
85
|
+
#
|
86
|
+
# Returns nothing
|
87
|
+
def write(dest_prefix, dest_suffix = nil)
|
88
|
+
dest = File.join(dest_prefix, @dir)
|
89
|
+
dest = File.join(dest, dest_suffix) if dest_suffix
|
90
|
+
FileUtils.mkdir_p(dest)
|
91
|
+
|
92
|
+
# The url needs to be unescaped in order to preserve the correct filename
|
93
|
+
path = File.join(dest, CGI.unescape(self.url))
|
94
|
+
if self.ext == '.html' && self.url[/\.html$/].nil?
|
95
|
+
FileUtils.mkdir_p(path)
|
96
|
+
path = File.join(path, "index.html")
|
97
|
+
end
|
98
|
+
|
99
|
+
File.open(path, 'w') do |f|
|
100
|
+
f.write(self.output)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def index?
|
107
|
+
basename == 'index'
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
data/lib/jekyll/pager.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Jekyll
|
2
|
+
class Pager
|
3
|
+
attr_reader :page, :per_page, :posts, :total_posts, :total_pages, :previous_page, :next_page
|
4
|
+
|
5
|
+
def self.calculate_pages(all_posts, per_page)
|
6
|
+
num_pages = all_posts.size / per_page.to_i
|
7
|
+
num_pages.abs + 1 if all_posts.size % per_page.to_i != 0
|
8
|
+
num_pages
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.pagination_enabled?(config, file)
|
12
|
+
file == 'index.html' && !config['paginate'].nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(config, page, all_posts, num_pages = nil)
|
16
|
+
@page = page
|
17
|
+
@per_page = config['paginate'].to_i
|
18
|
+
@total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page)
|
19
|
+
|
20
|
+
if @page > @total_pages
|
21
|
+
raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}"
|
22
|
+
end
|
23
|
+
|
24
|
+
init = (@page - 1) * @per_page
|
25
|
+
offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1)
|
26
|
+
|
27
|
+
@total_posts = all_posts.size
|
28
|
+
@posts = all_posts[init..offset]
|
29
|
+
@previous_page = @page != 1 ? @page - 1 : nil
|
30
|
+
@next_page = @page != @total_pages ? @page + 1 : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_hash
|
34
|
+
{
|
35
|
+
'page' => page,
|
36
|
+
'per_page' => per_page,
|
37
|
+
'posts' => posts,
|
38
|
+
'total_posts' => total_posts,
|
39
|
+
'total_pages' => total_pages,
|
40
|
+
'previous_page' => previous_page,
|
41
|
+
'next_page' => next_page
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/jekyll/post.rb
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
module Jekyll
|
2
|
+
|
3
|
+
class Post
|
4
|
+
include Comparable
|
5
|
+
include Convertible
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :lsi
|
9
|
+
end
|
10
|
+
|
11
|
+
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
|
12
|
+
|
13
|
+
# Post name validator. Post filenames must be like:
|
14
|
+
# 2008-11-05-my-awesome-post.textile
|
15
|
+
#
|
16
|
+
# Returns <Bool>
|
17
|
+
def self.valid?(name)
|
18
|
+
name =~ MATCHER
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_accessor :site, :date, :slug, :ext, :published, :data, :content, :output, :tags
|
22
|
+
attr_writer :categories
|
23
|
+
|
24
|
+
def categories
|
25
|
+
@categories ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
# Initialize this Post instance.
|
29
|
+
# +site+ is the Site
|
30
|
+
# +base+ is the String path to the dir containing the post file
|
31
|
+
# +name+ is the String filename of the post file
|
32
|
+
# +categories+ is an Array of Strings for the categories for this post
|
33
|
+
#
|
34
|
+
# Returns <Post>
|
35
|
+
def initialize(site, source, dir, name)
|
36
|
+
@site = site
|
37
|
+
@base = File.join(source, dir, '_posts')
|
38
|
+
@name = name
|
39
|
+
|
40
|
+
self.categories = dir.split('/').reject { |x| x.empty? }
|
41
|
+
self.process(name)
|
42
|
+
self.read_yaml(@base, name)
|
43
|
+
|
44
|
+
if self.data.has_key?('published') && self.data['published'] == false
|
45
|
+
self.published = false
|
46
|
+
else
|
47
|
+
self.published = true
|
48
|
+
end
|
49
|
+
|
50
|
+
if self.data.has_key?("tag")
|
51
|
+
self.tags = [self.data["tag"]]
|
52
|
+
elsif self.data.has_key?("tags")
|
53
|
+
self.tags = self.data['tags']
|
54
|
+
else
|
55
|
+
self.tags = []
|
56
|
+
end
|
57
|
+
|
58
|
+
if self.categories.empty?
|
59
|
+
if self.data.has_key?('category')
|
60
|
+
self.categories << self.data['category']
|
61
|
+
elsif self.data.has_key?('categories')
|
62
|
+
# Look for categories in the YAML-header, either specified as
|
63
|
+
# an array or a string.
|
64
|
+
if self.data['categories'].kind_of? String
|
65
|
+
self.categories = self.data['categories'].split
|
66
|
+
else
|
67
|
+
self.categories = self.data['categories']
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Spaceship is based on Post#date, slug
|
74
|
+
#
|
75
|
+
# Returns -1, 0, 1
|
76
|
+
def <=>(other)
|
77
|
+
cmp = self.date <=> other.date
|
78
|
+
if 0 == cmp
|
79
|
+
cmp = self.slug <=> other.slug
|
80
|
+
end
|
81
|
+
return cmp
|
82
|
+
end
|
83
|
+
|
84
|
+
# Extract information from the post filename
|
85
|
+
# +name+ is the String filename of the post file
|
86
|
+
#
|
87
|
+
# Returns nothing
|
88
|
+
def process(name)
|
89
|
+
m, cats, date, slug, ext = *name.match(MATCHER)
|
90
|
+
self.date = Time.parse(date)
|
91
|
+
self.slug = slug
|
92
|
+
self.ext = ext
|
93
|
+
end
|
94
|
+
|
95
|
+
# The generated directory into which the post will be placed
|
96
|
+
# upon generation. This is derived from the permalink or, if
|
97
|
+
# permalink is absent, set to the default date
|
98
|
+
# e.g. "/2008/11/05/" if the permalink style is :date, otherwise nothing
|
99
|
+
#
|
100
|
+
# Returns <String>
|
101
|
+
def dir
|
102
|
+
File.dirname(url)
|
103
|
+
end
|
104
|
+
|
105
|
+
# The full path and filename of the post.
|
106
|
+
# Defined in the YAML of the post body
|
107
|
+
# (Optional)
|
108
|
+
#
|
109
|
+
# Returns <String>
|
110
|
+
def permalink
|
111
|
+
self.data && self.data['permalink']
|
112
|
+
end
|
113
|
+
|
114
|
+
def template
|
115
|
+
case self.site.permalink_style
|
116
|
+
when :pretty
|
117
|
+
"/:categories/:year/:month/:day/:title/"
|
118
|
+
when :none
|
119
|
+
"/:categories/:title.html"
|
120
|
+
when :date
|
121
|
+
"/:categories/:year/:month/:day/:title.html"
|
122
|
+
else
|
123
|
+
self.site.permalink_style.to_s
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# The generated relative url of this post
|
128
|
+
# e.g. /2008/11/05/my-awesome-post.html
|
129
|
+
#
|
130
|
+
# Returns <String>
|
131
|
+
def url
|
132
|
+
return permalink if permalink
|
133
|
+
|
134
|
+
@url ||= {
|
135
|
+
"year" => date.strftime("%Y"),
|
136
|
+
"month" => date.strftime("%m"),
|
137
|
+
"day" => date.strftime("%d"),
|
138
|
+
"title" => CGI.escape(slug),
|
139
|
+
"categories" => categories.sort.join('/')
|
140
|
+
}.inject(template) { |result, token|
|
141
|
+
result.gsub(/:#{token.first}/, token.last)
|
142
|
+
}.gsub(/\/\//, "/")
|
143
|
+
end
|
144
|
+
|
145
|
+
# The UID for this post (useful in feeds)
|
146
|
+
# e.g. /2008/11/05/my-awesome-post
|
147
|
+
#
|
148
|
+
# Returns <String>
|
149
|
+
def id
|
150
|
+
File.join(self.dir, self.slug)
|
151
|
+
end
|
152
|
+
|
153
|
+
# Calculate related posts.
|
154
|
+
#
|
155
|
+
# Returns [<Post>]
|
156
|
+
def related_posts(posts)
|
157
|
+
return [] unless posts.size > 1
|
158
|
+
|
159
|
+
if self.site.lsi
|
160
|
+
self.class.lsi ||= begin
|
161
|
+
puts "Running the classifier... this could take a while."
|
162
|
+
lsi = Classifier::LSI.new
|
163
|
+
posts.each { |x| $stdout.print(".");$stdout.flush;lsi.add_item(x) }
|
164
|
+
puts ""
|
165
|
+
lsi
|
166
|
+
end
|
167
|
+
|
168
|
+
related = self.class.lsi.find_related(self.content, 11)
|
169
|
+
related - [self]
|
170
|
+
else
|
171
|
+
(posts - [self])[0..9]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Add any necessary layouts to this post
|
176
|
+
# +layouts+ is a Hash of {"name" => "layout"}
|
177
|
+
# +site_payload+ is the site payload hash
|
178
|
+
#
|
179
|
+
# Returns nothing
|
180
|
+
def render(layouts, site_payload)
|
181
|
+
# construct payload
|
182
|
+
payload =
|
183
|
+
{
|
184
|
+
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
|
185
|
+
"page" => self.to_liquid
|
186
|
+
}
|
187
|
+
payload = payload.deep_merge(site_payload)
|
188
|
+
|
189
|
+
do_layout(payload, layouts)
|
190
|
+
end
|
191
|
+
|
192
|
+
# Write the generated post file to the destination directory.
|
193
|
+
# +dest+ is the String path to the destination dir
|
194
|
+
#
|
195
|
+
# Returns nothing
|
196
|
+
def write(dest)
|
197
|
+
FileUtils.mkdir_p(File.join(dest, dir))
|
198
|
+
|
199
|
+
# The url needs to be unescaped in order to preserve the correct filename
|
200
|
+
path = File.join(dest, CGI.unescape(self.url))
|
201
|
+
|
202
|
+
# Write the post's files out into a directory named after the post
|
203
|
+
self.write_postfiles(path) if self.site.config['postfiles']
|
204
|
+
|
205
|
+
if template[/\.html$/].nil?
|
206
|
+
FileUtils.mkdir_p(path)
|
207
|
+
path = File.join(path, "index.html")
|
208
|
+
end
|
209
|
+
|
210
|
+
File.open(path, 'w') do |f|
|
211
|
+
f.write(self.output)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
# Write the files that the post depends on into its directory
|
217
|
+
#
|
218
|
+
# Returns nothing
|
219
|
+
def write_postfiles(dest)
|
220
|
+
glob = File.join("./_postfiles", File.basename(@name, File.extname(@name)), "*")
|
221
|
+
postfiles = Dir[glob]
|
222
|
+
if postfiles.any?
|
223
|
+
FileUtils.mkdir_p(dest)
|
224
|
+
postfiles.each{|pf| FileUtils.cp(pf, dest) }
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
|
229
|
+
# Convert this post into a Hash for use in Liquid templates.
|
230
|
+
#
|
231
|
+
# Returns <Hash>
|
232
|
+
def to_liquid
|
233
|
+
{ "title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
|
234
|
+
"url" => self.url,
|
235
|
+
"date" => self.date,
|
236
|
+
"id" => self.id,
|
237
|
+
"categories" => self.categories,
|
238
|
+
"next" => self.next,
|
239
|
+
"previous" => self.previous,
|
240
|
+
"tags" => self.tags,
|
241
|
+
"content" => self.content }.deep_merge(self.data)
|
242
|
+
end
|
243
|
+
|
244
|
+
def inspect
|
245
|
+
"<Post: #{self.id}>"
|
246
|
+
end
|
247
|
+
|
248
|
+
def next
|
249
|
+
pos = self.site.posts.index(self)
|
250
|
+
|
251
|
+
if pos && pos < self.site.posts.length-1
|
252
|
+
self.site.posts[pos+1]
|
253
|
+
else
|
254
|
+
nil
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
def previous
|
259
|
+
pos = self.site.posts.index(self)
|
260
|
+
if pos && pos > 0
|
261
|
+
self.site.posts[pos-1]
|
262
|
+
else
|
263
|
+
nil
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|