Class: Nippocf::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/nippocf/cli.rb

Constant Summary collapse

KEYCHAIN_SERVICE_NAME =
'Nippocf'

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



21
22
23
24
# File 'lib/nippocf/cli.rb', line 21

def initialize(argv)
  @argv = argv
  parse_options!
end

Instance Method Details

#runObject



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
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nippocf/cli.rb', line 26

def run
  rpc = connect_to_confl

  blog_entries = rpc.getBlogEntries("~#{username}")
  title = time.strftime('%Y/%m/%d')
  entry_summary = blog_entries.find {|entry| entry['title'] == title }

  markdown_text = ""

  if entry_summary
    entry = rpc.getBlogEntry(entry_summary['id'])
    # entry contains markdown content
    doc = Nokogiri::HTML(entry['content'])
    elm = doc.css('div.markdown_source').first
    markdown_text = elm.text if elm
  else
    entry = {
      "content" => "",
      "title" => title,
      "space" => "~#{username}",
      "author" => username,
      "publishDate" => time,
      "permissions" => "0"
    }
  end

  Dir.mktmpdir do |dir|
    filename = File.join(dir, 'editing.md')
    open(filename, 'w') do |f|
      f.write markdown_text
    end
    editor = ENV['EDITOR'] || 'vim'
    system "#{editor} #{filename}"
    markdown_text = File.read(filename)
  end

  entry['content'] = GitHub::Markdown.render(markdown_text)
  entry['content'] << "<div class=\"markdown_source\" style=\"display:none\">#{markdown_text}</div>"
  rpc.storeBlogEntry(entry)
end