forked from colemanm/colemanm.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.rb
executable file
·126 lines (112 loc) · 3.1 KB
/
blog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env ruby
#frozen_string_literal: true
require 'rubygems'
require 'thor'
require 'json'
require 'yaml'
require 'rake'
require 'haml'
require 'fileutils'
require 'active_support/all'
require 'kramdown'
class Blog < Thor
desc 'post', 'Create a new blog post.'
method_option :title, aliases: '-t', desc: 'Blog post title', default: 'New blog post'
method_option :date, aliases: '-d', desc: 'Publish date'
def post
title = options[:title]
slug = title.downcase.strip.tr(' ', '-').gsub(/[^\w-]/, '')
date = generate_date(options[:date])
datetime = generate_datetime(options[:date])
file = File.join('./_posts/', "#{date}-#{slug}.md")
open(file, 'w') do |post|
post.puts <<~BLOGPOST
---
layout: post
date: #{datetime}
title: "#{title.tr('-', ' ')}"
description: ""
categories: blog
tags:
-
---
Content for blog post.
BLOGPOST
end
puts "Post '#{date}-#{slug}.md' created."
end
desc 'link', 'Create a new link post.'
method_option :title, aliases: '-t', desc: 'Blog post title', default: 'New blog post'
method_option :date, aliases: '-d', desc: 'Publish date'
def link
title = options[:title]
slug = title.downcase.strip.tr(' ', '-').gsub(/[^\w-]/, '')
date = generate_date(options[:date])
datetime = generate_datetime(options[:date])
file = File.join('./_posts/', "#{date}-#{slug}.md")
open(file, 'w') do |link|
link.puts <<~LINKPOST
---
layout: link
date: #{datetime}
title: "#{title.tr('-', ' ')}"
target: url
description: ""
categories: blog
tags:
-
links:
- url:
title:
---
Content for link post.
LINKPOST
end
puts "Link '#{date}-#{slug}.md' created."
end
desc 'book', 'Create a new book in library.'
method_option :title, aliases: '-t', desc: 'Book title', default: 'New book'
method_option :author, aliases: '-a', desc: 'Book author last name'
def book
title = options[:title]
author_last = options[:author]
slug = author_last.downcase.strip.tr(' ', '-').gsub(/[^\w-]/, '') + "-" + title.downcase.strip.tr(' ', '-').gsub(/[^\w-]/, '')
file = File.join('./_books/', "#{slug}.md")
open(file, 'w') do |link|
link.puts <<~BOOK
---
layout: book
title: "#{title}"
subtitle: ""
author:
author_last: #{author_last}
slug: #{slug}
type: nonfiction
img:
series:
part:
genres:
-
isbn:
rating:
pages:
format:
publish_year:
date_started: 2019-
date_completed: 2019-
goodreads_id:
---
BOOK
end
puts "Book '#{slug}.md' created."
end
no_tasks do
def generate_date(postdate)
(postdate ? Time.parse(postdate) : Time.now).strftime('%F')
end
def generate_datetime(postdate)
(postdate ? Time.parse(postdate) : Time.now).strftime('%F %R %H:%M:%S')
end
end
end
Blog.start