Class: Homeostasis::Blog

Inherits:
Stasis::Plugin
  • Object
show all
Includes:
Helpers
Defined in:
lib/homeostasis.rb

Constant Summary collapse

DATE_REGEX =
/^(\d{4}-\d{2}-\d{2})-/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stasis) ⇒ Blog

Returns a new instance of Blog.



440
441
442
443
444
445
446
447
448
# File 'lib/homeostasis.rb', line 440

def initialize(stasis)
  @stasis = stasis
  @@directory = nil
  @@path = nil
  @@url = nil
  @@title = nil
  @@desc = nil
  @@posts = []
end

Class Method Details

.config(options) ⇒ Object



450
451
452
453
454
455
456
# File 'lib/homeostasis.rb', line 450

def self.config(options)
  @@directory = options[:directory]
  @@path = options[:path] || options[:directory]
  @@url = options[:url]
  @@title = options[:title]
  @@desc = options[:desc]
end

Instance Method Details

#after_allObject



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/homeostasis.rb', line 487

def after_all
  return if @@directory.nil?
  blog_dest = File.join(@stasis.destination, @@path)
  FileUtils.mkdir_p(blog_dest) if !Dir.exist?(blog_dest)
  Dir.glob("#{File.join(@stasis.destination, @@directory)}/*").each do |filename|
    next if (base = File.basename(filename)) !~ DATE_REGEX
    FileUtils.mv(filename, File.join(blog_dest, base.sub(DATE_REGEX, '')))
  end
  url = h(File.join(@@url, @@path))
  zone = Time.new.zone
  rss = "<?xml version=\"1.0\"?>\n"
  rss += "<rss version=\"2.0\">\n"
  rss += "  <channel>\n"
  rss += "    <title>#{h @@title}</title>\n" if @@title
  rss += "    <link>#{h @@url}/</link>\n" if @@url
  rss += "    <description>#{h @@desc}</description>\n" if @@desc
  blog_posts.reject {|p| p.has_key?(:norss) }[0..5].each do |post|
    body = post[:body]
    body.gsub!(/(href|src)=('|")\//, "\\1=\\2#{@@url}/")
    rss += "    <item>\n"
    rss += "      <title>#{h post[:title]}</title>\n"
    rss += "      <link>#{h(File.join(@@url, post[:path]))}</link>\n"
    rss += "      <pubDate>#{post[:date].strftime("%a, %d %b %Y 0:00:01 #{zone}")}</pubDate>\n"
    rss += "      <description>#{h post[:body]}</description>\n"
    rss += "    </item>\n"
  end
  rss += "  </channel>\n"
  rss += "</rss>"
  File.open(File.join(blog_dest, 'rss.xml'), 'w') do |f|
    f.puts(rss)
  end
end

#before_allObject



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/homeostasis.rb', line 458

def before_all
  return if @@directory.nil?
  @@posts = []
  front_site = Homeostasis::Front._front_site
  Dir.glob("#{File.join(@stasis.root, @@directory)}/*").each do |filename|
    next if File.basename(filename) !~ DATE_REGEX
    date = $1
    post = front_site[filename.sub(@stasis.root, '')[1..-1]] || {}
    post[:blog] = true
    post[:date] = Date.parse(date)
    post[:path] = post[:path].sub(
      "/#{@@directory}/#{date}-",
      File.join('/', @@path, '/'))
    @@posts << post
  end
  @@posts = @@posts.sort_by { |post| post[:date] }.reverse
end

#before_renderObject



476
477
478
479
480
481
482
483
484
485
# File 'lib/homeostasis.rb', line 476

def before_render
  path = Helpers.stasis_path || @stasis.path
  return if path.nil?

  post = Homeostasis::Front._front_site[Homeostasis::Front._front_key(@stasis, path)]
  if post && post[:blog] && post[:date] && post[:path]
    yaml, body = Front.preamble_load(path)
    post[:body] = render_multi(path, body, @stasis.action)
  end
end

#blog_postsObject



520
521
522
523
# File 'lib/homeostasis.rb', line 520

def blog_posts
  raise 'Homeostasis::Blog#config never called' if @@directory.nil?
  @@posts
end