Module: RuneBlog::REPL

Defined in:
lib/helpers-repl.rb,
lib/repl.rb

Constant Summary collapse

Patterns =
{"help"              => :cmd_help, 
  "h"                 => :cmd_help,
  "version"           => :cmd_version,
  "v"                 => :cmd_version,
  "list views"        => :cmd_list_views, 
  "lsv"               => :cmd_list_views, 

  "new view $name"    => :cmd_new_view,

  "tags"              => :cmd_tags,
  "import"            => :cmd_import,

  "new post"          => :cmd_new_post,
  "p"                 => :cmd_new_post,
  "post"              => :cmd_new_post,

  "change view $name" => :cmd_change_view,
  "cv $name"          => :cmd_change_view,
  "cv"                => :cmd_change_view,  # 0-arity must come second

  "config"            => :cmd_config,

  "list posts"        => :cmd_list_posts,
  "lsp"               => :cmd_list_posts,

  "list drafts"       => :cmd_list_drafts,
  "lsd"               => :cmd_list_drafts,

  "list assets"       => :cmd_list_assets,
  "lsa"               => :cmd_list_assets,

  "delete >postid"        => :cmd_remove_post,
  "undel $postid"     => :cmd_undelete_post,

  "edit $postid"      => :cmd_edit_post,
  "ed $postid"        => :cmd_edit_post,
  "e $postid"         => :cmd_edit_post,

  "preview"           => :cmd_preview,

  "browse"            => :cmd_browse,

  "rebuild"           => :cmd_rebuild,

  "publish"           => :cmd_publish,

  "ssh"               => :cmd_ssh,

  "q"                 => :cmd_quit,
  "quit"              => :cmd_quit
}
Abbr =
{
"h"                 => :cmd_help,
"v"                 => :cmd_version,
"lsv"               => :cmd_list_views, 
"p"                 => :cmd_new_post,
"cv $name"          => :cmd_change_view,
"cv"                => :cmd_change_view,  # 0-arity must come second
"lsp"               => :cmd_list_posts,
"lsd"               => :cmd_list_drafts,
"list assets"       => :cmd_list_assets,
"lsa"               => :cmd_list_assets,
"rm $postid"        => :cmd_remove_post,
"ed $postid"        => :cmd_edit_post,
"e $postid"         => :cmd_edit_post,
"q"                 => :cmd_quit
}
Regexes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.choose_method(cmd) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/helpers-repl.rb', line 93

def self.choose_method(cmd)
  cmd = cmd.strip
  found = nil
  params = nil
  Regexes.each_pair do |rx, meth|
    m = cmd.match(rx)
    result = m ? m.to_a : nil
    next unless result
    found = meth
    params = m[1]
  end
  meth = found || :cmd_INVALID
  params = cmd if meth == :cmd_INVALID
puts "choose: #{[meth, params].inspect}"
  [meth, params]
end

Instance Method Details

#_remove_post(arg, testing = false) ⇒ Object



182
183
184
185
186
# File 'lib/repl.rb', line 182

def _remove_post(arg, testing=false)
  id = get_integer(arg)
  result = @blog.remove_post(id)
  puts "Post #{id} not found" if result.nil?
end

#all_tagsObject



218
219
220
221
222
# File 'lib/helpers-repl.rb', line 218

def all_tags
  all = []
  @blog.views.each {|view| all.append(*tags_for_view(view)) }
  all.sort + ["NEW TAG"]
end

#ask(prompt, meth = :to_s) ⇒ Object



116
117
118
119
120
121
# File 'lib/helpers-repl.rb', line 116

def ask(prompt, meth = :to_s)
  print prompt
  str = gets
  str.chomp!
  str.send(meth)
end

#check_empty(arg) ⇒ Object



160
161
162
# File 'lib/helpers-repl.rb', line 160

def check_empty(arg)
  raise InternalError(caller[0], arg.inspect)  unless arg.nil?
end

#check_file_exists(file) ⇒ Object



170
171
172
# File 'lib/helpers-repl.rb', line 170

def check_file_exists(file)
  raise FileNotFound(file) unless File.exist?(file)
end

#cmd_browse(arg, testing = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/repl.rb', line 69

def cmd_browse(arg, testing = false)
  reset_output
  check_empty(arg)
  url = @blog.view.publisher.url
  if url.nil?   
    output! "Publish first."
    puts "\n  Publish first."
    return @out
  end
  result = system!("open '#{url}'")
  raise CantOpen(url) unless result
  return @out
end

#cmd_change_view(arg, testing = false) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/repl.rb', line 138

