henshin 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +5 -1
  2. data/README.markdown +9 -46
  3. data/Rakefile +2 -1
  4. data/VERSION +1 -1
  5. data/bin/henshin +11 -12
  6. data/henshin.gemspec +20 -27
  7. data/lib/henshin.rb +21 -98
  8. data/lib/henshin/archive.rb +87 -115
  9. data/{bin → lib/henshin/exec}/files.rb +0 -0
  10. data/lib/henshin/ext.rb +35 -58
  11. data/lib/henshin/gen.rb +83 -68
  12. data/lib/henshin/labels.rb +144 -0
  13. data/lib/henshin/plugin.rb +70 -33
  14. data/lib/henshin/plugins/highlight.rb +19 -26
  15. data/lib/henshin/plugins/liquid.rb +50 -52
  16. data/lib/henshin/plugins/maruku.rb +14 -16
  17. data/lib/henshin/plugins/sass.rb +19 -23
  18. data/lib/henshin/plugins/textile.rb +14 -16
  19. data/lib/henshin/post.rb +67 -120
  20. data/lib/henshin/site.rb +154 -131
  21. data/lib/henshin/static.rb +10 -8
  22. data/test/helper.rb +20 -8
  23. data/test/site/css/{includes/reset.sass → _reset.sass} +0 -0
  24. data/test/site/css/screen.sass +1 -1
  25. data/test/site/index.html +2 -3
  26. data/test/site/layouts/archive_date.html +5 -4
  27. data/test/site/layouts/archive_month.html +10 -5
  28. data/test/site/layouts/archive_year.html +13 -6
  29. data/test/site/layouts/category_page.html +7 -7
  30. data/test/site/layouts/main.html +3 -4
  31. data/test/site/layouts/post.html +1 -1
  32. data/test/site/layouts/tag_index.html +3 -2
  33. data/test/site/layouts/tag_page.html +6 -6
  34. data/test/site/options.yaml +2 -2
  35. data/test/site/plugins/test.rb +1 -3
  36. data/test/site/posts/Testing-Stuff.markdown +1 -1
  37. data/test/site/posts/Textile-Test.textile +1 -1
  38. data/test/site/posts/lorem-ipsum.markdown +1 -1
  39. data/test/site/posts/same-date.markdown +1 -1
  40. data/test/{test_henshin.rb → suite.rb} +0 -1
  41. data/test/test_gen.rb +98 -0
  42. data/test/test_options.rb +34 -19
  43. data/test/test_post.rb +67 -0
  44. data/test/test_site.rb +159 -46
  45. data/test/test_static.rb +13 -0
  46. metadata +53 -32
  47. data/lib/henshin/categories.rb +0 -29
  48. data/lib/henshin/tags.rb +0 -28
  49. data/test/test_archives.rb +0 -27
  50. data/test/test_categories.rb +0 -0
  51. data/test/test_gens.rb +0 -54
  52. data/test/test_layouts.rb +0 -14
  53. data/test/test_posts.rb +0 -75
  54. data/test/test_statics.rb +0 -0
  55. data/test/test_tags.rb +0 -0
  56. data/test/text_exts.rb +0 -0
data/.gitignore CHANGED
@@ -20,4 +20,8 @@ pkg
20
20
  .yardoc
21
21
 
22
22
  ## PROJECT::SPECIFIC
23
- _site/
23
+ _site/
24
+ .sass-cache/
25
+ docs-github/
26
+ hmm/
27
+ doc/
@@ -1,60 +1,22 @@
1
1
  # Henshin
2
2
 
3
- Henshin is a new static site generator, yeah another one. I wasn't going to release it yet but then I saw [awestruct](http://awestruct.org/) and thought I may as well now. Oh and this was totally inspired by the amazing [jekyll](http://github.com/mojombo/jekyll) (in case you hadn't guessed).
4
-
5
-
6
- ## Main Features
7
-
8
- - Generates posts, etc.
9
- - More control with settings (see below)
10
- - A plugin system (not great at the moment, still working on how best to implement it)
11
-
12
- ## Future Goals
13
-
14
- - Easy tag, category and archive pages
15
- - Default templates so you can just type `henshin` with any folder of text files and create a quick site
3
+ Henshin is a static site generator. It takes in posts, or just pages, runs them through plugins and layouts and gives you a folder to put on a webserver. It makes it easy to write archive pages, tags and categories.
16
4
 
