oaktree 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/oaktree.rb ADDED
@@ -0,0 +1,144 @@
1
+ require 'date'
2
+ require 'fileutils'
3
+ require 'pathname'
4
+
5
+ require 'oaktree/specification'
6
+ require 'oaktree/post_data'
7
+ require 'oaktree/template'
8
+
9
+ # Probably violating some rule on naming by calling this oaktree.rb instead of
10
+ # oak_tree.rb. Oh well.
11
+
12
+ # The central blog class (also just called a tree from time to time)
13
+ class OakTree
14
+
15
+ VERSION = '0.4.4'
16
+
17
+ @@EMPTY_DIR_ENTRIES = ['.', '..']
18
+
19
+ def initialize spec
20
+ @spec = spec
21
+
22
+ @posts = []
23
+
24
+ sync_posts
25
+ end
26
+
27
+ def blogspec
28
+ @spec
29
+ end
30
+
31
+ def posts
32
+ sync_posts
33
+ return @posts
34
+ end
35
+
36
+ # Generates and writes the HTML files for the blog.
37
+ # If force_rebuild is false, it will only rebuild pages with posts that have
38
+ # changed, otherwise it rebuilds everything.
39
+ # This does not check for changes to templates or other content -- if you
40
+ # make any changes to templates, you must force a rebuild.
41
+ def generate force_build = false
42
+ blog_template = Template::Blog.new self
43
+
44
+ skipped_files = []
45
+ new_files = []
46
+ updated_files = []
47
+ old_files = Dir.glob('public/**/*.html')
48
+
49
+ blog_template.modes.each {
50
+ |mode|
51
+
52
+ blog_template.mode = mode
53
+
54
+ (1..blog_template.pages).each { |page|
55
+ blog_template.page = page
56
+ path = blog_template.local_path
57
+ pretty_path = Pathname.new(path).relative_path_from(Pathname.new(@spec.blog_root)).to_s
58
+
59
+ if old_files.include? pretty_path
60
+ old_files.delete pretty_path
61
+ end
62
+
63
+ mtime = File.exists?(path) ? File.mtime(path) : nil
64
+ needs_update = force_build || mtime.nil?
65
+
66
+ if ! needs_update
67
+ needs_update = blog_template.posts.any? {
68
+ |post|
69
+ mtime < File.mtime(post.post_data.source_path)
70
+ }
71
+
72
+ if ! needs_update
73
+ skipped_files << path
74
+ next
75
+ end
76
+ end
77
+
78
+ dir = File.dirname(path)
79
+ FileUtils.mkdir_p dir unless File.directory? dir
80
+
81
+ if File.exists? path
82
+ updated_files << pretty_path
83
+ else
84
+ new_files << pretty_path
85
+ end
86
+
87
+ r = nil
88
+ File.open(path, 'w') {
89
+ |io|
90
+ io.write blog_template.render
91
+ }
92
+ }
93
+ }
94
+
95
+ updated_files.each { |path| puts "* #{path}" }
96
+
97
+ new_files.each { |path| puts "+ #{path}"}
98
+
99
+ old_files.each {
100
+ |path|
101
+ puts "- #{path}"
102
+ File.unlink path
103
+ dir = File.dirname path
104
+ if Dir.entries(dir) == @@EMPTY_DIR_ENTRIES
105
+ Dir.unlink dir
106
+ end
107
+ }
108
+ end
109
+
110
+ private
111
+
112
+ def sync_posts
113
+ spec = self.blogspec
114
+ entries = Dir.foreach(blogspec.sources_root).reject { |e| e.start_with? '.' }
115
+ postnames = @posts.map(&:source_name)
116
+
117
+ new_posts = entries.reject {
118
+ |entry|
119
+ postnames.include? entry
120
+ }.map {
121
+ |entry|
122
+ PostData.new entry, spec
123
+ }
124
+
125
+ if ! new_posts.empty?
126
+ @posts.each(&:sync_changes).concat(new_posts)
127
+
128
+ if spec.reversed
129
+ @posts.sort! {
130
+ |left, right|
131
+ left.time <=> right.time
132
+ }
133
+ else
134
+ @posts.sort! {
135
+ |left, right|
136
+ right.time <=> left.time
137
+ }
138
+ end
139
+ end
140
+
141
+ self
142
+ end
143
+
144
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oaktree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.4
5
+ platform: ruby
6
+ authors:
7
+ - Noel Cower
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mustache
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.99'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.99'
27
+ - !ruby/object:Gem::Dependency
28
+ name: kramdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0.13'
41
+ description: |
42
+ OakTree static HTML blog generation tool.
43
+
44
+ If you're smart, you won't use this. Otherwise, run 'oak' and read the
45
+ instructions. It's fairly simple to use.
46
+ email: ncower@gmail.com
47
+ executables:
48
+ - oak
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/oaktree.rb
53
+ - lib/oaktree/kramdown/oak_html.rb
54
+ - lib/oaktree/post_data.rb
55
+ - lib/oaktree/specification.rb
56
+ - lib/oaktree/template.rb
57
+ - lib/oaktree/template/base.rb
58
+ - lib/oaktree/template/blog.rb
59
+ - lib/oaktree/template/post.rb
60
+ - lib/oaktree/template/post_archive.rb
61
+ - bin/oak
62
+ - COPYING
63
+ - README.md
64
+ - Rakefile
65
+ homepage: http://spifftastic.net/oaktree/
66
+ licenses:
67
+ - WTFPL-2
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.0.0
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: OakTree static HTML blog
90
+ test_files: []