blog-generator 0.0.4 → 0.0.5

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: be671ad043de1d364e12bb8515a5f040a054687b
4
- data.tar.gz: 425ef4f0111617c8df5ab6778a9303ac67d9724f
3
+ metadata.gz: ed3c23b9ad0f82dde254a9d03d078a9531485b59
4
+ data.tar.gz: b826167d4a93e7e559c98a493680572fb85f76e8
5
5
  SHA512:
6
- metadata.gz: 9516667ffd5f5f35576f1322ce7ce79281395e9b9030595ca6990f96fa1725fe1fe14ae3293e8ea0e92a8b93b797d8589fa18d488df1986fe6261099f4ca25f8
7
- data.tar.gz: d03fb84a1276c3621f8c3c531b6d86985d23b48550323553706d29317c756d61394f0e1861ae22f0b502106112482e2f12b444cbd90c538536d1a32a8a2625a1
6
+ metadata.gz: b1f7b6d0caf385190f2d95bd8c680486cffff05a99f8e6cdf3de66f5b9e91466583908ff7a473cc0c39855158a81f3816cf32638f0d1a70dc53c12d07220fe53
7
+ data.tar.gz: 4298605c3a49269f119d3f3ccecea54e38e74a2970f429087f94628d3ae75ac0d35ddd660dcfa4bec4197a0fda9f8eb191a8a74a2e9affad8cef738f9f9ca5ff
@@ -29,57 +29,48 @@ end
29
29
 
30
30
  # Parse the posts.
31
31
  site = OpenStruct.new(File.exist?(path) ? YAML.load_file(path) : Hash.new)
32
+ site.feed = [site.base_url, 'posts.atom'].join('/')
32
33
  generator = BlogGenerator::Generator.parse(site, POSTS_DIR)
33
34
 
34
35
  # Generate.
35
36
 
36
- Dir.chdir(OUTPUT_BASE_PATH) do
37
- # GET /metadata.json
38
- File.open('metadata.json', 'w') do |file|
39
- # TODO: Refactor this, it's evil.
40
- file.puts(site.instance_variable_get(:@table).to_json)
37
+ def file(path, content)
38
+ puts "~ #{path}"
39
+ File.open(path, 'w') do |file|
40
+ file.puts(content)
41
41
  end
42
+ end
42
43
 
43
- # GET /api/posts.json
44
- File.open('posts.json', 'w') do |file|
45
- # This calls PostList#to_json
46
- file.puts(JSON.pretty_generate(generator.posts))
47
- end
44
+ Dir.chdir(OUTPUT_BASE_PATH) do
45
+ # GET /metadata.json
46
+ # TODO: Refactor this, it's evil.
47
+ file 'metadata.json', JSON.pretty_generate(site.instance_variable_get(:@table))
48
48
 
49
49
  # GET /posts.atom
50
- File.open('posts.atom', 'w') do |file|
51
- feed = BlogGenerator::Feed.new(site, generator.posts, 'posts.atom')
52
- file.puts(feed.render)
53
- end
50
+ feed = BlogGenerator::Feed.new(site, generator.posts, 'posts.atom')
51
+ file 'posts.atom', feed.render
52
+
53
+ # GET /api/posts.json
54
+ # This calls PostList#to_json
55
+ file 'posts.json', JSON.pretty_generate(generator.posts)
54
56
 
55
57
  # GET /api/posts/hello-world.json
56
58
  Dir.mkdir('posts') unless Dir.exist?('posts')
57
59
  generator.posts.each do |post|
58
- File.open("posts/#{post.slug}.json", 'w') do |file|
59
- file.puts(JSON.pretty_generate(post))
60
- end
60
+ file "posts/#{post.slug}.json", JSON.pretty_generate(post)
61
61
  end
62
62
 
63
63
  # GET /api/tags.json
64
- File.open('tags.json', 'w') do |file|
65
- # [{title: x, slug: y}]
66
- tags = generator.tags.map do |tag, _|
67
- tag.merge(path: "/tags/#{tag[:slug]}") ### TODO: some routing config.
68
- end
69
- file.puts(JSON.pretty_generate(tags))
70
- end
64
+ file 'tags.json', JSON.pretty_generate(generator.tags.keys)
71
65
 
72
66
  Dir.mkdir('tags') unless Dir.exist?('tags')
73
67
  generator.tags.each do |tag, posts|
74
68
  # GET /api/tags/doxxu.json
75
- File.open("tags/#{tag[:slug]}.json", 'w') do |file|
76
- file.puts(JSON.pretty_generate(posts))
77
- end
69
+ body = {tag: tag, posts: posts}
70
+ file "tags/#{tag[:slug]}.json", JSON.pretty_generate(body)
78
71
 
79
72
  # GET /api/tags/doxxu.atom
80
- File.open("tags/#{tag[:slug]}.atom", 'w') do |file|
81
- feed = BlogGenerator::Feed.new(site, posts, "#{tag[:slug]}.atom")
82
- file.puts(feed.render)
83
- end
73
+ feed = BlogGenerator::Feed.new(site, posts, "#{tag[:slug]}.atom")
74
+ file "tags/#{tag[:slug]}.atom", feed.render
84
75
  end
85
76
  end
