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,

  "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,

  "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,

  "pre"               => :cmd_preview,

  "browse"            => :cmd_browse,

  "relink"            => :cmd_relink,

  "rebuild"           => :cmd_rebuild,

  "publish"           => :cmd_publish,

  "q"                 => :cmd_quit,
  "quit"              => :cmd_quit
}
Regexes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.choose_method(cmd) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/helpers-repl.rb', line 127

def self.choose_method(cmd)
  cmd = cmd.strip
  found = nil
  params = nil
  Regexes.each_pair do |rx, meth|
    m = cmd.match(rx)
# puts "#{rx} =~ #{cmd.inspect}  --> #{m.to_a.inspect}"
    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

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



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

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

#ask_publishing_infoObject

returns Publishing object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/helpers-repl.rb', line 239

def ask_publishing_info   # returns Publishing object
  # 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



192
193
194
# File 'lib/helpers-repl.rb', line 192

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

#check_file_exists(file) ⇒ Object



202
203
204
# File 'lib/helpers-repl.rb', line 202

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

#cmd_browse(arg) ⇒ Object



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

def cmd_browse(arg)
  reset_output
  check_empty(arg)
  url = @blog.view.publisher.url
  # FIXME Bad logic here.
  if url.nil?   
    output! "Publish first."
    return [true, @out]
  end
  result = system("open '#{url}'")
  raise CantOpen(url) unless result
  nil
end

#cmd_change_view(arg) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/repl.rb', line 122

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

#cmd_clear(arg) ⇒ Object



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

def cmd_clear(arg)
  check_empty(arg)
  STDSCR.cwin.clear
  STDSCR.cwin.refresh
end

#cmd_config(arg) ⇒ Object



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

def cmd_config(arg)
  check_empty(arg)
  dir = @blog.view.dir
  items = ["publish", 
           "custom/blog_header.html", 
           "custom/blog_trailer.html", 
           "custom/post_template.html"] 
  num, fname = STDSCR.menu(title: "Edit file:", items: items)
  edit_file("#{dir}/#{fname}")
end

#cmd_edit_post(arg) ⇒ Object

– FIXME affects linking, building, publishing…



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

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

  file = files.first
  result = edit_file("#{@blog.root}/src/#{file}")
  @blog.rebuild_post(file)
  nil
end

#cmd_help(arg) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/repl.rb', line 269

def cmd_help(arg)
  reset_output 
  check_empty(arg)
  output(<<-EOS)
Commands:

     #{red('h, help       ')}    This message
     #{red('q, quit        ')}   Exit the program
     #{red('v, version    ')}    Print version information

     #{red('change view VIEW ')} Change current view
     #{red('cv VIEW          ')} Change current view

     #{red('new view         ')} Create a new view

     #{red('list views       ')} List all views available
     #{red('lsv              ')} Same as: list views

     #{red('p, post          ')} Create a new post
     #{red('new post         ')} Same as post (create a post)

     #{red('lsp, list posts  ')} List posts in current view

     #{red('lsd, list drafts ')} List all posts regardless of view

     #{red('rm ID            ')} Remove a post
     #{red('kill ID ID ID... ')} Remove multiple posts
     #{red('undelete ID      ')} Undelete a post
     #{red('edit ID          ')} Edit a post

     #{red('preview          ')} Look at current (local) view in browser
     #{red('browse           ')} Look at current (published) view in browser
     #{red('relink           ')} Regenerate index for all views
     #{red('rebuild          ')} Regenerate all posts and relink
     #{red('publish          ')} Publish (current view)
  EOS
  return [true, @out]
end

#cmd_INVALID(arg) ⇒ Object



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

def cmd_INVALID(arg)
  reset_output "\n  Command '#{red(arg)}' was not understood."
  return [true, @out]
end

#cmd_kill(arg) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/repl.rb', line 162

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

#cmd_list_drafts(arg) ⇒ Object



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

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

#cmd_list_posts(arg) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/repl.rb', line 217

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

#cmd_list_views(arg) ⇒ Object



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

def cmd_list_views(arg)
  reset_output("\n")
  check_empty(arg)
  puts
  @blog.views.each do |v| 
    v = v.to_s
    v = fx(v, :bold) if v == @blog.view.name
    print "  "
    puts v
  end
  puts
  return [false, @out]
end

#cmd_new_post(arg) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/repl.rb', line 151

def cmd_new_post(arg)
  reset_output
  check_empty(arg)
  title = ask("\nTitle: ")
  meta = OpenStruct.new
  meta.title = title
  @blog.create_new_post(meta)
  STDSCR.clear
  nil
end

#cmd_new_view(arg) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/repl.rb', line 142

def cmd_new_view(arg)
  reset_output
  @blog.create_view(arg)
  resp = yesno("Add publishing info now? ")
  @blog.view.publisher = ask_publishing_info
  write_config(@blog.view.publisher,  @blog.view.dir + "/publish")  # change this?
  nil
end

#cmd_preview(arg) ⇒ Object



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

