Class: Habari2md::PostExporter
- Inherits:
-
Object
- Object
- Habari2md::PostExporter
- Includes:
- Celluloid
- Defined in:
- lib/habari2md.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Output directory.
-
#manager ⇒ Object
readonly
Manager actor.
Instance Method Summary collapse
-
#done(post = {}) ⇒ Object
Signal the managing actor when a post has been exported.
-
#export(post) ⇒ Object
Export one post to disk.
-
#initialize(dest_dir, manager_actor) ⇒ PostExporter
constructor
A new instance of PostExporter.
-
#published?(post) ⇒ Boolean
This actually depends on the values in the poststatus table.
-
#untitled ⇒ Object
Placeholder title for untitled posts.
Constructor Details
#initialize(dest_dir, manager_actor) ⇒ PostExporter
Returns a new instance of PostExporter.
117 118 119 120 |
# File 'lib/habari2md.rb', line 117 def initialize(dest_dir, manager_actor) @dir = Pathname.new(dest_dir) @manager = manager_actor end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Output directory
112 113 114 |
# File 'lib/habari2md.rb', line 112 def dir @dir end |
#manager ⇒ Object (readonly)
Manager actor
115 116 117 |
# File 'lib/habari2md.rb', line 115 def manager @manager end |
Instance Method Details
#done(post = {}) ⇒ Object
Signal the managing actor when a post has been exported
128 129 130 |
# File 'lib/habari2md.rb', line 128 def done(post = {}) manager.post_exported(post[:id]) end |
#export(post) ⇒ Object
Export one post to disk
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/habari2md.rb', line 134 def export(post) # Ignore deleted posts and drafts. return done(post) unless published?(post) = manager.user(post[:user_id])[:username] title = post[:title].gsub(/[\r\n]/, '') title = untitled if title == "" date = Time.strptime(post[:pubdate].to_s, "%s").strftime("%Y-%m-%d") filename = dir.join("#{date}-#{post[:slug]}.md") return done(post) if File.exists?(filename) && ENV['FORCE'] == nil # Make sure content is at least formatted with <p> tags before # conversion. content = Habari2md::Text.simple_format(post[:content]) File.open(filename, 'w+') do |fh| fh << "---\n" fh << "title: #{title}\n" fh << "author: #{}\n" unless == nil fh << "---\n\n" fh << Habari2md::Text.html2text(content) end done(post) end |
#published?(post) ⇒ Boolean
This actually depends on the values in the poststatus table.
160 161 162 |
# File 'lib/habari2md.rb', line 160 def published?(post) post[:status] == 2 end |
#untitled ⇒ Object
Placeholder title for untitled posts
123 124 125 |
# File 'lib/habari2md.rb', line 123 def untitled "Untitled" end |