17
5
 
18
6
  ## How To
19
7
 
20
- Install by typing `(sudo) gem install henshin`
8
+ Install with
21
9
 
22
- Then create a folder for your site, in this you'll probably want to create two folders `layouts` and `posts`, to put in your layouts and posts.
10
+ (sudo) gem install henshin
23
11
 
24
- Next create an `index.html` file and your kind of done, unless of course you want some content.
12
+ Then create a new site
25
13
 
26
- Build the site by running `henshin` from the command line. Help available with `henshin -h`
14
+ cd [some directory]
15
+ henshin create
27
16
 
28
- ### YAML Frontmatter & Options.yaml
17
+ And build your new site with
29
18
 
30
- You can create an optional `options.yaml` file at the root of your site, here are the options so far:
31
-
32
- title: [title of your site]
33
- description: [description for your site]
34
- author: [your name]
35
-
36
- layout: [the default layout to use]
37
-
38
- post_name: [the way the post name is parsed]
39
- eg. '{title-with-dashes}.{extension}
40
-
41
- you can use:
42
- {title}
43
- {title-with-dashes}
44
- {date}
45
- {date-time}
46
- {extension}
47
-
48
- permalink: [the way you want the permalink to look]
49
- eg. '/{year}/{month}/{date}-{title}/index.html
50
- these are the only options at the moment for permalink
51
-
52
- plugins: [array of plugins to
53
-
54
-
55
- ### Plugins
56
-
57
- The only plugins at the moment are the ones included (liquid, maruku, sass and pygments) of which only liquid and maruku work at the moment. In the future you will be able to add your own plugins, probably in a plugins folder. If you want to make your own just look at the others, it's pretty simple to work out!
19
+ henshin
58
20
 
59
21
 
60
22
  ## Note on Patches/Pull Requests
@@ -67,6 +29,7 @@ The only plugins at the moment are the ones included (liquid, maruku, sass and p
67
29
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
68
30
  - Send me a pull request. Bonus points for topic branches.
69
31
 
32
+
70
33
  ## License
71
34
 
72
35
  Copyright (c) 2010 Joshua Hawxwell. See LICENSE for details.
data/Rakefile CHANGED
@@ -14,6 +14,7 @@ begin
14
14
  gem.add_dependency "directory_watcher", ">= 1.3.1"
15
15
  gem.add_dependency "maruku", ">= 0.6.0"
16
16
  gem.add_dependency "liquid", ">= 2.0.0"
17
+ gem.add_dependency "parsey", ">= 0.1.3"
17
18
  gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
18
19
  gem.add_development_dependency "yard", ">= 0"
19
20
  end
@@ -24,7 +25,7 @@ end
24
25
 
25
26
  require 'rake/testtask'
26
27
  Rake::TestTask.new(:test) do |test|
27
- test.libs << 'lib' << 'test'
28
+ test.libs << 'test'
28
29
  test.pattern = 'test/**/test_*.rb'
29
30
  test.verbose = true
30
31
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.dirname(__FILE__) + '/../lib/henshin'
4
- require File.dirname(__FILE__) + '/files.rb'
3
+ require 'henshin'
4
+ require 'henshin/exec/files'
5
+
5
6
  require 'optparse'
6
7
  require 'fileutils'
7
8
 
@@ -62,19 +63,18 @@ else
62
63
  opts.parse!
63
64
 
64
65
  if ARGV[0]
65
- override[:root] = ARGV[0].split(':')[0]
66
- override[:target] = ARGV[0].split(':')[1] if ARGV[0].split(':')[1]
66
+ override['root'] = ARGV[0].split(':')[0]
67
+ override['target'] = ARGV[0].split(':')[1] if ARGV[0].split(':')[1]
67
68
  end
68
69
 
69
70
 
70
- config = Henshin.configure(override)
71
- site = Henshin::Site.new(config)
71
+ site = Henshin::Site.new(override)
72
72
  unless build[:auto]
73
73
  start = Time.now
74
74
  # build normally
75
75
  puts "Building site..."
76
76
  site.build
77
- puts "Site created in #{config[:target]} (#{Time.now - start}s)"
77
+ puts "Site created in #{site.config['target']} (#{Time.now - start}s)"
78
78
  end
79
79
 
80
80
  threads = []
@@ -116,14 +116,14 @@ else
116
116
  puts "", "Auto-build initiated..."
117
117
 
118
118
  # build the glob pattern
119
- gl = Dir[ File.join(config[:root], '*')].select { |x| File.directory?(x) }
119
+ gl = Dir[ File.join(site.config['root'], '*')].select { |x| File.directory?(x) }
120
120
  ['/_site', '/plugins'].each do |r|
121
- gl = gl.select {|i| !i.include?( File.join(config[:root], r) )}
121
+ gl = gl.select {|i| !i.include?( File.join(site.config['root'], r) )}
122
122
  end
123
- gl.collect! {|x| "#{x}/**/*"[config[:root].size+1..-1]}
123
+ gl.collect! {|x| "#{x}/**/*"[site.config['root'].size+1..-1]}
124
124
  gl += ['*']
125
125
 
126
- dw = DirectoryWatcher.new config[:root], :glob => gl
126
+ dw = DirectoryWatcher.new(site.config['root'], :glob => gl)
127
127
  dw.interval = 1
128
128
  dw.add_observer do |*args|
129
129
  if args.size > 1
@@ -132,7 +132,6 @@ else
132
132
  puts "rebuilding -> #{args[0].path}"
133
133
  end
134
134
  site.build
135
- puts "rebuilt..."
136
135
  end
137
136
 
138
137
  threads << Thread.new { dw.start }
@@ -5,14 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{henshin}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hawx"]
12
- s.date = %q{2010-06-22}
12
+ s.date = %q{2010-07-17}
13
+ s.default_executable = %q{henshin}
13
14
  s.description = %q{Henshin is a static site generator, with a plugin system and more}
14
15
  s.email = %q{m@hawx.me}
15
- s.executables = ["files.rb", "henshin"]
16
+ s.executables = ["henshin"]
16
17
  s.extra_rdoc_files = [
17
18
  "LICENSE",
18
19
  "README.markdown"
@@ -23,14 +24,14 @@ Gem::Specification.new do |s|
23
24
  "README.markdown",
24
25
  "Rakefile",
25
26
  "VERSION",
26
- "bin/files.rb",
27
27
  "bin/henshin",
28
28
  "henshin.gemspec",
29
29
  "lib/henshin.rb",
30
30
  "lib/henshin/archive.rb",
31
- "lib/henshin/categories.rb",
31
+ "lib/henshin/exec/files.rb",
32
32
  "lib/henshin/ext.rb",
33
33
  "lib/henshin/gen.rb",
34
+ "lib/henshin/labels.rb",
34
35
  "lib/henshin/plugin.rb",
35
36
  "lib/henshin/plugins/highlight.rb",
36
37
  "lib/henshin/plugins/liquid.rb",
@@ -40,9 +41,8 @@ Gem::Specification.new do |s|
40
41
  "lib/henshin/post.rb",
41
42
  "lib/henshin/site.rb",
42
43
  "lib/henshin/static.rb",
43
- "lib/henshin/tags.rb",
44
44
  "test/helper.rb",
45
- "test/site/css/includes/reset.sass",
45
+ "test/site/css/_reset.sass",
46
46
  "test/site/css/print.css",
47
47
  "test/site/css/screen.sass",
48
48
  "test/site/includes/head.html",
@@ -64,48 +64,39 @@ Gem::Specification.new do |s|
64
64
  "test/site/posts/lorem-ipsum.markdown",
65
65
  "test/site/posts/same-date.markdown",
66
66
  "test/site/static.html",
67
- "test/test_archives.rb",
68
- "test/test_categories.rb",
69
- "test/test_gens.rb",
70
- "test/test_henshin.rb",
71
- "test/test_layouts.rb",
67
+ "test/suite.rb",
68
+ "test/test_gen.rb",
72
69
  "test/test_options.rb",
73
- "test/test_posts.rb",
70
+ "test/test_post.rb",
74
71
  "test/test_site.rb",
75
- "test/test_statics.rb",
76
- "test/test_tags.rb",
77
- "test/text_exts.rb"
72
+ "test/test_static.rb"
78
73
  ]
79
74
  s.homepage = %q{http://github.com/hawx/henshin}
80
75
  s.rdoc_options = ["--charset=UTF-8"]
81
76
  s.require_paths = ["lib"]
82
- s.rubygems_version = %q{1.3.6}
77
+ s.rubygems_version = %q{1.3.7}
83
78
  s.summary = %q{Henshin is a static site generator}
84
79
  s.test_files = [
85
80
  "test/helper.rb",
86
81
  "test/site/plugins/test.rb",
87
- "test/test_archives.rb",
88
- "test/test_categories.rb",
89
- "test/test_gens.rb",
90
- "test/test_henshin.rb",
91
- "test/test_layouts.rb",
82
+ "test/suite.rb",
83
+ "test/test_gen.rb",
92
84
  "test/test_options.rb",
93
- "test/test_posts.rb",
85
+ "test/test_post.rb",
94
86
  "test/test_site.rb",
95
- "test/test_statics.rb",
96
- "test/test_tags.rb",
97
- "test/text_exts.rb"
87
+ "test/test_static.rb"
98
88
  ]
99
89
 
100
90
  if s.respond_to? :specification_version then
101
91
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
102
92
  s.specification_version = 3
103
93
 
104
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
94
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
105
95
  s.add_runtime_dependency(%q<titlecase>, [">= 0.1.0"])
106
96
  s.add_runtime_dependency(%q<directory_watcher>, [">= 1.3.1"])
107
97
  s.add_runtime_dependency(%q<maruku>, [">= 0.6.0"])
108
98
  s.add_runtime_dependency(%q<liquid>, [">= 2.0.0"])
99
+ s.add_runtime_dependency(%q<parsey>, [">= 0.1.3"])
109
100
  s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
110
101
  s.add_development_dependency(%q<yard>, [">= 0"])
111
102
  else
@@ -113,6 +104,7 @@ Gem::Specification.new do |s|
113
104
  s.add_dependency(%q<directory_watcher>, [">= 1.3.1"])
114
105
  s.add_dependency(%q<maruku>, [">= 0.6.0"])
115
106
  s.add_dependency(%q<liquid>, [">= 2.0.0"])
107
+ s.add_dependency(%q<parsey>, [">= 0.1.3"])
116
108
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
117
109
  s.add_dependency(%q<yard>, [">= 0"])
118
110
  end
@@ -121,6 +113,7 @@ Gem::Specification.new do |s|
121
113
  s.add_dependency(%q<directory_watcher>, [">= 1.3.1"])
122
114
  s.add_dependency(%q<maruku>, [">= 0.6.0"])
123
115
  s.add_dependency(%q<liquid>, [">= 2.0.0"])
116
+ s.add_dependency(%q<parsey>, [">= 0.1.3"])
124
117
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
125
118
  s.add_dependency(%q<yard>, [">= 0"])
126
119
  end
@@ -1,20 +1,21 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
- # standard ruby
4
3
  require 'time'
5
4
  require 'yaml'
6
5
  require 'pp'
6
+ require 'pathname'
7
7
 
8
- # 3rd party
9
8
  require 'titlecase'
9
+ require 'parsey'
10
10
 
11
- # library
12
11
  require 'henshin/site'
12
+ require 'henshin/plugin'
13
13
 
14
14
  require 'henshin/gen'
15
15
  require 'henshin/post'
16
16
  require 'henshin/static'
17
17
 
18
+ require 'henshin/labels'
18
19
  require 'henshin/tags'
19
20
  require 'henshin/categories'
20
21
  require 'henshin/archive'
@@ -24,103 +25,25 @@ require 'henshin/ext'
24
25
  module Henshin
25
26
 
26
27
  # Default options for configuration
27
- Defaults = {:title => 'A site',
28
- :description => 'No description',
29
- :time_zone => 'GMT',
30
- :author => '',
31
- :layout => '',
32
- :file_name => '<{category}/>{title-with-dashes}.{extension}',
33
- :permalink => '/{year}/{month}/{date}/{title}.html',
34
- :plugins => ['maruku', 'liquid'],
35
- :root => '.',
36
- :target => '_site',
37
- :plugin_options => {},
38
- :exclude => [] }
28
+ Defaults = {'title' => 'A site',
29
+ 'file_name' => '<{category}/>{title-with-dashes}.{extension}',
30
+ 'permalink' => '/{year}/{month}/{date}/{title}.html',
31
+ 'plugins' => ['maruku', 'liquid'],
32
+ 'root' => './',
33
+ 'target' => '_site',
34
+ 'exclude' => []}.freeze
35
+
36
+ # Partial regexs for use in parsing file names
37
+ Partials = {'title' => '([a-zA-Z0-9_ -]+)',
38
+ 'title-with-dashes' => '([a-zA-Z0-9-]+)',
39
+ 'date' => '(\d{4}-\d{2}-\d{2})',
40
+ 'date-time' => '(\d{4}-\d{2}-\d{2} at \d{2}:\d{2})',
41
+ 'xml-date-time' => '(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?((\+|-)\d{2}:\d{2})?)',
42
+ 'category' => '([a-zA-Z0-9_ -]+)',
43
+ 'extension' => "([a-zA-Z0-9_-]+)"}.freeze
39
44
 
40
-
41
- # Creates the configuration hash by merging defaults, supplied options and options
42
- # read from the 'options.yaml' file. Then loads the plugins and sorts them
43
- #
44
- # @param [Hash] override to override other set options
45
- # @return [Hash] the merged configuration hash
46
- def self.configure( override={} )
47
- config_file = File.join((override[:root] || Defaults[:root]), '/options.yaml')
48
-
49
- begin
50
- config = YAML.load_file(config_file).to_options
51
- settings = Defaults.merge(config).merge(override)
52
- rescue => e
53
- $stderr.puts "\nCould not read configuration, falling back to defaults..."
54
- $stderr.puts "-> #{e.to_s}"
55
- settings = Defaults.merge(override)
56
- end
57
-
58
- settings[:exclude] << '/_site' << '/plugins'
59
- settings[:plugins] = Henshin.sort_plugins( Henshin.load_plugins(settings) )
60
- settings
61
- end
62
-
63
- # Organises the plugins into generators and layout parses,
64
- # then turns the generators into a hash with a key for each extension.
65
- #
66
- # @param [Array] plugins
67
- # @return [Hash]
68
- def self.sort_plugins(plugins)
69
- r = {:generators => {}, :layout_parsers => []}
70
- plugins.each do |plugin|
71
- if plugin.is_a? Generator
72
- plugin.extensions[:input].each do |ext|
73
- r[:generators][ext] = plugin
74
- end
75
- elsif plugin.is_a? LayoutParser
76
- r[:layout_parsers] << plugin
77
- end
78
- end
79
- r
80
- end
81
-
82
- # Loads the plugins, each plugin then calls Henshin.register!, and then we loop through
83
- # the options and pass the options for the plugin to it.
45
+ # Reads the current version from VERSION
84
46
  #
85
- # @param [Hash] settings of loaded so far
86
- # @return [Array] list of loaded plugin instances
87
- def self.load_plugins(opts)
88
- plugins = []
89
- opts[:plugins].each do |l|
90
- begin
91
- require 'henshin/plugins/' + l
92
- rescue LoadError
93
- require File.join(opts[:root], 'plugins/', l)
94
- end
95
- end
96
-
97
- @registered_plugins.each do |plugin|
98
- if plugin[:opts] && opts[plugin[:opts]]
99
- opts[plugin[:opts]].each do |k, v|
100
- if k.to_s.include? 'dir'
101
- opts[plugin[:opts]][k] = File.join(opts[:root], v)
102
- end
103
- end
104
- plugin[:plugin].configure opts[plugin[:opts]]
105
- end
106
- plugins << plugin[:plugin]
107
- end
108
-
109
- plugins
110
- end
111
-
112
- # Each plugin will call this method when loaded from #load_plugins, these plugins then
113
- # populate @registered_plugins, which is returned from #load_plugins.
114
- #
115
- # @param [Class] plugin to load
116
- # @param [Symbol] options symbol to look up in settings hash
117
- # @return [Array] plugins and options symbol in hashes
118
- def self.register!( plug, opts=nil )
119
- @registered_plugins ||= []
120
- @registered_plugins << {:plugin => plug.new, :opts => opts}
121
- end
122
-
123
-
124
47
  # @return [String] current version
125
48
  def self.version
126
49
  File.read( File.join(File.dirname(__FILE__), '..', 'VERSION') )
@@ -1,137 +1,100 @@
1
1
  module Henshin
2
2
 
3
- class Archive
3
+ # This should really be a hash which just holds post objects, when #to_hash is
4
+ # called, each post object held is turned has Post#to_hash called and this is
5
+ # returned. It also controls writing of the archive pages.
6
+ #
7
+ class Archive < Hash
4
8
 
5
- attr_accessor :config
9
+ attr_accessor :site
6
10
 
7
- def initialize( site )
8
- @archive = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }}
11
+ # Create a new instance of Archive
12
+ def initialize(site)
9
13
  @site = site
10
- @config = site.config
11
- end
12
-
13
- def add_post( post )
14
- date = post.date
15
- @archive[date.year.to_s][date.month.to_s][date.day.to_s] << post.to_hash
16
- end
17
-
18
- # Turns the whole archive into a hash. Probably the least efficient thing in the world, but it works
19
- def to_hash
20
- if @hashed
21
- @hashed
22
- else
23
- @hashed = Hash.new do |h, k|
24
- h[k] = Hash.new do |h, k| # years
25
- if k == 'posts'
26
- h[k] = []
27
- else
28
- h[k] = Hash.new do |h, k| # months
29
- if k == 'posts'
30
- h[k] = []
31
- else
32
- h[k] = Hash.new do |h, k| # days
33
- if k == 'posts'
34
- h[k] = []
35
- else
36
- h[k] = {}
37
- end
38
- end # /days
39
- end
40
- end # /months
41
- end
42
- end # /years
43
- end
44
- @archive.each do |y, month|
45
- month.each do |m, date|
46
- date.each do |d, p|
47
- @hashed[y]['posts'] << p
48
- @hashed[y][m]['posts'] << p
49
- @hashed[y][m][d]['posts'] << p
50
-
51
- @hashed[y]['posts'].flatten!
52
- @hashed[y][m]['posts'].flatten!
53
- @hashed[y][m][d]['posts'].flatten!
54
- end
55
- end
56
- end
57
-
58
- end
59
- end
60
-
61
- # Creates a hash with posts separated by year, month then date
62
- def to_date_hash
63
- r = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }}
64
- @archive.each do |year, m|
65
- m.each do |month, d|
66
- d.each do |date, p|
67
- r[year][month][date] << p
68
- r[year][month][date].flatten!
69
- end
70
- end
71
- end
72
- r
73
14
  end
74
15
 
75
- # Creates a hash with posts separated by year then month
76
- def to_month_hash
77
- r = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }
78
- @archive.each do |year, m|
79
- m.each do |month, d|
80
- d.each do |date, p|
81
- r[year][month] << p
82
- r[year][month].flatten!
83
- end
84
- end
85
- end
86
- r
16
+ # Adds the post to the correct year array, month array and day array within the archive
17
+ #
18
+ # @param [Post] post to be added to the archive
19
+ def <<(post)
20
+ return nil unless post.data['date']
21
+ date = post.data['date']
22
+ year, month, day = date.year, date.month, date.day
23
+
24
+ self[year] ||= {}
25
+ self[year][month] ||= {}
26
+ self[year][month][day] ||= []
27
+ self[year][month][day] << post
87
28
  end
88
29
 
89
- # Creates a hash with posts separated by year
90
- def to_year_hash
91
- r = Hash.new {|h, k| h[k] = []}
92
- @archive.each do |year, m|
93
- m.each do |month, d|
94
- d.each do |date, p|
95
- r[year] << p
96
- r[year].flatten!
97
- end
30
+ # Converts the archive to a hash suitable for putting in a layout by
31
+ # calling #to_hash for all of the posts it contains
32
+ #
33
+ # @return [Hash]
34
+ def to_hash
35
+ r = {}
36
+ self.each do |y, i|
37
+ r[y] = {}
38
+ i.each do |m, i|
39
+ r[y][m] = {}
40
+ i.each do |d, i|
41
+ r[y][m][d] = []
42
+ i.each do |j|
43
+ r[y][m][d] << j.to_hash
44
+ end
45
+ end
98
46
  end
