Module: RuneBlog::REPL

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

Overview

make_exception(:EditorProblem, “Could not edit $1”)

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,

#    "customize"         => :cmd_customize,
  "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,

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

  "kill >postid"      => :cmd_kill, 

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

  "preview"           => :cmd_preview,

  "browse"            => :cmd_browse,

  "relink"            => :cmd_relink,

  "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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/helpers-repl.rb', line 98

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
  [meth, params]
end

Instance Method Details

#all_tagsObject



238
239
240
241
242
# File 'lib/helpers-repl.rb', line 238

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



120
121
122
123
124
125
# File 'lib/helpers-repl.rb', line 120

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

#ask_publishing_infoObject

returns Publishing object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/helpers-repl.rb', line 211

def ask_publishing_info   # returns Publishing object
  verify(@blog => "@blog is nil", 
         @blog.view => "@blog.view is nil")
  # user, server, root, path, protocol = "http"
  puts "Please enter publishing data for view #{@blog.view}..."
  user = ask("User: ")
  root = ask("Doc root: ")
  server = ask("Server: ")
  path = ask("View path: ")
  proto = ask("Protocol (ENTER for http): ")
  [user, root, server, path, proto].each {|x| x.chomp! }
  proto = "http" if proto.empty?
  RuneBlog::Publishing.new(user, server, root, path, proto)
end

#check_empty(arg) ⇒ Object



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

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

#check_file_exists(file) ⇒ Object



175
176
177
# File 'lib/helpers-repl.rb', line 175

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

#cmd_browse(arg, testing = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/repl.rb', line 72

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/repl.rb', line 156

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
46
47
48
# File 'lib/repl.rb', line 39

def cmd_config(arg, testing = false)
  check_empty(arg)
  dir = @blog.view.dir
# FIXME bad path
  items = ["publish", 
           "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



51
52
53
54
55
56
57
58
59
# File 'lib/repl.rb', line 51

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…



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/repl.rb', line 230

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



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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/repl.rb', line 349

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

     rm ID             Remove a post
     kill ID 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
     relink            Regenerate index for all views
     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



66
67
68
69
70
# File 'lib/repl.rb', line 66

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



341
342
343
344
345
346
347
# File 'lib/repl.rb', line 341

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_kill(arg, testing = false) ⇒ Object



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

def cmd_kill(arg, testing = false)
  reset_output
  args = arg.split
  args.each do |x| 
    # FIXME
    ret = cmd_remove_post(x.to_i, false)
    puts ret
    output ret
  end
  @out
end

#cmd_list_assets(arg, testing = false) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/repl.rb', line 315

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/repl.rb', line 294

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/repl.rb', line 271

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



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/repl.rb', line 257

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



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/repl.rb', line 193

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/repl.rb', line 178

def cmd_new_view(arg, testing = false)
  reset_output
  @blog.create_view(arg)
  @blog.change_view(arg)
  resp = yesno("Add publishing/hosting info now? (Y/N): ")
  if resp
    @blog.view.publisher = ask_publishing_info
    out = @blog.view.dir + "/publish"
    write_config(@blog.view.publisher, out)
  end
  @out
rescue ViewAlreadyExists
  puts 'Blog already exists'
end

#cmd_preview(arg, testing = false) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/repl.rb', line 86

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



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
128
129
# File 'lib/repl.rb', line 95

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?
    puts "Can't publish without entries in #{@blog.view.name}/publish" unless testing
    output! "Can't publish without entries in #{@blog.view.name}/publish"
    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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/repl.rb', line 131

def cmd_rebuild(arg, testing = false)
  debug "Starting cmd_rebuild..."
  reset_output
  check_empty(arg)
  puts unless testing
  files = @blog.find_draft_slugs
  if files.empty? 
    msg = "No files changed"
    output! msg
    puts "\n  #{msg}\n " unless testing
    return @out
  end
  files.each {|file| @blog.rebuild_post(file) }
  @blog.dirty_views.each {|view| generate_index(view) }  # All views for now?
  File.write("#{@blog.root}/drafts/last_rebuild", Time.now)
  @out
end


149
150
151
152
153
154
# File 'lib/repl.rb', line 149

def cmd_relink(arg, testing = false)
  reset_output
  check_empty(arg)
  @blog.relink
  @out
end

#cmd_remove_post(arg, testing = false) ⇒ Object

– FIXME affects linking, building, publishing…



219
220
221
222
223
224
225
226
# File 'lib/repl.rb', line 219

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

#cmd_ssh(arg, testing = false) ⇒ Object



336
337
338
339
# File 'lib/repl.rb', line 336

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

#cmd_tags(arg, testing = false) ⇒ Object



61
62
63
64
# File 'lib/repl.rb', line 61

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



188
189
190
# File 'lib/helpers-repl.rb', line 188

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



114
115
116
117
118
# File 'lib/helpers-repl.rb', line 114

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

#error_cant_delete(files) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/helpers-repl.rb', line 179

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

#find_all_assets(list, views) ⇒ Object

find_all_assets



264
265
266
267
# File 'lib/helpers-repl.rb', line 264

def find_all_assets(list, views)
  list ||= []
  list.each {|asset| puts "#{asset} => #{find_asset(asset, views)}" }
end

#find_asset(asset_name) ⇒ Object

FIXME is this per-post?



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/helpers-repl.rb', line 248

def find_asset(asset_name)    # , views)
  search_path = proc do |path| 
    full_path = path + asset_name
    return full_path if File.exist?(full_path)
  end 
  check_meta(@meta, "find_asset")
  views = @meta.views
  views.each do |view| search_path.call("#{view.dir}/#{@meta.slug}/assets/") end
  views.each do |view| search_path.call(view.dir + "/assets/") end
  search_path.call(@root + "/assets/", asset_name)
  # If all fail... return nil
  return nil
end

#flush_output(initial = "") ⇒ Object



137
138
139
140
141
142
# File 'lib/helpers-repl.rb', line 137

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

#get_integer(arg) ⇒ Object



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

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

#import(arg = nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/helpers-repl.rb', line 192

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



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

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

#output!(str) ⇒ Object

n and indent



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

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

#output_newline(n = 1) ⇒ Object



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

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

#outstr(str) ⇒ Object

indent



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

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

#reset_output(initial = "") ⇒ Object



132
133
134
135
# File 'lib/helpers-repl.rb', line 132

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

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



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/helpers-repl.rb', line 226

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



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

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