tkmr-jekyll 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/History.txt +127 -0
  2. data/README.textile +41 -0
  3. data/Rakefile +91 -0
  4. data/VERSION.yml +4 -0
  5. data/bin/jekyll +150 -0
  6. data/lib/jekyll.rb +85 -0
  7. data/lib/jekyll/albino.rb +122 -0
  8. data/lib/jekyll/converters/csv.rb +26 -0
  9. data/lib/jekyll/converters/mephisto.rb +79 -0
  10. data/lib/jekyll/converters/mt.rb +59 -0
  11. data/lib/jekyll/converters/textpattern.rb +50 -0
  12. data/lib/jekyll/converters/typo.rb +49 -0
  13. data/lib/jekyll/converters/wordpress.rb +54 -0
  14. data/lib/jekyll/convertible.rb +82 -0
  15. data/lib/jekyll/core_ext.rb +22 -0
  16. data/lib/jekyll/filters.rb +47 -0
  17. data/lib/jekyll/layout.rb +36 -0
  18. data/lib/jekyll/page.rb +70 -0
  19. data/lib/jekyll/pager.rb +45 -0
  20. data/lib/jekyll/post.rb +243 -0
  21. data/lib/jekyll/site.rb +265 -0
  22. data/lib/jekyll/tags/eval_assign.rb +25 -0
  23. data/lib/jekyll/tags/highlight.rb +56 -0
  24. data/lib/jekyll/tags/include.rb +31 -0
  25. data/test/helper.rb +27 -0
  26. data/test/source/_includes/sig.markdown +3 -0
  27. data/test/source/_layouts/default.html +27 -0
  28. data/test/source/_layouts/simple.html +1 -0
  29. data/test/source/_posts/2008-02-02-not-published.textile +8 -0
  30. data/test/source/_posts/2008-02-02-published.textile +8 -0
  31. data/test/source/_posts/2008-10-18-foo-bar.textile +8 -0
  32. data/test/source/_posts/2008-11-21-complex.textile +8 -0
  33. data/test/source/_posts/2008-12-03-permalinked-post.textile +9 -0
  34. data/test/source/_posts/2008-12-13-include.markdown +8 -0
  35. data/test/source/_posts/2009-01-27-array-categories.textile +10 -0
  36. data/test/source/_posts/2009-01-27-categories.textile +7 -0
  37. data/test/source/_posts/2009-01-27-category.textile +7 -0
  38. data/test/source/_posts/2009-03-12-hash-#1.markdown +6 -0
  39. data/test/source/category/_posts/2008-9-23-categories.textile +6 -0
  40. data/test/source/css/screen.css +76 -0
  41. data/test/source/foo/_posts/bar/2008-12-12-topical-post.textile +8 -0
  42. data/test/source/index.html +22 -0
  43. data/test/source/z_category/_posts/2008-9-23-categories.textile +6 -0
  44. data/test/suite.rb +9 -0
  45. data/test/test_filters.rb +49 -0
  46. data/test/test_generated_site.rb +38 -0
  47. data/test/test_post.rb +269 -0
  48. data/test/test_site.rb +65 -0
  49. data/test/test_tags.rb +116 -0
  50. metadata +167 -0