def cmd_change_view(arg, testing = false)
  reset_output
  # Simplify this
  if arg.nil?
    viewnames = @blog.views.map {|x| x.name }
    n = viewnames.find_index(@blog.view.name)
    name = @blog.view.name
    k, name = STDSCR.menu(title: "Views", items: viewnames, curr: n) unless testing
    @blog.view = name
    output name + "\n"
    puts "\n  ", fx(name, :bold), "\n" unless testing
    return @out
  else
    if @blog.view?(arg)
      @blog.view = arg  # reads config
      output "View: " + @blog.view.name.to_s
      puts "\n  ", fx(arg, :bold), "\n" unless testing
    end
  end
  return @out
end

#cmd_clear(arg, testing = false) ⇒ Object



25
26
27
28
29
# File 'lib/repl.rb', line 25

def cmd_clear(arg, testing = false)
  check_empty(arg)
  STDSCR.cwin.clear
  STDSCR.cwin.refresh
end

#cmd_config(arg, testing = false) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/repl.rb', line 39

def cmd_config(arg, testing = false)
  check_empty(arg)
  dir = @blog.view.dir
  items = ["themes/standard/blogview.lt3", "themes/standard/post-index.lt3"] 
  num, fname = STDSCR.menu(title: "Edit file:", items: items)
  edit_file("#{dir}/#{fname}")
end

#cmd_customize(arg, testing = false) ⇒ Object

Currently not used



48
49
50
51
52
53
54
55
56
# File 'lib/repl.rb', line 48

def cmd_customize(arg, testing = false)
  # add extra views? add tags?
  puts "\n  This is still buggy.\n "
  return

  avail_tags = all_tags
  tags = STDSCR.multimenu(items: avail_tags)
  @blog. = tags
end

#cmd_edit_post(arg, testing = false) ⇒ Object

– FIXME affects linking, building, publishing…



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/repl.rb', line 203

