Class: Habari2md::PostExporter

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/habari2md.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dirObject (readonly)

Output directory



112
113
114
# File 'lib/habari2md.rb', line 112

def dir
  @dir
end

#managerObject (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

Parameters:

  • post (Hash)


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)

  author = 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: #{author}\n" unless author == 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.

Returns:

  • (Boolean)


160
161
162
# File 'lib/habari2md.rb', line 160

def published?(post)
  post[:status] == 2
end

#untitledObject

Placeholder title for untitled posts



123
124
125
# File 'lib/habari2md.rb', line 123

def untitled
  "Untitled"
end