blog-generator 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8dc5db77c4a2a7f608f424d16709202a2b74c6c
4
- data.tar.gz: 78333987b262250f8d2e8a35cfaa97f4b85129a9
3
+ metadata.gz: c4223a5c99949293121434806a6e208892610b2e
4
+ data.tar.gz: 11ddc6a101e1d2da88cc950fc5b22addbeed5662
5
5
  SHA512:
6
- metadata.gz: bfde06a5ab223af902144ed64d8f050218c9213db6f549a584d03d91b56af1fcac2c4e71604901d04351e5589d058a88bab2c71427f9a561374d9997c6ab5bb3
7
- data.tar.gz: 871ccc05a09e354864c1312c3f288bddf943da6371ec5da5894a2c59e53ced02e716ff6db6641edcc803e6d170f063cffbc6d891c4f55da72d4deb065ea80d13
6
+ metadata.gz: 9c96fe40d449a78473f476f3810f84908c27f7258428c1fd5df7891427be9f54e92674fefd745703c1e33bed69763ea61cf7e784c6a2bb2672e170bbfccffe59
7
+ data.tar.gz: 69ae7f14ae1bad2d91c380243f1c5fd4e475c165907989e82eb7b94604b98257c64043cf0e0030a0474091d75dcf1c1f95f58ac5ffdbb5ef9a9f3abde162d7fc
data/README.md CHANGED
@@ -37,11 +37,11 @@ tags: ['Hello world', 'Test']
37
37
  - Metadata `draft: true` will exclude the post.
38
38
  - Any other metadata can be added and will be accessible in the resulting JSON.
39
39
  - Body has `#excerpt`.
40
- - Format `posts/:published_on-:slug.:format`.
40
+ - Format `posts/:published_at-:slug.:format`.
41
41
 
42
42
  # Routes generated
43
43
 
44
- - `slug` and `published_on` added to metadata.
44
+ - `slug` and `published_at` added to metadata.
45
45
  - `/metadata.json`
46
46
  - `/posts.json`
47
47
  - `/posts/:slug.json`
@@ -8,7 +8,7 @@ require 'ostruct'
8
8
  require 'fileutils'
9
9
  require 'blog-generator'
10
10
 
11
- POSTS_DIR, OUTPUT_BASE_PATH = ARGV
11
+ POSTS_DIR, OUTPUT_BASE_PATH = ARGV.map { |i| i.chomp('/') }
12
12
 
13
13
  unless ARGV.length == 2
14
14
  abort "Usage: #{$0} [posts dir] [output base path]"
@@ -18,9 +18,21 @@ unless File.directory?(POSTS_DIR)
18
18
  abort "Posts directory #{POSTS_DIR} doesn't exist."
19
19
  end
20
20
 
21
+ if Dir.exists?("#{OUTPUT_BASE_PATH}_prev") # If it does, the last build crashed.
22
+ puts "~ Last build crashed. Reusing #{OUTPUT_BASE_PATH}_prev."
23
+ elsif (! Dir.exists?("#{OUTPUT_BASE_PATH}_prev")) && Dir.exists?(OUTPUT_BASE_PATH) # otherwise it's first run.
24
+ FileUtils.mv(OUTPUT_BASE_PATH, "#{OUTPUT_BASE_PATH}_prev") # So we don't get any artifacts.
25
+ end
21
26
 
22
- FileUtils.rm_r(OUTPUT_BASE_PATH) if File.directory?(OUTPUT_BASE_PATH)
23
- Dir.mkdir(OUTPUT_BASE_PATH)
27
+ unless Dir.exists?(OUTPUT_BASE_PATH)
28
+ puts "~ #{OUTPUT_BASE_PATH} doesn't exist, creating."
29
+ Dir.mkdir(OUTPUT_BASE_PATH)
30
+ end
31
+
32
+ OLD_POSTS = Dir.glob("#{OUTPUT_BASE_PATH}_prev/posts/*.json").reduce(Hash.new) do |posts, path|
33
+ post = JSON.parse(File.read(path))
34
+ posts.merge(post['slug'] => post)
35
+ end
24
36
 
25
37
  path = File.expand_path(File.join(POSTS_DIR, '..', 'defaults.yml'))
26
38
  unless File.exist?(path)
@@ -30,7 +42,7 @@ end
30
42
  # Parse the posts.
31
43
  site = OpenStruct.new(File.exist?(path) ? YAML.load_file(path) : Hash.new)
32
44
  site.feed = [site.base_url, 'posts.atom'].join('/')
33
- generator = BlogGenerator::Generator.parse(site, POSTS_DIR)
45
+ generator = BlogGenerator::Generator.parse(site, POSTS_DIR, OLD_POSTS)
34
46
 
35
47
  # Generate.
36
48
 
@@ -76,3 +88,5 @@ Dir.chdir(OUTPUT_BASE_PATH) do
76
88
  file "tags/#{tag[:slug]}.atom", feed.render
77
89
  end
78
90
  end
91
+
92
+ FileUtils.rm_rf("#{OUTPUT_BASE_PATH}_prev") # Has to be forced, otherwise fails on the first run.
@@ -7,7 +7,7 @@ require 'blog-generator/feed'
7
7
 
8
8
  module BlogGenerator
9
9
  class Generator
10
- def self.parse(site, posts_dir)
10
+ def self.parse(site, posts_dir, old_posts)
11
11
  posts = Dir.glob("#{posts_dir}/*.{html,md}").reduce(Array.new) do |posts, path|
12
12
  posts.push(Post.new(site, path))
13
13
  end
