summaryrefslogtreecommitdiff
path: root/script/converter.rb
blob: ec57404ed1c0475799b18f2e23f5e56057e96ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby

require 'reverse_markdown'

class OldNewsItem

  RE_TITLE = /\#{3}(.*)/
  RE_DATE  = /(\d+-\d+-\d+).*/

  attr_reader :html, :markdown

  def initialize(filename)
    @filename = filename
    @html = File.read(@filename)
  end

  def self.convert(filename)
    new(filename).converted
  end

  def date
    @filename[RE_DATE,1]
  end

  def markdown
    @markdown ||= ReverseMarkdown.convert(@html)
  end

  def title
    markdown[RE_TITLE,1].lstrip
  end

  def body
    markdown.gsub(RE_TITLE,'').lstrip
  end

  def frontmatter
    <<-EOS
---
title: "#{title}"
date: #{date}
---
    EOS
  end

  def converted
    frontmatter << body
  end

end

if __FILE__ == $0
  puts OldNewsItem.convert(ARGV.shift)
end