@@ -0,0 +1,82 @@
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---\s*\n/m
22
+ self.content = self.content[($1.size + 5)..-1]
23
+
24
+ self.data = YAML.load($1)
25
+ end
26
+ end
27
+
28
+ # Transform the contents based on the file extension.
29
+ #
30
+ # Returns nothing
31
+ def transform
32
+ case self.content_type
33
+ when 'textile'
34
+ self.ext = ".html"
35
+ self.content = self.site.textile(self.content)
36
+ when 'markdown'
37
+ self.ext = ".html"
38
+ self.content = self.site.markdown(self.content)
39
+ end
40
+ end
41
+
42
+ # Determine which formatting engine to use based on this convertible's
43
+ # extension
44
+ #
45
+ # Returns one of :textile, :markdown or :unknown
46
+ def content_type
47
+ case self.ext[1..-1]
48
+ when /textile/i
49
+ return 'textile'
50
+ when /markdown/i, /mkdn/i, /md/i
51
+ return 'markdown'
52
+ end
53
+ return 'unknown'
54
+ end
55
+
56
+ # Add any necessary layouts to this convertible document
57
+ # +layouts+ is a Hash of {"name" => "layout"}
58
+ # +site_payload+ is the site payload hash
59
+ #
60
+ # Returns nothing
61
+ def do_layout(payload, layouts)
62
+ info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
63
+
64
+ # render and transform content (this becomes the final content of the object)
65
+ payload["content_type"] = self.content_type
66
+ self.content = Liquid::Template.parse(self.content).render(payload, info)
67
+ self.transform
68
+
69
+ # output keeps track of what will finally be written
70
+ self.output = self.content
71
+
72
+ # recursively render layouts
73
+ layout = layouts[self.data["layout"]]
74
+ while layout
75
+ payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
76
+ self.output = Liquid::Template.parse(layout.content).render(payload, info)
77
+
78
+ layout = layouts[layout.data["layout"]]
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,22 @@
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
@@ -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
@@ -0,0 +1,70 @@
1
+ module Jekyll
2
+
3
+ class Page
4
+ include Convertible
5
+
6
+ attr_accessor :site
7
+ attr_accessor :ext
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.data = {}
24
+
25
+ self.process(name)
26
+ self.read_yaml(File.join(base, dir), name)
27
+ #self.transform
28
+ end
29
+
30
+ # Extract information from the page filename
31
+ # +name+ is the String filename of the page file
32
+ #
33
+ # Returns nothing
34
+ def process(name)
35
+ self.ext = File.extname(name)
36
+ end
37
+
38
+ # Add any necessary layouts to this post
39
+ # +layouts+ is a Hash of {"name" => "layout"}
40
+ # +site_payload+ is the site payload hash
41
+ #
42
+ # Returns nothing
43
+ def render(layouts, site_payload)
44
+ payload = {"page" => self.data}.deep_merge(site_payload)
45
+ do_layout(payload, layouts)
46
+ end
47
+
48
+ # Write the generated page file to the destination directory.
49
+ # +dest_prefix+ is the String path to the destination dir
50
+ # +dest_suffix+ is a suffix path to the destination dir
51
+ #
52
+ # Returns nothing
53
+ def write(dest_prefix, dest_suffix = nil)
54
+ dest = File.join(dest_prefix, @dir)
55
+ dest = File.join(dest, dest_suffix) if dest_suffix
56
+ FileUtils.mkdir_p(dest)
57
+
58
+ name = @name
59
+ if self.ext != ""
60
+ name = @name.split(".")[0..-2].join('.') + self.ext
61
+ end
62
+
63
+ path = File.join(dest, name)
64
+ File.open(path, 'w') do |f|
65
+ f.write(self.output)
66
+ end
67
+ end
68
+ end
69
+
70
+ end
@@ -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
@@ -0,0 +1,243 @@
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, :topics, :published, :data, :content, :output
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
+
42
+ parts = name.split('/')
43
+ self.topics = parts.size > 1 ? parts[0..-2] : []
44
+
45
+ self.process(name)
46
+ self.read_yaml(@base, name)
47
+
48
+ if self.data.has_key?('published') && self.data['published'] == false
49
+ self.published = false
50
+ else
51
+ self.published = true
52
+ end
53
+
54
+ if self.categories.empty?
55
+ if self.data.has_key?('category')
56
+ self.categories << self.data['category']
57
+ elsif self.data.has_key?('categories')
58
+ # Look for categories in the YAML-header, either specified as
59
+ # an array or a string.
60
+ if self.data['categories'].kind_of? String
61
+ self.categories = self.data['categories'].split
62
+ else
63
+ self.categories = self.data['categories']
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # Spaceship is based on Post#date
70
+ #
71
+ # Returns -1, 0, 1
72
+ def <=>(other)
73
+ self.date <=> other.date
74
+ end
75
+
76
+ # Extract information from the post filename
77
+ # +name+ is the String filename of the post file
78
+ #
79
+ # Returns nothing
80
+ def process(name)
81
+ m, cats, date, slug, ext = *name.match(MATCHER)
82
+ self.date = Time.parse(date)
83
+ self.slug = slug
84
+ self.ext = ext
85
+ end
86
+
87
+ # The generated directory into which the post will be placed
88
+ # upon generation. This is derived from the permalink or, if
89
+ # permalink is absent, set to the default date
90
+ # e.g. "/2008/11/05/" if the permalink style is :date, otherwise nothing
91
+ #
92
+ # Returns <String>
93
+ def dir
94
+ File.dirname(url)
95
+ end
96
+
97
+ # The full path and filename of the post.
98
+ # Defined in the YAML of the post body
99
+ # (Optional)
100
+ #
101
+ # Returns <String>
102
+ def permalink
103
+ self.data && self.data['permalink']
104
+ end
105
+
106
+ def template
107
+ case self.site.permalink_style
108
+ when :pretty
109
+ "/:categories/:year/:month/:day/:title"
110
+ when :none
111
+ "/:categories/:title.html"
112
+ when :date
113
+ "/:categories/:year/:month/:day/:title.html"
114
+ else
115
+ self.site.permalink_style.to_s
116
+ end
117
+ end
118
+
119
+ # The generated relative url of this post
120
+ # e.g. /2008/11/05/my-awesome-post.html
121
+ #
122
+ # Returns <String>
123
+ def url
124
+ return permalink if permalink
125
+
126
+ @url ||= {
127
+ "year" => date.strftime("%Y"),
128
+ "month" => date.strftime("%m"),
129
+ "day" => date.strftime("%d"),
130
+ "title" => CGI.escape(slug),
131
+ "categories" => categories.sort.join('/')
132
+ }.inject(template) { |result, token|
133
+ result.gsub(/:#{token.first}/, token.last)
134
+ }.gsub(/\/\//, "/")
135
+ end
136
+
137
+ # The UID for this post (useful in feeds)
138
+ # e.g. /2008/11/05/my-awesome-post
139
+ #
140
+ # Returns <String>
141
+ def id
142
+ File.join(self.dir, self.slug)
143
+ end
144
+
145
+ # Calculate related posts.
146
+ #
147
+ # Returns [<Post>]
148
+ def related_posts(posts)
149
+ return [] unless posts.size > 1
150
+
151
+ if self.site.lsi
152
+ self.class.lsi ||= begin
153
+ puts "Running the classifier... this could take a while."
154
+ lsi = Classifier::LSI.new
155
+ posts.each { |x| $stdout.print(".");$stdout.flush;lsi.add_item(x) }
156
+ puts ""
157
+ lsi
158
+ end
159
+
160
+ related = self.class.lsi.find_related(self.content, 11)
161
+ related - [self]
162
+ else
163
+ (posts - [self])[0..9]
164
+ end
165
+ end
166
+
167
+ # Add any necessary layouts to this post
168
+ # +layouts+ is a Hash of {"name" => "layout"}
169
+ # +site_payload+ is the site payload hash
170
+ #
171
+ # Returns nothing
172
+ def render(layouts, site_payload)
173
+ # construct payload
174
+ payload =
175
+ {
176
+ "site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
177
+ "page" => self.to_liquid
178
+ }
179
+ payload = payload.deep_merge(site_payload)
180
+
181
+ do_layout(payload, layouts)
182
+ end
183
+
184
+ # Write the generated post file to the destination directory.
185
+ # +dest+ is the String path to the destination dir
186
+ #
187
+ # Returns nothing
188
+ def write(dest)
189
+ FileUtils.mkdir_p(File.join(dest, dir))
190
+
191
+ # The url needs to be unescaped in order to preserve the correct filename
192
+ path = File.join(dest, CGI.unescape(self.url))
193
+
194
+ if template[/\.html$/].nil?
195
+ FileUtils.mkdir_p(path)
196
+ path = File.join(path, "index.html")
197
+ end
198
+
199
+ File.open(path, 'w') do |f|
200
+ f.write(self.output)
201
+ end
202
+ end
203
+
204
+ # Convert this post into a Hash for use in Liquid templates.
205
+ #
206
+ # Returns <Hash>
207
+ def to_liquid
208
+ { "title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
209
+ "url" => self.url,
210
+ "date" => self.date,
211
+ "id" => self.id,
212
+ "topics" => self.topics,
213
+ "categories" => self.categories,
214
+ "next" => self.next,
215
+ "previous" => self.previous,
216
+ "content" => self.content }.deep_merge(self.data)
217
+ end
218
+
219
+ def inspect
220
+ "<Post: #{self.id}>"
221
+ end
222
+
223
+ def next
224
+ pos = self.site.posts.index(self)
225
+
226
+ if pos && pos < self.site.posts.length-1
227
+ self.site.posts[pos+1]
228
+ else
229
+ nil
230
+ end
231
+ end
232
+
233
+ def previous
234
+ pos = self.site.posts.index(self)
235
+ if pos && pos > 0
236
+ self.site.posts[pos-1]
237
+ else
238
+ nil
239
+ end
240
+ end
241
+ end
242
+
243
+ end