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,265 @@
1
+ module Jekyll
2
+
3
+ class Site
4
+ attr_accessor :config, :layouts, :posts, :categories, :exclude
5
+ attr_accessor :source, :dest, :lsi, :pygments, :permalink_style
6
+
7
+ # Initialize the site
8
+ # +config+ is a Hash containing site configurations details
9
+ #
10
+ # Returns <Site>
11
+ def initialize(config)
12
+ self.config = config.clone
13
+
14
+ self.source = config['source']
15
+ self.dest = config['destination']
16
+ self.lsi = config['lsi']
17
+ self.pygments = config['pygments']
18
+ self.permalink_style = config['permalink'].to_sym
19
+ self.exclude = config['exclude'] || []
20
+
21
+ self.reset
22
+ self.setup
23
+ end
24
+
25
+ def reset
26
+ self.layouts = {}
27
+ self.posts = []
28
+ self.categories = Hash.new { |hash, key| hash[key] = Array.new }
29
+ end
30
+
31
+ def setup
32
+ # Check to see if LSI is enabled.
33
+ require 'classifier' if self.lsi
34
+
35
+ # Set the Markdown interpreter (and Maruku self.config, if necessary)
36
+ case self.config['markdown']
37
+ when 'rdiscount'
38
+ begin
39
+ require 'rdiscount'
40
+
41
+ def markdown(content)
42
+ RDiscount.new(content).to_html
43
+ end
44
+
45
+ rescue LoadError
46
+ puts 'You must have the rdiscount gem installed first'
47
+ end
48
+ when 'maruku'
49
+ begin
50
+ require 'maruku'
51
+
52
+ def markdown(content)
53
+ Maruku.new(content).to_html
54
+ end
55
+
56
+ if self.config['maruku']['use_divs']
57
+ require 'maruku/ext/div'
58
+ puts 'Maruku: Using extended syntax for div elements.'
59
+ end
60
+
61
+ if self.config['maruku']['use_tex']
62
+ require 'maruku/ext/math'
63
+ puts "Maruku: Using LaTeX extension. Images in `#{self.config['maruku']['png_dir']}`."
64
+
65
+ # Switch off MathML output
66
+ MaRuKu::Globals[:html_math_output_mathml] = false
67
+ MaRuKu::Globals[:html_math_engine] = 'none'
68
+
69
+ # Turn on math to PNG support with blahtex
70
+ # Resulting PNGs stored in `images/latex`
71
+ MaRuKu::Globals[:html_math_output_png] = true
72
+ MaRuKu::Globals[:html_png_engine] = self.config['maruku']['png_engine']
73
+ MaRuKu::Globals[:html_png_dir] = self.config['maruku']['png_dir']
74
+ MaRuKu::Globals[:html_png_url] = self.config['maruku']['png_url']
75
+ end
76
+ rescue LoadError
77
+ puts "The maruku gem is required for markdown support!"
78
+ end
79
+ end
80
+ end
81
+
82
+ def textile(content)
83
+ RedCloth.new(content).to_html
84
+ end
85
+
86
+ # Do the actual work of processing the site and generating the
87
+ # real deal.
88
+ #
89
+ # Returns nothing
90
+ def process
91
+ self.reset
92
+ self.read_layouts
93
+ self.transform_pages
94
+ self.write_posts
95
+ end
96
+
97
+ # Read all the files in <source>/_layouts into memory for later use.
98
+ #
99
+ # Returns nothing
100
+ def read_layouts
101
+ base = File.join(self.source, "_layouts")
102
+ entries = []
103
+ Dir.chdir(base) { entries = filter_entries(Dir['*.*']) }
104
+
105
+ entries.each do |f|
106
+ name = f.split(".")[0..-2].join(".")
107
+ self.layouts[name] = Layout.new(self, base, f)
108
+ end
109
+ rescue Errno::ENOENT => e
110
+ # ignore missing layout dir
111
+ end
112
+
113
+ # Read all the files in <base>/_posts and create a new Post object with each one.
114
+ #
115
+ # Returns nothing
116
+ def read_posts(dir)
117
+ base = File.join(self.source, dir, '_posts')
118
+ entries = []
119
+ Dir.chdir(base) { entries = filter_entries(Dir['**/*']) }
120
+
121
+ # first pass processes, but does not yet render post content
122
+ entries.each do |f|
123
+ if Post.valid?(f)
124
+ post = Post.new(self, self.source, dir, f)
125
+
126
+ if post.published
127
+ self.posts << post
128
+ post.categories.each { |c| self.categories[c] << post }
129
+ end
130
+ end
131
+ end
132
+
133
+ self.posts.sort!
134
+
135
+ # second pass renders each post now that full site payload is available
136
+ self.posts.each do |post|
137
+ post.render(self.layouts, site_payload)
138
+ end
139
+
140
+ self.categories.values.map { |cats| cats.sort! { |a, b| b <=> a} }
141
+ rescue Errno::ENOENT => e
142
+ # ignore missing layout dir
143
+ end
144
+
145
+ # Write each post to <dest>/<year>/<month>/<day>/<slug>
146
+ #
147
+ # Returns nothing
148
+ def write_posts
149
+ self.posts.each do |post|
150
+ post.write(self.dest)
151
+ end
152
+ end
153
+
154
+ # Copy all regular files from <source> to <dest>/ ignoring
155
+ # any files/directories that are hidden or backup files (start
156
+ # with "." or "#" or end with "~") or contain site content (start with "_")
157
+ # unless they are "_posts" directories or web server files such as
158
+ # '.htaccess'
159
+ # The +dir+ String is a relative path used to call this method
160
+ # recursively as it descends through directories
161
+ #
162
+ # Returns nothing
163
+ def transform_pages(dir = '')
164
+ base = File.join(self.source, dir)
165
+ entries = filter_entries(Dir.entries(base))
166
+ directories = entries.select { |e| File.directory?(File.join(base, e)) }
167
+ files = entries.reject { |e| File.directory?(File.join(base, e)) }
168
+
169
+ # we need to make sure to process _posts *first* otherwise they
170
+ # might not be available yet to other templates as {{ site.posts }}
171
+ if directories.include?('_posts')
172
+ directories.delete('_posts')
173
+ read_posts(dir)
174
+ end
175
+
176
+ [directories, files].each do |entries|
177
+ entries.each do |f|
178
+ if File.directory?(File.join(base, f))
179
+ next if self.dest.sub(/\/$/, '') == File.join(base, f)
180
+ transform_pages(File.join(dir, f))
181
+ elsif Pager.pagination_enabled?(self.config, f)
182
+ paginate_posts(f, dir)
183
+ else
184
+ first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
185
+
186
+ if first3 == "---"
187
+ # file appears to have a YAML header so process it as a page
188
+ page = Page.new(self, self.source, dir, f)
189
+ page.render(self.layouts, site_payload)
190
+ page.write(self.dest)
191
+ else
192
+ # otherwise copy the file without transforming it
193
+ FileUtils.mkdir_p(File.join(self.dest, dir))
194
+ FileUtils.cp(File.join(self.source, dir, f), File.join(self.dest, dir, f))
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ # Constructs a hash map of Posts indexed by the specified Post attribute
202
+ #
203
+ # Returns {post_attr => [<Post>]}
204
+ def post_attr_hash(post_attr)
205
+ # Build a hash map based on the specified post attribute ( post attr => array of posts )
206
+ # then sort each array in reverse order
207
+ hash = Hash.new { |hash, key| hash[key] = Array.new }
208
+ self.posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
209
+ hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a} }
210
+ return hash
211
+ end
212
+
213
+ # The Hash payload containing site-wide data
214
+ #
215
+ # Returns {"site" => {"time" => <Time>,
216
+ # "posts" => [<Post>],
217
+ # "categories" => [<Post>],
218
+ # "topics" => [<Post>] }}
219
+ def site_payload
220
+ {"site" => {
221
+ "time" => Time.now,
222
+ "posts" => self.posts.sort { |a,b| b <=> a },
223
+ "categories" => post_attr_hash('categories'),
224
+ "topics" => post_attr_hash('topics')
225
+ }}
226
+ end
227
+
228
+ # Filter out any files/directories that are hidden or backup files (start
229
+ # with "." or "#" or end with "~") or contain site content (start with "_")
230
+ # unless they are "_posts" directories or web server files such as
231
+ # '.htaccess'
232
+ def filter_entries(entries)
233
+ entries = entries.reject do |e|
234
+ unless ['_posts', '.htaccess'].include?(e)
235
+ ['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || self.exclude.include?(e)
236
+ end
237
+ end
238
+ end
239
+
240
+ # Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
241
+ # and adds more wite-wide data
242
+ #
243
+ # {"paginator" => { "page" => <Number>,
244
+ # "per_page" => <Number>,
245
+ # "posts" => [<Post>],
246
+ # "total_posts" => <Number>,
247
+ # "total_pages" => <Number>,
248
+ # "previous_page" => <Number>,
249
+ # "next_page" => <Number> }}
250
+ def paginate_posts(file, dir)
251
+ all_posts = self.posts.sort { |a,b| b <=> a }
252
+ page = Page.new(self, self.source, dir, file)
253
+
254
+ pages = Pager.calculate_pages(all_posts, self.config['paginate'].to_i)
255
+
256
+ (1..pages).each do |num_page|
257
+ pager = Pager.new(self.config, num_page, all_posts, pages)
258
+
259
+ page.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash}))
260
+ suffix = "page#{num_page}" if num_page > 1
261
+ page.write(self.dest, suffix)
262
+ end
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,25 @@
1
+ module Jekyll
2
+ class EvalAssignTag < Liquid::Tag
3
+ Syntax = /([^\s]*)\s*=\s*(.*)/
4
+
5
+ def initialize(tag_name, markup, tokens)
6
+ if markup =~ Syntax
7
+ @to = $1.to_s.chomp
8
+ @from = $2
9
+ else
10
+ raise SyntaxError.new("SyntaxError in 'evalassign' - Valid syntax: evalassign [var] = [ruby expression]")
11
+ end
12
+ super
13
+ end
14
+
15
+ def render(context)
16
+ post = context.scopes[0]["post"] if context.scopes[0]
17
+ site = context.scopes[1]["site"] if context.scopes[1]
18
+ page = context.scopes[1]["page"] if context.scopes[1]
19
+ context.scopes.last[@to.to_s.chomp] = eval(@from)
20
+ ""
21
+ end
22
+ end
23
+ end
24
+
25
+ Liquid::Template.register_tag("evalassign", Jekyll::EvalAssignTag)
@@ -0,0 +1,56 @@
1
+ module Jekyll
2
+
3
+ class HighlightBlock < Liquid::Block
4
+ include Liquid::StandardFilters
5
+
6
+ # we need a language, but the linenos argument is optional.
7
+ SYNTAX = /(\w+)\s?(:?linenos)?\s?/
8
+
9
+ def initialize(tag_name, markup, tokens)
10
+ super
11
+ if markup =~ SYNTAX
12
+ @lang = $1
13
+ if defined? $2
14
+ # additional options to pass to Albino.
15
+ @options = { 'O' => 'linenos=inline' }
16
+ else
17
+ @options = {}
18
+ end
19
+ else
20
+ raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]")
21
+ end
22
+ end
23
+
24
+ def render(context)
25
+ if context.registers[:site].pygments
26
+ render_pygments(context, super.to_s)
27
+ else
28
+ render_codehighlighter(context, super.to_s)
29
+ end
30
+ end
31
+
32
+ def render_pygments(context, code)
33
+ if context["content_type"] == "markdown"
34
+ return "\n" + Albino.new(code, @lang).to_s(@options) + "\n"
35
+ elsif context["content_type"] == "textile"
36
+ return "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
37
+ else
38
+ return Albino.new(code, @lang).to_s(@options)
39
+ end
40
+ end
41
+
42
+ def render_codehighlighter(context, code)
43
+ #The div is required because RDiscount blows ass
44
+ <<-HTML
45
+ <div>
46
+ <pre>
47
+ <code class='#{@lang}'>#{h(code).strip}</code>
48
+ </pre>
49
+ </div>
50
+ HTML
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)
@@ -0,0 +1,31 @@
1
+ module Jekyll
2
+
3
+ class IncludeTag < Liquid::Tag
4
+ def initialize(tag_name, file, tokens)
5
+ super
6
+ @file = file.strip
7
+ end
8
+
9
+ def render(context)
10
+ if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
11
+ return "Include file '#{@file}' contains invalid characters or sequences"
12
+ end
13
+
14
+ Dir.chdir(File.join(context.registers[:site].source, '_includes')) do
15
+ choices = Dir['**/*'].reject { |x| File.symlink?(x) }
16
+ if choices.include?(@file)
17
+ source = File.read(@file)
18
+ partial = Liquid::Template.parse(source)
19
+ context.stack do
20
+ partial.render(context)
21
+ end
22
+ else
23
+ "Included file '#{@file}' not found in _includes directory"
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ Liquid::Template.register_tag('include', Jekyll::IncludeTag)
data/test/helper.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ gem 'RedCloth', '= 4.1.0'
3
+
4
+ require File.join(File.dirname(__FILE__), *%w[.. lib jekyll])
5
+
6
+ require 'test/unit'
7
+ require 'redgreen'
8
+ require 'shoulda'
9
+ require 'rr'
10
+
11
+ include Jekyll
12
+
13
+ class Test::Unit::TestCase
14
+ include RR::Adapters::TestUnit
15
+
16
+ def dest_dir(*subdirs)
17
+ File.join(File.dirname(__FILE__), 'dest', *subdirs)
18
+ end
19
+
20
+ def source_dir(*subdirs)
21
+ File.join(File.dirname(__FILE__), 'source', *subdirs)
22
+ end
23
+
24
+ def clear_dest
25
+ FileUtils.rm_rf(dest_dir)
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ --
2
+ Tom Preston-Werner
3
+ github.com/mojombo
@@ -0,0 +1,27 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7
+ <title>{{ page.title }}</title>
8
+ <meta name="author" content="<%= @page.author %>" />
9
+
10
+ <!-- CodeRay syntax highlighting CSS -->
11
+ <link rel="stylesheet" href="/css/coderay.css" type="text/css" />
12
+
13
+ <!-- Homepage CSS -->
14
+ <link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection" />
15
+ </head>
16
+ <body>
17
+
18
+ <div class="site">
19
+ <div class="title">
20
+ Tom Preston-Werner
21
+ </div>
22
+
23
+ {{ content }}
24
+ </div>
25
+
26
+ </body>
27
+ </html>
@@ -0,0 +1 @@
1
+ <<< {{ content }} >>>