#!/usr/bin/env ruby # $LOAD_PATH << "./lib" require 'runeblog' require 'rubytext' require 'repl' include RuneBlog::REPL def yesno(question, noskip=false) puts fx("\n #{question}", :bold) puts unless noskip STDSCR.yesno end def pick_editor choices = %w[vim emacs vi nano] r, c = STDSCR.rc num, name = STDSCR.menu(r: r, c: c+6, title: "Default editor", items: choices) file = `which #{name}`.chomp end def get_universal univ = "#{@blog.root}/data/universal.lt3" if yesno("Faster initial setup? (no: edit universal.lt3)") author = ask("Author name: ") site = ask("Site/domain: ") # Now stash it... str = File.read(univ) str = str.gsub(/AUTHOR/, author) str = str.gsub(/SITE_DOMAIN/, site) else vim_params = '-c ":set hlsearch" -c ":hi Search ctermfg=2 ctermbg=6" +/"\(AUTHOR.*\|SITE.*\)"' edit_file(univ, vim: vim_params) end end def get_global if yesno("Faster view setup? (no: edit global.lt3)") view_name = ask("\nFilename: ") @blog.create_view(view_name) # call change_view?? title = ask("View title: ") subtitle = ask("Subtitle : ") gname = "#{@blog.root}/views/#{view_name}/themes/standard/global.lt3" text = File.read(gname) text = text.gsub(/VIEW_NAME/, view_name) text = text.gsub(/VIEW_TITLE/, title) text = text.gsub(/VIEW_SUBTITLE/, subtitle) File.write(gname, text) else view_name = ask("\nFilename: ") @blog.create_view(view_name) # call change_view?? vim_params = '-c ":set hlsearch" -c ":hi Search ctermfg=2 ctermbg=6" +/"\(VIEW_.*\|SITE.*\)"' edit_file(@blog.view.dir/"themes/standard/global.lt3", vim: vim_params) end end def get_started if yesno("Do you want to qo a quick setup?") puts " First choose your editor." @blog.editor = pick_editor File.write("#{@blog.root}/data/EDITOR", @blog.editor) get_universal # Now create a custom global.lt3 @blog._generate_global puts fx("\n Quick setup complete!", :bold) if yesno("Create your first view now?") get_global puts "\n View #{@blog.view} created!\n " end end puts print fx(" For help", :bold) puts " type h or help." print fx(" Create a view", :bold) puts " with: new view" print fx(" Create a post", :bold) puts " (within current view): new post" end def mainloop info = @blog.view || "no view" print fx("[#{info}] ", Red, :bold) cmd = STDSCR.gets(history: @cmdhist, tab: @tabcom) cmd_quit(nil) if cmd.nil? # ^D cmd.chomp! return if cmd.empty? # CR does nothing meth, params = RuneBlog::REPL.choose_method(cmd) ret, str = send(meth, params) rescue => err puts err end def cmdline_preview _need_view local = @blog.view.local_index result = system("open #{local}") end def cmdline_publish abort "Nor implemented yet" _need_view end def cmdline_browse abort "Nor implemented yet" _need_view end def _need_view @view = ARGV[1] abort "Need 'view' parameter" if @view.nil? abort "No such view '#{view}'" unless @blog.view?(@view) end def cmdline_rebuild _need_view print "Generating view... " @blog.generate_view(@view) print "Generating index... " num = @blog.generate_index(@view) puts "#{num} posts\n " end def handle_cmdline cmd = ARGV[0] @blog = RuneBlog.new abort "No blog found" if @blog.nil? case cmd when "rebuild"; cmdline_rebuild when "publish"; cmdline_publish when "preview"; cmdline_preview when "browse"; cmdline_browse else puts "Command '#{cmd}' is unknown" end exit end ### Main major, minor = RUBY_VERSION.split(".").values_at(0,1) ver = major.to_i*10 + minor.to_i unless ver >= 24 RubyText.stop abort "Need Ruby 2.4 or greater" end include RuneBlog::Helpers # for try_read_config handle_cmdline unless ARGV.empty? errfile = File.new("stderr.out", "w") STDERR.reopen(errfile) # read a .rubytext file here?? Call it something else? home = ENV['HOME'] @fg, @bg = Blue, White ## FIXME!! try_read_config("#{home}/.rubytext", fg: Blue, bg: White) @fg = @fg.downcase.to_sym @bg = @bg.downcase.to_sym RubyText.start(:_echo, :keypad, scroll: true, log: "binblog.txt", fg: @fg, bg: @bg) new_repo = false if ! RuneBlog.exist? exit unless yesno("No blog repo found. Create new one?") RuneBlog.create_new_blog_repo puts fx(" Blog repo successfully created.", :bold) new_repo = true end @blog = RuneBlog.new get_started if new_repo print fx(" For help", :bold) puts " type h or help.\n " puts fx("\n RuneBlog", :bold), fx(" v #{RuneBlog::VERSION}\n", Red) @cmdhist = [] @tabcom = RuneBlog::REPL::Patterns.keys.uniq - RuneBlog::REPL::Abbr.keys @tabcom.map! {|x| x.sub(/ [\$\>].*/, "") + " " } @tabcom.sort! loop { mainloop } system("tput clear") sleep 0.2 puts