Class: MrPoole::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/mr_poole/commands.rb

Constant Summary collapse

POSTS_FOLDER =
'_posts'
DRAFTS_FOLDER =
'_drafts'

Instance Method Summary collapse

Constructor Details

#initialize(extension = 'md') ⇒ Commands

Returns a new instance of Commands.



9
10
11
12
# File 'lib/mr_poole/commands.rb', line 9

def initialize(extension='md')
  @helper = Helper.new
  @ext = extension
end

Instance Method Details

#draft(opts) ⇒ Object

Generate a non-timestamped draft



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mr_poole/commands.rb', line 42

def draft(opts)
  opts = @helper.ensure_open_struct(opts)

  # the drafts folder might not exist yet...create it just in case
  FileUtils.mkdir_p(DRAFTS_FOLDER)

  slug = if opts.slug.nil? || opts.slug.empty?
           opts.title
         else
           opts.slug
         end
  slug = @helper.get_slug_for(slug)

  # put the metadata into the layout header
  head, ext = @helper.get_layout(opts.layout)
  head.sub!(/^title:\s*$/, "title: #{opts.title}")
  head.sub!(/^date:\s*$/, "date: #{@helper.get_date_stamp}")
  ext ||= @ext

  path = File.join(DRAFTS_FOLDER, "#{slug}.#{ext}")
  f = File.open(path, "w")
  f.write(head)
  f.close
  @helper.open_in_editor(path) # open file if config key set
  path    # return the path, in case we want to do anything useful
end

#post(opts) ⇒ Object

Generate a timestamped post



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
# File 'lib/mr_poole/commands.rb', line 15

def post(opts)
  opts = @helper.ensure_open_struct(opts)
  date = @helper.get_date_stamp

  # still want to escape any garbage in the slug
  slug = if opts.slug.nil? || opts.slug.empty?
           opts.title
         else
           opts.slug
         end
  slug = @helper.get_slug_for(slug)

  # put the metadata into the layout header
  head, ext = @helper.get_layout(opts.layout)
  head.sub!(/^title:\s*$/, "title: #{opts.title}")
  head.sub!(/^date:\s*$/, "date: #{date}")
  ext ||= @ext

  path = File.join(POSTS_FOLDER, "#{date}-#{slug}.#{ext}")
  f = File.open(path, "w")
  f.write(head)
  f.close
  @helper.open_in_editor(path) # open file if config key set
  path    # return the path, in case we want to do anything useful
end

#publish(draftpath, opts = {}) ⇒ Object

Todo make this take a path instead?

Parameters:

  • draftpath (String)

    path to the draft, relative to source directory

  • options (Hash)

    a customizable set of options



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mr_poole/commands.rb', line 74

def publish(draftpath, opts={})
  opts = @helper.ensure_open_struct(opts)
  tail = File.basename(draftpath)

  begin
    infile = File.open(draftpath, "r")
  rescue Errno::ENOENT
    @helper.bad_path(draftpath)
  end

  date = @helper.get_date_stamp
  time = @helper.get_time_stamp

  outpath = File.join(POSTS_FOLDER, "#{date}-#{tail}")
  outfile = File.open(outpath, "w")

  infile.each_line do |line|
    line.sub!(/^date:.*$/, "date: #{date} #{time}\n") unless opts.keep_timestamp
    outfile.write(line)
  end

  infile.close
  outfile.close
  FileUtils.rm(draftpath) unless opts.keep_draft

  outpath
end

#unpublish(inpath, opts = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mr_poole/commands.rb', line 102

def unpublish(inpath, opts={})
  opts = @helper.ensure_open_struct(opts)

  # the drafts folder might not exist yet...create it just in case
  FileUtils.mkdir_p(DRAFTS_FOLDER)

  begin
    infile = File.open(inpath, "r")
  rescue Errno::ENOENT
    @helper.bad_path(inpath)
  end

  slug = inpath.sub(/.*?\d{4}-\d{2}-\d{2}-(.*)/, '\1')
  outpath = File.join(DRAFTS_FOLDER, slug)
  outfile = File.open(outpath, "w")

  infile.each_line do |line|
    line.sub!(/^date:\s*.*$/, "date:") unless opts.keep_timestamp
    outfile.write(line)
  end

  infile.close
  outfile.close
  FileUtils.rm(inpath) unless opts.keep_post

  outpath
end