99
47
  end
48
+
100
49
  r
101
50
  end
102
51
 
103
- ##
104
- # Writes the archive pages
52
+ # Writes the archives if the correct layouts are present
105
53
  def write
106
54
  self.write_years if @site.layouts['archive_year']
107
55
  self.write_months if @site.layouts['archive_month']
108
56
  self.write_dates if @site.layouts['archive_date']
109
57
  end
110
58
 
59
+ # This writes all the archives for years
111
60
  def write_years
112
- years = self.to_year_hash
113
- years.each do |year, posts|
114
- write_path = File.join( @config[:root], year, 'index.html' )
115
- # date should give the full date, as a Time object!! and the others
116
- t = Time.parse("#{year}/01/01")
117
- payload = {:name => 'archive', :payload => {'date' => t, 'posts' => years[year]} }
118
- page = Gen.new( write_path, @site, payload )
119
- page.layout = @site.layouts['archive_year']
61
+ self.to_hash.each do |year, v|
62
+ # need to fake the file loc so that gen automatically creates permalink
63
+ t = @site.root + year.to_s + 'index.html'
64
+
65
+ payload = {
66
+ :name => 'archive',
67
+ :payload => {
68
+ 'posts' => self.to_hash,
69
+ 'year' => year
70
+ }
71
+ }
72
+ page = Gen.new(t, @site, payload)
73
+ page.read
74
+ page.data['layout'] = @site.layouts['archive_year']
120
75
 