@@ -8,13 +8,15 @@ require 'blog-generator/feed'
8
8
  module BlogGenerator
9
9
  class Generator
10
10
  def self.parse(site, posts_dir)
11
- posts = Dir.glob("#{posts_dir}/*.{html,md}").reduce(PostList.new(site)) do |posts, path|
12
- post = Post.new(site, path)
13
- puts "~ Parsing #{post.inspect}"
14
- posts.push(post)
11
+ posts = Dir.glob("#{posts_dir}/*.{html,md}").reduce(Array.new) do |posts, path|
12
+ posts.push(Post.new(site, path))
15
13
  end
16
14
 
17
- self.new(site, posts)
15
+ posts = posts.sort do |a, b|
16
+ b.published_on <=> a.published_on
17
+ end
18
+
19
+ self.new(site, PostList.new(site, posts))
18
20
  end
19
21
 
20
22
  attr_reader :site, :posts
@@ -17,6 +17,14 @@ module BlogGenerator
17
17
  [@site.base_url, @relative_url].join('/')
18
18
  end
19
19
 
20
+ def as_json
21
+ {title: self.title, url: self.feed_url}
22
+ end
23
+
24
+ def to_json(*args)
25
+ self.as_json.to_json(*args)
26
+ end
27
+
20
28
  def id
21
29
  digest = Digest::MD5.hexdigest(posts.each.map(&:title).join(","))
22
30
  "urn:uuid:#{digest}"
@@ -7,16 +7,17 @@ module BlogGenerator
7
7
  class Post
8
8
  REGEXP = /^(\d{4}-\d{2}-\d{2})-(.+)\.(html|md)$/
9
9
 
10
- attr_reader :site, :metadata
10
+ attr_reader :site, :metadata, :format
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)
14
14
 
15
- @metadata = YAML.load_file(path).reduce(Hash.new) do |buffer, (key, value)|
16
- buffer.merge(key.to_sym => value)
15
+ @metadata = YAML.load_file(path).reduce(Hash.new) do |buffer, (slug, value)|
16
+ buffer.merge(slug.to_sym => value)
17
17
  end
18
18
 
19
19
  published_on, slug, format = parse_path(path)
20
+ @format = format # So we can access it from excerpt without having to pass it as an argument and break everything.
20
21
 
21
22
  @body = convert_markdown(self.body) if format == :md
22
23
  self.body # cache if it wasn't called yet
@@ -26,9 +27,17 @@ module BlogGenerator
26
27
  @metadata.merge!(path: "/posts/#{slug}") ### TODO: some routing config.
27
28
 
28
29
  @metadata[:tags].map! do |tag|
29
- {title: tag, slug: generate_slug(tag)}
30
+ slug = generate_slug(tag)
31
+ feed = "#{site.base_url}/#{slug}.atom"
32
+ {title: tag, slug: slug, feed: feed}
30
33
  end
31
34
 
35
+ tag_feeds = @metadata[:tags].map do |tag|
36
+ tag[:feed]
37
+ end
38
+
39
+ # @metadata.merge!(feeds: atom.feeds + tag_feeds) ### TODO: some routing config.
40
+
32
41
  document = Nokogiri::HTML(self.body)
33
42
  document.css('#excerpt').remove
34
43
  @body = document.css('body').inner_html.strip
@@ -68,9 +77,15 @@ module BlogGenerator
68
77
  @body ||= File.read(@path).match(/\n---\n(.+)$/m)[1].strip
69
78
  end
70
79
 
71
- # We're converting it to MD, apparently it's necessary even though we converted the whole text initially, but it seems like MD ignores whatever is in <div id="excerpt">...</div>.
72
80
  def excerpt
73
- @excerpt ||= Nokogiri::HTML(convert_markdown(Nokogiri::HTML(self.body).css('#excerpt').inner_html.strip)).css('p').inner_html
81
+ if self.format == :html
82
+ @excerpt ||= Nokogiri::HTML(Nokogiri::HTML(self.body).css('#excerpt').inner_html.strip).css('p').inner_html
83
+ elsif self.format == :md
84
+ # We're converting it to MD, apparently it's necessary even though we
85
+ # converted the whole text initially, but it seems like MD ignores whatever
86
+ # is in <div id="excerpt">...</div>.
87
+ @excerpt ||= Nokogiri::HTML(convert_markdown(Nokogiri::HTML(self.body).css('#excerpt').inner_html.strip)).css('p').inner_html
88
+ end
74
89
  end
75
90
 
76
91
  def as_json
@@ -6,11 +6,11 @@ module BlogGenerator
6
6
  extend Forwardable
7
7
 
8
8
  attr_reader :site, :posts
9
- def initialize(site)
10
- @site, @posts = site, Array.new
9
+ def initialize(site, posts = Array.new)
10
+ @site, @posts = site, posts
11
11
  end
12
12
 
13
- def_delegators :@posts, :reduce, :each, :first, :last, :[]
13
+ def_delegators :@posts, :reduce, :each, :sort, :first, :last, :[]
14
14
 
15
15
  def as_json
16
16
  self.posts.map do |post|
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.4
4
+ version: 0.0.5
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: 2015-06-19 00:00:00.000000000 Z
11
+ date: 2017-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri