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.



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

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

Class Method Details

.config(options) ⇒ Object



457
458
459
460
461
462
463
# File 'lib/homeostasis.rb', line 457

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



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/homeostasis.rb', line 495

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[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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/homeostasis.rb', line 465

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, '/'))
    post[:private] = true if post[:date] > Date.today
    @@posts << post
  end
  @@posts = @@posts.reject { |p| p[:private] }.sort_by { |p| p[:date] }.reverse
end

#before_renderObject



484
485
486
487
488
489
490
491
492
493
# File 'lib/homeostasis.rb', line 484

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



528
529
530
531
# File 'lib/homeostasis.rb', line 528

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