def cmd_edit_post(arg, testing = false)
  reset_output
  id = get_integer(arg)
  # Simplify this
  tag = "#{'%04d' % id}"
  files = Find.find(@blog.root+"/drafts").to_a
  files = files.grep(/#{tag}-.*lt3/)
  files = files.map {|f| File.basename(f) }
  if files.size > 1
    msg = "Multiple files: #{files}"
    output msg
    puts msg unless testing
    return [false, msg]
  end
  if files.empty?
    msg = "\n  Can't edit post #{id}"
    output msg
    puts msg unless testing
    return [false, msg]
  end

  file = files.first
  draft = "#{@blog.root}/drafts/#{file}"
  result = edit_file(draft)
  @blog.generate_post(draft)
end

#cmd_help(arg, testing = false) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/repl.rb', line 322

def cmd_help(arg, testing = false)
  reset_output 
  check_empty(arg)
  msg = <<-EOS

     Commands:

     h, help           This message
     q, quit           Exit the program
     v, version        Print version information

     change view VIEW  Change current view
     cv VIEW           Change current view

     new view          Create a new view

     list views        List all views available
     lsv               Same as: list views

*      config            Edit the publish file or the templates
*      customize         (BUGGY) Change set of tags, extra views

     p, post           Create a new post
     new post          Same as post (create a post)

*      import ASSETS     Import assets (images, etc.)

     lsp, list posts   List posts in current view

     lsd, list drafts  List all posts regardless of view

     delete ID [ID...] Remove multiple posts
     undelete ID       Undelete a post
     edit ID           Edit a post

     preview           Look at current (local) view in browser
     browse            Look at current (published) view in browser
     rebuild           Regenerate all posts and relink
     publish           Publish (current view)
     ssh               Login to remote server
  EOS
  output msg
  msg.each_line do |line|
    next if testing
    line.chomp!
    s1, s2 = line[0..22], line[23..-1]
    print fx(s1, :bold)
    puts s2
  end
  puts unless testing
  @out
end

#cmd_import(arg, testing = false) ⇒ Object



63
64
65
66
67
# File 'lib/repl.rb', line 63

def cmd_import(arg, testing = false)
  check_empty(arg)
  files = ask("\n  File(s) = ")
  system!("cp #{files} #{@blog.root}/views/#{@blog.view.name}/assets/")
end

#cmd_INVALID(arg, testing = false) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/repl.rb', line 314

def cmd_INVALID(arg, testing = false)
  reset_output "\n  Command '#{arg}' was not understood."
  print fx("\n  Command ", :bold)
  print fx(arg, Red, :bold)
  puts fx(" was not understood.\n ", :bold)
  @out
end

#cmd_list_assets(arg, testing = false) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/repl.rb', line 288

def cmd_list_assets(arg, testing = false)
  reset_output
  check_empty(arg)
  dir = @blog.view.dir + "/assets"
  assets = Dir[dir + "/*"]
  if assets.empty?
    output! "No assets"
    puts "  No assets" unless testing
    return @out
  else
    puts unless testing
    assets.each do |name| 
      asset = File.basename(name)
      outstr asset
      puts "  ", fx(asset, Blue) unless testing
    end
  end
  puts unless testing
  @out
end

#cmd_list_drafts(arg, testing = false) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/repl.rb', line 267

def cmd_list_drafts(arg, testing = false)
  reset_output
  check_empty(arg)
  drafts = @blog.drafts  # current view
  if drafts.empty?
    output! "No drafts"
    puts "\n  No drafts\n " unless testing
    return @out
  else
    puts unless testing
    drafts.each do |draft| 
      outstr "  #{colored_slug(draft.sub(/.lt3$/, ""))}\n" 
      base = draft.sub(/.lt3$/, "")
      num, rest = base[0..3], base[4..-1]
      puts "  ", fx(num, Red), fx(rest, Blue) unless testing
    end
  end
  puts unless testing
  @out
end

#cmd_list_posts(arg, testing = false) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/repl.rb', line 244

def cmd_list_posts(arg, testing = false)
  reset_output
  check_empty(arg)
  posts = @blog.posts  # current view
  str = @blog.view.name + ":\n"
  output str
  puts unless testing
  puts "  ", fx(str, :bold) unless testing
  if posts.empty?
    output! "No posts"
    puts "  No posts" unless testing
  else
    posts.each do |post| 
      outstr "  #{colored_slug(post)}\n" 
      base = post.sub(/.lt3$/, "")
      num, rest = base[0..3], base[4..-1]
      puts "  ", fx(num, Red), fx(rest, Blue) unless testing
    end
  end
  puts unless testing
  @out
end

#cmd_list_views(arg, testing = false) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/repl.rb', line 230

def cmd_list_views(arg, testing = false)
  reset_output("\n")
  check_empty(arg)
  puts unless testing
  @blog.views.each do |v| 
    v = v.to_s
    v = fx(v, :bold) if v == @blog.view.name
    output v + "\n"
    puts "  ", v unless testing
  end
  puts unless testing
  @out
end

#cmd_new_post(arg, testing = false) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/repl.rb', line 170

def cmd_new_post(arg, testing = false)
  reset_output
  check_empty(arg)
  title = ask("\nTitle: ")
  @blog.create_new_post(title)
#   STDSCR.clear
  @out
rescue => err
  puts err
  puts err.backtrace.join("\n")
end

#cmd_new_view(arg, testing = false) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/repl.rb', line 160

def cmd_new_view(arg, testing = false)
  reset_output
  @blog.create_view(arg)
  edit_file(@blog.view.dir/"themes/standard/global.lt3")
  @blog.change_view(arg)
  @out
rescue ViewAlreadyExists
  puts 'Blog already exists'
end

#cmd_preview(arg, testing = false) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/repl.rb', line 83

def cmd_preview(arg, testing = false)
  reset_output
  check_empty(arg)
  local = @blog.view.local_index
  result = system!("open #{local}")
  raise CantOpen(local) unless result
  @out
end

#cmd_publish(arg, testing = false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
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
# File 'lib/repl.rb', line 92

def cmd_publish(arg, testing = false)
# Future Hal says please refactor this
  puts unless testing
  reset_output
  check_empty(arg)
  unless @blog.view.can_publish?
    msg = "Can't publish... see globals.lt3"
    puts msg unless testing
    output! msg
    return @out
  end

  # Need to check dirty/clean status first
  dirty, all, assets = @blog.view.publishable_files
  files = dirty
  if dirty.empty?
    puts fx("\n  No files are out of date." + " "*20, :bold)
    print "  Publish anyway? "
    yn = RubyText.gets.chomp
    files = all if yn == "y"
  end
  return @out if files.empty?

  ret = RubyText.spinner(label: " Publishing... ") do
    @blog.view.publisher.publish(files, assets)  # FIXME weird?
  end
  return @out unless ret

  vdir = @blog.view.dir
  dump("fix this later", "#{vdir}/last_published")
  if ! testing || ! ret
    puts "  ...finished.\n " 
    output! "...finished.\n"
  end
  return @out
end

#cmd_quit(arg, testing = false) ⇒ Object



18
19
20
21
22
23
# File 'lib/repl.rb', line 18

def cmd_quit(arg, testing = false)
  check_empty(arg)
  RubyText.stop
  system!("tput clear")
  exit
end

#cmd_rebuild(arg, testing = false) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/repl.rb', line 129

def cmd_rebuild(arg, testing = false)
  debug "Starting cmd_rebuild..."
  reset_output
  check_empty(arg)
  puts unless testing
  @blog.generate_view(@blog.view)
  @out
end

#cmd_remove_post(arg, testing = false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/repl.rb', line 188

def cmd_remove_post(arg, testing = false)
puts "arg = #{arg.inspect} is a #{arg.class}"
  reset_output
  args = arg.split
  args.each do |x| 
    # FIXME
    ret = _remove_post(x.to_i, false)
    puts ret
    output ret
  end
  @out
end

#cmd_ssh(arg, testing = false) ⇒ Object



309
310
311
312
# File 'lib/repl.rb', line 309

def cmd_ssh(arg, testing = false)
  pub = @blog.view.publisher
  system!("ssh #{pub.user}@#{pub.server}")
end

#cmd_tags(arg, testing = false) ⇒ Object



58
59
60
61
# File 'lib/repl.rb', line 58

def cmd_tags(arg, testing = false)
  Dir.chdir(@blog.root + "/views/" + @blog.view.name)
  edit_file("tagpool")
end

#cmd_version(arg, testing = false) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/repl.rb', line 31

def cmd_version(arg, testing = false)
  reset_output
  check_empty(arg)
  output RuneBlog::VERSION
  puts fx("\n  RuneBlog", :bold), fx(" v #{RuneBlog::VERSION}\n", Red) unless testing
  @out
end

#colored_slug(slug) ⇒ Object



183
184
185
# File 'lib/helpers-repl.rb', line 183

def colored_slug(slug)
  slug[0..3] + slug[4..-1]
end

#edit_file(file) ⇒ Object



11
12
13
14
15
16
# File 'lib/repl.rb', line 11

def edit_file(file)
  result = system!("#{@blog.editor} #{file}")
  raise EditorProblem(file) unless result
  sleep 0.1
  STDSCR.clear
end

#error(err) ⇒ Object

Hmm, this is duplicated



110
111
112
113
114
# File 'lib/helpers-repl.rb', line 110

def error(err)  # Hmm, this is duplicated
  str = "\n  Error: #{err}"
  puts str
  puts err.backtrace.join("\n")
end

#error_cant_delete(files) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/helpers-repl.rb', line 174

def error_cant_delete(files)
  case files
    when String
      raise CantDelete(files)
    when Array
      raise CantDelete(files.join("\n"))
  end
end

#flush_output(initial = "") ⇒ Object



133
134
135
136
137
138
# File 'lib/helpers-repl.rb', line 133

def flush_output(initial = "")
  CantOpen
  @out ||= ""
  puts @out
  reset_output
end

#get_integer(arg) ⇒ Object



164
165
166
167
168
# File 'lib/helpers-repl.rb', line 164

def get_integer(arg)
  Integer(arg) 
rescue 
  raise ArgumentError, "'#{arg}' is not an integer"
end

#import(arg = nil) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/helpers-repl.rb', line 187

def import(arg = nil)
  raise "Not implemented at present..."
  arg = nil if arg == ""
  arg ||= ask("Filename: ")  # check validity later
  name = arg
  grep = `grep ^.title #{name}`
  @title = grep.sub(/^.title /, "")
  @slug = @blog.make_slug(@title) # import (not impl)
  @fname = @slug + ".lt3"
  result = system!("cp #{name} #@root/drafts/#@fname")
  raise CantCopy(name, "#@root/drafts/#@fname") unless result

  edit_initial_post(@fname)
  process_post(@fname)
  link_post_all_views(@meta)
rescue => err
  error(err)
end

#output(str) ⇒ Object

n and indent



140
141
142
143
# File 'lib/helpers-repl.rb', line 140

def output(str)  # \n and indent
  @out ||= ""
  @out << "  " + str.to_s
end

#output!(str) ⇒ Object

n and indent



150
151
152
153
# File 'lib/helpers-repl.rb', line 150

def output!(str)  # \n and indent
  @out ||= ""
  @out << "  " + str
end

#output_newline(n = 1) ⇒ Object



155
156
157
158
# File 'lib/helpers-repl.rb', line 155

def output_newline(n = 1)
  @out ||= ""
  n.times { @out << "\n" }
end

#outstr(str) ⇒ Object

indent



145
146
147
148
# File 'lib/helpers-repl.rb', line 145

def outstr(str)  # indent
  @out ||= ""
  @out << str
end

#reset_output(initial = "") ⇒ Object



128
129
130
131
# File 'lib/helpers-repl.rb', line 128

def reset_output(initial = "")
  @out ||= ""
  @out.replace initial
end

#tags_for_view(vname = @blog.view) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/helpers-repl.rb', line 206

def tags_for_view(vname = @blog.view)
  Dir.chdir(vname) do
    fname = "tagpool"
    if File.exist?(fname)
      tags = File.readlines(fname).map(&:chomp)
    else
      tags = []
    end
  end
  tags.sort
end

#yesno(prompt, meth = :to_s) ⇒ Object



123
124
125
126
# File 'lib/helpers-repl.rb', line 123

def yesno(prompt, meth = :to_s)
  print prompt
  gets.chomp.upcase[0] == "Y"
end