121
76
  page.render
122
77
  page.write
123
78
  end
124
79
  end
125
80
 
81
+ # Writes all of the archives for the months
126
82
  def write_months
127
- months = self.to_month_hash
128
- months.each do |year, posts|
129
- posts.each do |month, posts|
130
- write_path = File.join( @config[:root], year, month, 'index.html' )
131
- t = Time.parse("#{year}/#{month}/01")
132
- payload = {:name => 'archive', :payload => {'date' => t, 'posts' => months[year][month]} }
133
- page = Gen.new( write_path, @site, payload )
134
- page.layout = @site.layouts['archive_month']
83
+ self.to_hash.each do |year, v|
84
+ v.each do |month, v|
85
+ t = @site.root + year.to_s + month.to_s + 'index.html'
86
+
87
+ payload = {
88
+ :name => 'archive',
89
+ :payload => {
90
+ 'posts' => self.to_hash,
91
+ 'year' => year,
92
+ 'month' => month
93
+ }
94
+ }
95
+ page = Gen.new(t, @site, payload)
96
+ page.read
97
+ page.data['layout'] = @site.layouts['archive_month']
135
98
 
136
99
  page.render
137
100
  page.write
@@ -139,16 +102,25 @@ module Henshin
139
102
  end
140
103
  end
141
104
 
105
+ # Writes all of the archives for the days
142
106
  def write_dates
143
- dates = self.to_date_hash
144
- dates.each do |year, posts|
145
- posts.each do |month, posts|
146
- posts.each do |date, posts|
147
- write_path = File.join( @config[:root], year, month, date, 'index.html' )
148
- t = Time.parse("#{year}/#{month}/#{date}")
149
- payload = {:name => 'archive', :payload => {'date' => t, 'posts' => dates[year][month][date]} }
150
- page = Gen.new( write_path, @site, payload )
151
- page.layout = @site.layouts['archive_date']
107
+ self.to_hash.each do |year, v|
108
+ v.each do |month, v|
109
+ v.each do |date, v|
110
+ t = @site.root + year.to_s + month.to_s + date.to_s + 'index.html'
111
+
112
+ payload = {
113
+ :name => 'archive',
114
+ :payload => {
115
+ 'posts' => self.to_hash,
116
+ 'year' => year,
117
+ 'month' => month,
118
+ 'day' => date
119
+ }
120
+ }
121
+ page = Gen.new(t, @site, payload)
122
+ page.read
123
+ page.data['layout'] = @site.layouts['archive_date']
152
124
 
153
125
  page.render
154
126
  page.write