def cmd_preview(arg)
  reset_output
  check_empty(arg)
  local = @blog.view.index
  result = system("open #{local}")
  raise CantOpen(local) unless result
end

#cmd_publish(arg) ⇒ Object

FIXME non-string return expected in caller?



71
72
73
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
101
102
103
# File 'lib/repl.rb', line 71

def cmd_publish(arg)  # FIXME non-string return expected in caller?
  puts
  reset_output
  check_empty(arg)
  unless @blog.view.can_publish?
    puts "Can't publish without entries in #{@blog.view.name}/publish"
    output! "Can't publish without entries in #{@blog.view.name}/publish"
    return [false, @out]
  end
  RubyText.spinner { @blog.view.publish }
#     user, server, sroot, spath = *@publish[@blog.view]
#     if files.empty?    # FIXME  baloney
#       output! "No files to publish"
#       return [true, @out]
#     end

#     output "Files:"
#     files.each {|f| output "    #{f}\n" }
#     output_newline
#     dir = "#{sroot}/#{spath}"
#     # FIXME - may or may not already exist
#     result = system("ssh root@#{server} mkdir -p #{dir}") 
# 
#     cmd = "scp -r #{files.join(' ')} root@#{server}:#{dir} >/dev/null 2>&1"
#     output! "Publishing #{files.size} files...\n"
#     result = system(cmd)
#     raise PublishError unless result

  dump(files, "#{vdir}/last_published")
  puts "  ...finished" 
  output! "...finished.\n"
  return [false, @out]
end

#cmd_quit(arg) ⇒ Object



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

def cmd_quit(arg)
  check_empty(arg)
#   system("tput rmcup")
  RubyText.stop
  system("tput clear")
  exit
end

#cmd_rebuild(arg) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/repl.rb', line 105

def cmd_rebuild(arg)
  debug "Starting cmd_rebuild..."
  reset_output
  check_empty(arg)
  puts  # CHANGE_FOR_CURSES?
  files = @blog.find_src_slugs
  files.each {|file| @blog.rebuild_post(file) }
  nil
end


115
116
117
118
119
120
# File 'lib/repl.rb', line 115

def cmd_relink(arg)
  reset_output
  check_empty(arg)
  @blog.relink
  nil
end

#cmd_remove_post(arg, safe = true) ⇒ Object

– FIXME affects linking, building, publishing…



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

def cmd_remove_post(arg, safe=true)
  # FIXME - 'safe' is no longer a thing
  reset_output
  id = get_integer(arg)
  result = @blog.remove_post(id)
  output! "Post #{id} not found" if result.nil?
  return [true, @out]
end

#cmd_version(arg) ⇒ Object



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

def cmd_version(arg)
  reset_output
  check_empty(arg)
  output RuneBlog::VERSION
  [true, @out]
end

#colored_slug(slug) ⇒ Object



215
216
217
# File 'lib/helpers-repl.rb', line 215

def colored_slug(slug)
  red(slug[0..3])+blue(slug[4..-1])  # CHANGE_FOR_CURSES?
end

#dumb_menu(array) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/helpers-repl.rb', line 252

def dumb_menu(array)
  # { string => :meth, ... }
  max = array.size
  puts "\n  Select from:"  # CHANGE_FOR_CURSES?
  array.each.with_index do |string, i|
    puts "   #{red('%2d' % (i+1))} #{string}"  # CHANGE_FOR_CURSES?
  end
  picked = nil
  loop do
    print red("> ")  # CHANGE_FOR_CURSES?
    num = gets.to_i
    if num.between?(1, max)
      picked = array[num-1]
      break
    else
      puts "Huh? Must be 1 to #{max}"  # CHANGE_FOR_CURSES?
    end
  end
  picked
end

#edit_file(file) ⇒ Object



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

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

#error(err) ⇒ Object



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

def error(err)
  str = "\n  Error: #{red(err)}"
  puts str  # CHANGE_FOR_CURSES?
  puts err.backtrace[0]  # CHANGE_FOR_CURSES?
end

#error_cant_delete(files) ⇒ Object



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

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



292
293
294
295
# File 'lib/helpers-repl.rb', line 292

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?



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/helpers-repl.rb', line 277

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
  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



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

def flush_output(initial = "")
  @out ||= ""
  puts @out  # CHANGE_FOR_CURSES?
  reset_output
end

#get_integer(arg) ⇒ Object



196
197
198
199
200
# File 'lib/helpers-repl.rb', line 196

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

#import(arg = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/helpers-repl.rb', line 219

def import(arg = nil)
#   open_blog unless @blog
  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)
  @fname = @slug + ".lt3"
  result = system("cp #{name} #@root/src/#@fname")
  raise CantCopy(name, "#@root/src/#@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



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

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

#output!(str) ⇒ Object

red, n and indent



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

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

#output_newline(n = 1) ⇒ Object



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

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

#outstr(str) ⇒ Object

indent



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

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

#reset_output(initial = "") ⇒ Object



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

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

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



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

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