require 'runeblog' require 'ostruct' require 'helpers-repl' # FIXME structure make_exception(:PublishError, "Error during publishing") # make_exception(:EditorProblem, "Could not edit $1") module RuneBlog::REPL def edit_file(file) result = system("#{@blog.editor} #{file}") raise EditorProblem(file) unless result sleep 0.1 STDSCR.clear end def cmd_quit(arg, testing = false) check_empty(arg) # system("tput rmcup") RubyText.stop system("tput clear") exit end def cmd_clear(arg, testing = false) check_empty(arg) STDSCR.cwin.clear STDSCR.cwin.refresh end def cmd_version(arg, testing = false) reset_output check_empty(arg) output RuneBlog::VERSION puts RuneBlog::VERSION unless testing [false, @out] end def cmd_config(arg, testing = false) 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 def cmd_browse(arg, testing = false) reset_output check_empty(arg) url = @blog.view.publisher.url # FIXME Bad logic here. if url.nil? output! "Publish first." puts "\n Publish first." return [false, @out] end result = system("open '#{url}'") raise CantOpen(url) unless result return [false, @out] end def cmd_preview(arg, testing = false) reset_output check_empty(arg) local = @blog.view.index result = system("open #{local}") raise CantOpen(local) unless result end def cmd_publish(arg, testing = false) # FIXME non-string return expected in caller? 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 [false, @out] end ret = RubyText.spinner(label: "Publishing... ") { @blog.view.publish } return [false, @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 [false, @out] end def cmd_rebuild(arg, testing = false) debug "Starting cmd_rebuild..." reset_output check_empty(arg) puts unless testing files = @blog.find_src_slugs files.each {|file| @blog.rebuild_post(file) } nil end def cmd_relink(arg, testing = false) reset_output check_empty(arg) @blog.relink nil end 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 [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 def cmd_new_view(arg, testing = false) 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 def cmd_new_post(arg, testing = false) reset_output check_empty(arg) title = ask("\nTitle: ") @blog.create_new_post(title) STDSCR.clear nil rescue => err puts err puts err.backtrace.join("\n") end 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 return [false, @out] end #-- FIXME affects linking, building, publishing... def cmd_remove_post(arg, testing = false, 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? # puts "Post #{id} not found" if result.nil? return [false, @out] end #-- FIXME affects linking, building, publishing... def cmd_edit_post(arg, testing = false) 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) } 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 result = edit_file("#{@blog.root}/src/#{file}") @blog.rebuild_post(file) return [false, @out] end 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 return [false, @out] end 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! bold("No posts") puts fx(" No posts", :bold) 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 return [false, @out] end def cmd_list_drafts(arg, testing = false) reset_output check_empty(arg) drafts = @blog.drafts # current view if drafts.empty? output! "No drafts" puts " No drafts" unless testing return [false, @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 return [false, @out] end def cmd_INVALID(arg, testing = false) reset_output "\n Command '#{red(arg)}' was not understood." puts "\n Command ", fx(arg, Red), " was not understood." return [false, @out] end 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 p, post Create a new post new post Same as post (create a post) 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) EOS output msg puts msg unless testing return [false, @out] end end