@@ -15,7 +15,20 @@ module BlogGenerator
15
15
  published_posts = posts.select { |post| ! post.metadata[:draft] }
16
16
 
17
17
  published_posts.sort! do |a, b|
18
- b.published_on <=> a.published_on
18
+ b.published_at <=> a.published_at
19
+ end
20
+
21
+ published_posts.each do |post|
22
+ if old_post = old_posts[post.slug]
23
+ post.update_post_with_previous_values(OpenStruct.new(old_post))
24
+ p [post.published_at, post.published_on]
25
+ if post.published_at && post.published_on.to_date != post.published_at.to_date
26
+ abort "~ Published_at doesn't match published_on from the date part of #{post.slug} filename: #{[post.published_on.to_date, post.published_at.to_date].inspect}"
27
+ end
28
+
29
+ else
30
+ puts "~ New post: #{post.slug}"
31
+ end
19
32
  end
20
33
 
21
34
  self.new(site, PostList.new(site, published_posts))
@@ -1,6 +1,7 @@
1
1
  require 'erb'
2
2
  require 'digest'
3
3
  require 'forwardable'
4
+ require 'date'
4
5
 
5
6
  module BlogGenerator
6
7
  class Feed
@@ -31,7 +32,7 @@ module BlogGenerator
31
32
  end
32
33
 
33
34
  def updated_at
34
- self.posts.last.updated_at
35
+ self.posts.last.updated_at || self.posts.last.published_at
35
36
  end
36
37
 
37
38
  def template
@@ -54,7 +55,7 @@ __END__
54
55
  <link href="<%= self.feed_url %>" rel="self" />
55
56
  <link href="<%= self.base_url %>" />
56
57
  <id><%= self.id %></id>
57
- <updated><%= self.updated_at.iso8601 %></updated>
58
+ <updated><%= self.updated_at.to_date.iso8601 %></updated>
58
59
 
59
60
  <% posts.each do |post| %>
60
61
  <entry>
@@ -62,7 +63,7 @@ __END__
62
63
  <link href="<%= post.absolute_url %>" />
63
64
  <link rel="alternate" type="text/html" href="<%= post.absolute_url %>"/>
64
65
  <id><%= post.id %></id>
65
- <updated><%= post.updated_at.iso8601 %></updated>
66
+ <updated><%= (post.updated_at || post.published_at).to_date.iso8601 %></updated>
66
67
  <!-- TODO: strip HTML from excerpt -->
67
68
  <summary><%= post.excerpt %></summary>
68
69
  <!-- <content type="xhtml">
@@ -7,7 +7,7 @@ module BlogGenerator
7
7
  class Post
8
8
  REGEXP = /^(\d{4}-\d{2}-\d{2})-(.+)\.(html|md)$/
9
9
 
10
- attr_reader :site, :metadata, :format
10
+ attr_reader :site, :metadata, :format, :published_on, :updated_at
11
11
  def initialize(site, path)
12
12
  # TODO: metadata so we can construct url (base_url + relative) AND merge author
13
13
  @site, @path = site, File.expand_path(path)
@@ -16,13 +16,14 @@ module BlogGenerator
16
16
  buffer.merge(slug.to_sym => value)
17
17
  end
18
18
 
19
- published_on, slug, format = parse_path(path)
19
+ @published_on, slug, format = parse_path(path)
20
+
20
21
  @format = format # So we can access it from excerpt without having to pass it as an argument and break everything.
21
22
 
22
23
  @body = convert_markdown(self.body) if format == :md
23
24
  self.body # cache if it wasn't called yet
24
25
 
25
- @metadata.merge!(slug: slug, published_on: published_on)
26
+ @metadata.merge!(slug: slug, published_at: self.published_at)
26
27
  @metadata.merge!(excerpt: excerpt)
27
28
  @metadata.merge!(path: "/posts/#{slug}") ### TODO: some routing config.
28
29
 
@@ -43,8 +44,27 @@ module BlogGenerator
43
44
  @body = document.css('body').inner_html.strip
44
45
  end
45
46
 
46
- def updated_at
47
- File.mtime(@path).to_datetime
47
+ # slug cannot be updated
48
+ # => It has to be in Git now.
49
+ def update_post_with_previous_values(old_post)
50
+ @published_at = DateTime.parse(old_post.published_at).to_time.utc
51
+ @updated_at = DateTime.parse(old_post.updated_at).to_time.utc if old_post.updated_at
52
+ @metadata.merge!(published_at: self.published_at)
53
+ @metadata.merge!(updated_at: self.updated_at) if @updated_at
54
+ if old_post.body != self.body # TODO: some more intelligent analysis, if more than 10% changed.
55
+ puts "~ Post #{self.slug} has been updated."
56
+ self.update!
57
+ end
58
+ end
59
+
60
+ # TODO: get rid off the variable and proxy it to metadata[:published_at].
61
+ def published_at
62
+ # When it was actually generated. It is then sourced from the last generated file, so it doesn't keep updating.
63
+ @published_at ||= Time.now.utc
64
+ end
65
+
66
+ def update!
67
+ @updated_at = Time.now.utc
48
68
  end
49
69
 
50
70
  def author
@@ -104,7 +124,7 @@ module BlogGenerator
104
124
 
105
125
  def parse_path(path)
106
126
  match = File.basename(path).match(REGEXP)
107
- [Date.parse(match[1]), match[2], match[3].to_sym]
127
+ [Date.parse(match[1]).to_time.utc, match[2], match[3].to_sym]
108
128
  end
109
129
 
110
130
  def convert_markdown(markup)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blog-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - James C Russell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri