diary 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/diary +5 -5
- data/diary.gemspec +3 -2
- data/lib/diary.rb +1 -0
- data/lib/diary/draft.rb +11 -24
- data/lib/diary/item.rb +71 -90
- data/lib/diary/message.rb +7 -3
- data/lib/diary/output.rb +57 -0
- data/lib/diary/page.rb +5 -12
- data/lib/diary/post.rb +19 -22
- data/lib/diary/site.rb +6 -6
- data/lib/diary/template.rb +14 -15
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/bin/diary
CHANGED
@@ -21,20 +21,20 @@ when 1
|
|
21
21
|
when 2
|
22
22
|
case ARGV[0]
|
23
23
|
when "draft"
|
24
|
-
|
24
|
+
Draft.new(ARGV[1])
|
25
25
|
when "page"
|
26
|
-
|
26
|
+
Page.new(ARGV[1])
|
27
27
|
when "post"
|
28
|
-
|
28
|
+
Post.new(ARGV[1])
|
29
29
|
when "publish"
|
30
|
-
|
30
|
+
Draft.new(ARGV[1]).publish
|
31
31
|
else
|
32
32
|
puts "Invalid option. Run `diary --help`."
|
33
33
|
end
|
34
34
|
when 3
|
35
35
|
case ARGV[0]
|
36
36
|
when "publish"
|
37
|
-
|
37
|
+
Draft.new(ARGV[1]).publish(ARGV[2])
|
38
38
|
else
|
39
39
|
puts "Invalid option. Run `diary --help`."
|
40
40
|
end
|
data/diary.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{diary}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Robin Clart"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-07-02}
|
13
13
|
s.default_executable = %q{diary}
|
14
14
|
s.email = %q{robin@clart.me}
|
15
15
|
s.executables = ["diary"]
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/diary/draft.rb",
|
31
31
|
"lib/diary/item.rb",
|
32
32
|
"lib/diary/message.rb",
|
33
|
+
"lib/diary/output.rb",
|
33
34
|
"lib/diary/page.rb",
|
34
35
|
"lib/diary/post.rb",
|
35
36
|
"lib/diary/site.rb",
|
data/lib/diary.rb
CHANGED
data/lib/diary/draft.rb
CHANGED
@@ -1,27 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
class Draft < Diary::Item
|
2
|
+
@@base_directory = 'drafts'
|
3
|
+
|
4
|
+
def publish(date = Date.today)
|
5
|
+
to = File.join(Post.class_variable_get(:@@base_directory), *date.to_s.split('-'))
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.publish(title, date = Date.today)
|
10
|
-
from = [base_directory, "#{title}.md"].join('/')
|
11
|
-
to = [Post.base_directory, date.to_s.split('-').join('/')].join('/')
|
12
|
-
|
13
|
-
FileUtils.mkdir_p to
|
14
|
-
FileUtils.mv from, to
|
7
|
+
FileUtils.mkpath to
|
8
|
+
FileUtils.mv path, to
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def self.base_directory
|
24
|
-
'drafts'
|
25
|
-
end
|
10
|
+
say Publish, File.join(to, "#{slug}.md")
|
11
|
+
rescue Errno::ENOENT
|
12
|
+
say Error, "No draft found"
|
26
13
|
end
|
27
|
-
end
|
14
|
+
end
|
data/lib/diary/item.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Diary
|
4
4
|
class Item
|
5
5
|
include Message
|
6
|
-
|
6
|
+
|
7
7
|
ACCENTS = {
|
8
8
|
['á','à','â','ä','ã'] => 'a',
|
9
9
|
['Ã','Ä','Â','À','�?'] => 'A',
|
@@ -19,113 +19,73 @@ module Diary
|
|
19
19
|
['ñ'] => 'n', ['Ñ'] => 'N'
|
20
20
|
}
|
21
21
|
|
22
|
-
attr_reader :
|
23
|
-
attr_reader :dir_path
|
24
|
-
|
25
|
-
def initialize(title, directory)
|
26
|
-
@file_name = title
|
27
|
-
@dir_path = directory.eql?("") ? nil : directory
|
28
|
-
@file_path = file_path
|
29
|
-
|
30
|
-
self.class.create(file_name, file_path, dir_path) unless file_exists?
|
31
|
-
end
|
32
|
-
|
33
|
-
def output(force = false)
|
34
|
-
if changed? or force
|
35
|
-
FileUtils.mkdir_p ["output", dir_path].compact.join('/')
|
36
|
-
output_file = File.new(output_file_path, 'w+')
|
37
|
-
output_file.puts render
|
38
|
-
output_file.close
|
22
|
+
attr_reader :file
|
39
23
|
|
40
|
-
|
24
|
+
def initialize(title_or_file, attributes = {})
|
25
|
+
case title_or_file
|
26
|
+
when File
|
27
|
+
@new_file = false
|
28
|
+
@file = title_or_file
|
41
29
|
else
|
42
|
-
|
30
|
+
@new_file = true
|
31
|
+
@attributes = attributes
|
32
|
+
@directory = extract_directory!
|
33
|
+
@title = title_or_file
|
34
|
+
@slug = nice_slug
|
35
|
+
|
36
|
+
create!
|
43
37
|
end
|
44
38
|
end
|
45
39
|
|
46
|
-
def template
|
47
|
-
Template.lookup(self)
|
48
|
-
end
|
49
|
-
|
50
|
-
def file
|
51
|
-
File.new(file_path)
|
52
|
-
end
|
53
|
-
|
54
|
-
def content
|
55
|
-
file.read.split(/---\n/).slice(-1)
|
56
|
-
end
|
57
|
-
|
58
|
-
def data
|
59
|
-
ydoc.is_a?(Hash) ? OpenStruct.new(ydoc) : OpenStruct.new
|
60
|
-
end
|
61
|
-
|
62
|
-
def ydoc
|
63
|
-
YAML.parse_file(file_path).transform if YAML.parse_file(file_path)
|
64
|
-
end
|
65
|
-
|
66
40
|
def slug
|
67
|
-
|
41
|
+
@slug || basename
|
68
42
|
end
|
69
43
|
|
70
|
-
def
|
71
|
-
@
|
44
|
+
def title
|
45
|
+
@title || data.title
|
72
46
|
end
|
73
|
-
|
74
|
-
def
|
75
|
-
|
47
|
+
|
48
|
+
def path
|
49
|
+
file ? file.path : File.join(base_directory, directory, "#{slug}.md")
|
76
50
|
end
|
77
51
|
|
78
|
-
def
|
79
|
-
|
52
|
+
def directory
|
53
|
+
@directory || File.dirname(path).gsub("#{base_directory}/", "")
|
80
54
|
end
|
81
55
|
|
82
|
-
def
|
83
|
-
|
84
|
-
|
85
|
-
|
56
|
+
def data
|
57
|
+
OpenStruct.new(YAML.load_file(path))
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.all
|
61
|
+
Dir[File.join(self.class_variable_get(:@@base_directory), "**", "*.md")].map do |p|
|
62
|
+
self.new File.new(p, 'r')
|
63
|
+
end
|
86
64
|
end
|
87
65
|
|
88
66
|
def self.first
|
89
67
|
all.first
|
90
68
|
end
|
91
|
-
|
69
|
+
|
92
70
|
def self.last
|
93
71
|
all.last
|
94
72
|
end
|
95
73
|
|
96
|
-
def self.all
|
97
|
-
Dir[File.join(base_directory, "**", "*.md")].map do |path|
|
98
|
-
self.new resolve_file_name(path), resolve_directory(path)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def self.compile(force = false)
|
103
|
-
all.each { |p| p.output(force) }
|
104
|
-
end
|
105
|
-
|
106
74
|
private
|
107
75
|
|
108
|
-
def
|
109
|
-
|
76
|
+
def base_directory
|
77
|
+
self.class.class_variable_get(:@@base_directory)
|
110
78
|
end
|
111
79
|
|
112
|
-
def
|
113
|
-
|
80
|
+
def basename
|
81
|
+
File.basename(file, '.md')
|
114
82
|
end
|
115
83
|
|
116
|
-
def liquidize
|
117
|
-
Liquid::Template.parse(template.read)
|
118
|
-
end
|
119
|
-
|
120
|
-
def markdownize
|
121
|
-
BlueCloth.new(content).to_html
|
122
|
-
end
|
123
|
-
|
124
84
|
def nice_slug
|
125
85
|
str = ""
|
126
86
|
ACCENTS.each do |ac,rep|
|
127
87
|
ac.each do |s|
|
128
|
-
str =
|
88
|
+
str = @title.gsub(s, rep)
|
129
89
|
end
|
130
90
|
end
|
131
91
|
str.gsub(/[^a-zA-Z0-9 ]/, "")
|
@@ -134,24 +94,45 @@ module Diary
|
|
134
94
|
.downcase
|
135
95
|
end
|
136
96
|
|
137
|
-
def
|
138
|
-
|
139
|
-
ary.pop
|
140
|
-
ary.shift
|
141
|
-
ary.join('/')
|
97
|
+
def extract_directory!
|
98
|
+
@attributes.delete(:directory) { |el| "" }
|
142
99
|
end
|
143
100
|
|
144
|
-
def
|
145
|
-
|
101
|
+
def create!
|
102
|
+
if new_file?
|
103
|
+
ensure_directories_exists
|
104
|
+
@file = File.new(path, "w+")
|
105
|
+
write!
|
106
|
+
|
107
|
+
say Create, path
|
108
|
+
else
|
109
|
+
@file = File.new(path)
|
110
|
+
@new_file = false
|
111
|
+
|
112
|
+
say Exist, path
|
113
|
+
end
|
114
|
+
|
115
|
+
remove_instance_variable(:@attributes)
|
116
|
+
remove_instance_variable(:@title)
|
117
|
+
remove_instance_variable(:@slug)
|
146
118
|
end
|
147
119
|
|
148
|
-
def
|
149
|
-
|
150
|
-
|
151
|
-
|
120
|
+
def new_file?
|
121
|
+
@new_file and not File.exists?(path)
|
122
|
+
end
|
123
|
+
|
124
|
+
def write!
|
125
|
+
file.puts "---\n"
|
126
|
+
file.puts "title: #{@title}"
|
127
|
+
@attributes.each do |key, value|
|
128
|
+
file.puts "#{key}: #{value}\n"
|
129
|
+
end
|
130
|
+
file.puts "\n---\n"
|
152
131
|
file.close
|
153
|
-
|
154
|
-
|
132
|
+
end
|
133
|
+
|
134
|
+
def ensure_directories_exists
|
135
|
+
FileUtils.mkpath(File.dirname(path))
|
155
136
|
end
|
156
137
|
end
|
157
|
-
end
|
138
|
+
end
|
data/lib/diary/message.rb
CHANGED
@@ -5,9 +5,9 @@ module Diary
|
|
5
5
|
Skip = " \e[1;31mskip\e[0m"
|
6
6
|
|
7
7
|
# Green
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
Publish = " \e[1;32mpublish\e[0m"
|
9
|
+
Create = " \e[1;32mcreate\e[0m"
|
10
|
+
Update = " \e[1;32mupdate\e[0m"
|
11
11
|
|
12
12
|
# Yellow
|
13
13
|
Identical = " \e[1;33midentical\e[0m"
|
@@ -15,5 +15,9 @@ module Diary
|
|
15
15
|
|
16
16
|
# Cyan
|
17
17
|
Invoke = " \e[1;36minvoke\e[0m"
|
18
|
+
|
19
|
+
def say(const, message)
|
20
|
+
puts "#{const} #{message}"
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
data/lib/diary/output.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module Diary
|
2
|
+
module Output
|
3
|
+
include Message
|
4
|
+
|
5
|
+
@@output_base_directory = 'output'
|
6
|
+
|
7
|
+
def output(force = false)
|
8
|
+
if changed? or force
|
9
|
+
FileUtils.mkpath output_directory
|
10
|
+
f = File.new(output_path, 'w+')
|
11
|
+
f.puts render
|
12
|
+
f.close
|
13
|
+
|
14
|
+
say Update, output_path
|
15
|
+
else
|
16
|
+
say Identical, output_path
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def content
|
21
|
+
file.rewind
|
22
|
+
file.read.split(/---\n/).slice(-1).strip
|
23
|
+
end
|
24
|
+
|
25
|
+
def render
|
26
|
+
Liquid::Template.parse(template.read).render('yield' => html)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def changed?
|
32
|
+
file.mtime > output_file.ctime
|
33
|
+
rescue Errno::ENOENT
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def html
|
38
|
+
BlueCloth.new(content).to_html
|
39
|
+
end
|
40
|
+
|
41
|
+
def output_path
|
42
|
+
path.gsub(base_directory, output_base_directory).gsub('.md', '.html')
|
43
|
+
end
|
44
|
+
|
45
|
+
def output_directory
|
46
|
+
File.dirname(output_path)
|
47
|
+
end
|
48
|
+
|
49
|
+
def output_file
|
50
|
+
File.new(output_path)
|
51
|
+
end
|
52
|
+
|
53
|
+
def output_base_directory
|
54
|
+
self.class.class_variable_get(:@@output_base_directory)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/diary/page.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
private
|
8
|
-
|
9
|
-
def self.base_directory
|
10
|
-
'pages'
|
11
|
-
end
|
12
|
-
end
|
1
|
+
class Page < Diary::Item
|
2
|
+
@@base_directory = 'pages'
|
3
|
+
|
4
|
+
include Diary::Template
|
5
|
+
include Diary::Output
|
13
6
|
end
|
data/lib/diary/post.rb
CHANGED
@@ -1,25 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def self.base_directory
|
22
|
-
'posts'
|
1
|
+
class Post < Diary::Item
|
2
|
+
@@base_directory = 'posts'
|
3
|
+
|
4
|
+
include Diary::Template
|
5
|
+
include Diary::Output
|
6
|
+
|
7
|
+
def initialize(title_or_file)
|
8
|
+
super(title_or_file)
|
9
|
+
end
|
10
|
+
|
11
|
+
def date
|
12
|
+
Date.parse(directory)
|
13
|
+
end
|
14
|
+
|
15
|
+
def output(force = false)
|
16
|
+
if date <= Date.today
|
17
|
+
super(force)
|
18
|
+
else
|
19
|
+
say Skip, path
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
data/lib/diary/site.rb
CHANGED
@@ -16,8 +16,8 @@ module Diary
|
|
16
16
|
|
17
17
|
def self.compile(force = false)
|
18
18
|
unless (Post.all.size == 0) and (Page.all.size == 0)
|
19
|
-
Post.
|
20
|
-
Page.
|
19
|
+
Post.all.each { |p| p.output(force) }
|
20
|
+
Page.all.each { |p| p.output(force) }
|
21
21
|
return self
|
22
22
|
else
|
23
23
|
puts "#{Error} Nothing to compile"
|
@@ -34,9 +34,9 @@ module Diary
|
|
34
34
|
unless File.exists?(path)
|
35
35
|
FileUtils.mkdir_p(path)
|
36
36
|
|
37
|
-
|
37
|
+
say Create, path
|
38
38
|
else
|
39
|
-
|
39
|
+
say Exist, path
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -46,9 +46,9 @@ module Diary
|
|
46
46
|
f.puts(content)
|
47
47
|
f.close
|
48
48
|
|
49
|
-
|
49
|
+
say Create, path
|
50
50
|
else
|
51
|
-
|
51
|
+
say Exist, path
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
data/lib/diary/template.rb
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
module Diary
|
2
|
-
|
2
|
+
module Template
|
3
3
|
include Message
|
4
|
-
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
return file("index")
|
4
|
+
|
5
|
+
def template
|
6
|
+
File.new(template_lookup).tap do |file|
|
7
|
+
say Invoke, file.path
|
8
|
+
end
|
10
9
|
end
|
11
10
|
|
12
11
|
private
|
13
12
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
13
|
+
def template_lookup
|
14
|
+
template_names.each do |name|
|
15
|
+
return template_path(name) if File.exists?(template_path(name))
|
17
16
|
end
|
18
17
|
end
|
19
|
-
|
20
|
-
def self.exists?(name)
|
21
|
-
File.exists?(resolve(name))
|
22
|
-
end
|
23
18
|
|
24
|
-
def
|
19
|
+
def template_path(name)
|
25
20
|
File.join("templates", "#{name}.html")
|
26
21
|
end
|
22
|
+
|
23
|
+
def template_names
|
24
|
+
[data.template, slug, self.class.name.downcase, 'index'].compact
|
25
|
+
end
|
27
26
|
end
|
28
27
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Robin Clart
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-07-02 00:00:00 +02:00
|
18
18
|
default_executable: diary
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/diary/draft.rb
|
68
68
|
- lib/diary/item.rb
|
69
69
|
- lib/diary/message.rb
|
70
|
+
- lib/diary/output.rb
|
70
71
|
- lib/diary/page.rb
|
71
72
|
- lib/diary/post.rb
|
72
73
|
- lib/diary/site.rb
|