diary 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
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
- Diary::Draft.new(ARGV[1])
24
+ Draft.new(ARGV[1])
25
25
  when "page"
26
- Diary::Page.new(ARGV[1])
26
+ Page.new(ARGV[1])
27
27
  when "post"
28
- Diary::Post.new(ARGV[1])
28
+ Post.new(ARGV[1])
29
29
  when "publish"
30
- Diary::Draft.publish(ARGV[1])
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
- Diary::Draft.publish(ARGV[1], ARGV[2])
37
+ Draft.new(ARGV[1]).publish(ARGV[2])
38
38
  else
39
39
  puts "Invalid option. Run `diary --help`."
40
40
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{diary}
8
- s.version = "0.1.4"
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-06-28}
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",
@@ -17,6 +17,7 @@ require 'bluecloth'
17
17
  require 'diary/message'
18
18
  require 'diary/site'
19
19
  require 'diary/template'
20
+ require 'diary/output'
20
21
  require 'diary/item'
21
22
  require 'diary/draft'
22
23
  require 'diary/page'
@@ -1,27 +1,14 @@
1
- module Diary
2
- class Draft < Item
3
- include Message
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
- def initialize(title, not_used = nil)
6
- super(title, "")
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
- puts "#{Published} #{to}/#{title}.md"
17
- rescue Errno::ENOENT
18
- puts "#{Error} No draft found"
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
@@ -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 :file_name
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
- puts "#{Updated} #{output_file_path}"
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
- puts "#{Identical} #{output_file_path}"
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
- nice_slug
41
+ @slug || basename
68
42
  end
69
43
 
70
- def file_path
71
- @file_path ||= [self.class.base_directory, @dir_path, "#{file_name}.md"].compact.join('/')
44
+ def title
45
+ @title || data.title
72
46
  end
73
-
74
- def output_file_path
75
- @output_file_path ||= ["output", @dir_path, "#{slug}.html"].compact.join('/')
47
+
48
+ def path
49
+ file ? file.path : File.join(base_directory, directory, "#{slug}.md")
76
50
  end
77
51
 
78
- def class_name
79
- self.class.name.split('::').slice(-1)
52
+ def directory
53
+ @directory || File.dirname(path).gsub("#{base_directory}/", "")
80
54
  end
81
55
 
82
- def changed?
83
- file.mtime > File.new(output_file_path).ctime
84
- rescue Errno::ENOENT
85
- true
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 file_exists?
109
- File.exists?(file_path)
76
+ def base_directory
77
+ self.class.class_variable_get(:@@base_directory)
110
78
  end
111
79
 
112
- def render
113
- liquidize.render('yield' => markdownize)
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 = file_name.gsub(s, rep)
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 self.resolve_directory(path)
138
- ary = path.split('/')
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 self.resolve_file_name(path)
145
- path.gsub(/.md/, '').split('/').slice(-1)
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 self.create(title, path, directory)
149
- FileUtils.mkdir_p("#{base_directory}/#{directory}")
150
- file = File.new(path, "w+")
151
- file.puts("---\ntitle: #{title}\n---\n")
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
- puts "#{Created} #{path}"
132
+ end
133
+
134
+ def ensure_directories_exists
135
+ FileUtils.mkpath(File.dirname(path))
155
136
  end
156
137
  end
157
- end
138
+ end
@@ -5,9 +5,9 @@ module Diary
5
5
  Skip = " \e[1;31mskip\e[0m"
6
6
 
7
7
  # Green
8
- Published = " \e[1;32mpublished\e[0m"
9
- Created = " \e[1;32mcreated\e[0m"
10
- Updated = " \e[1;32mupdated\e[0m"
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
@@ -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
@@ -1,13 +1,6 @@
1
- module Diary
2
- class Page < Item
3
- def initialize(title, path = "")
4
- super(title, path)
5
- end
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
@@ -1,25 +1,22 @@
1
- module Diary
2
- class Post < Item
3
- def initialize(title, date_or_path = Date.today)
4
- super(title, date_or_path.to_s.split('-').join('/'))
5
- end
6
-
7
- def date
8
- @date ||= Date.parse(@dir_path)
9
- end
10
-
11
- def output(force = false)
12
- if date <= Date.today
13
- super(force)
14
- else
15
- puts "#{Skip} #{file_path}"
16
- end
17
- end
18
-
19
- private
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
@@ -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.compile(force)
20
- Page.compile(force)
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
- puts "#{Created} #{path}"
37
+ say Create, path
38
38
  else
39
- puts "#{Exist} #{path}"
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
- puts "#{Created} #{path}"
49
+ say Create, path
50
50
  else
51
- puts "#{Exist} #{path}"
51
+ say Exist, path
52
52
  end
53
53
  end
54
54
  end
@@ -1,28 +1,27 @@
1
1
  module Diary
2
- class Template
2
+ module Template
3
3
  include Message
4
-
5
- def self.lookup(item)
6
- return file(item.data.template) if exists?(item.data.template)
7
- return file(item.file_name) if exists?(item.file_name)
8
- return file(item.class_name.downcase) if exists?(item.class_name.downcase)
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 self.file(name)
15
- File.new(resolve(name)).tap do |file|
16
- puts "#{Invoke} #{file.path}"
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 self.resolve(name)
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
- - 4
9
- version: 0.1.4
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-06-28 00:00:00 +02:00
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