From d11f14949125a5595f70df7bdce973ba68e5ab76 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sat, 24 Jul 2021 01:01:19 +0200 Subject: Add the think script --- README.md | 36 +++++++++++++++++++++++++++++++++++- sunday.rb | 40 ++++++++++++++++++++++++++++++++++++++++ think.rb | 24 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100755 sunday.rb create mode 100755 think.rb diff --git a/README.md b/README.md index f030706..a26c404 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,22 @@ do it in every configuration, but here's how I did it on my Nginx setup: Make sure sunday.rb is executable by your webserver process (e.g. by running `chmod a+x sunday.rb`). +## Configuration + +sunday.rb has a few configurable variables. + +`THOUGHTS_DIR` should point to the directory where you will submit your +thoughts. More details about it under "Adding thoughts" below. Defaults to +`/srv/thoughts`, make sure to create this directory if you keep the default. + +Change `heading` to contain a personalized heading for your Sunday Corner. + +You can also change the `no_thoughts` message which will appear on a Sunday if +you hadn't added any thoughts that week. + +And you can also change the `not_sunday` message which will inform your +impatient visitors to wait until next Sunday for the next batch of thoughts. + ## Thinking thoughts This one is hard. But not as hard as you might think! @@ -30,4 +46,22 @@ This one is easy! At it's simplest, just point `THOUGHTS_DIR` in sunday.rb to a directory where you will manually create thoughts files. A thoughts file's filename is the date of the Sunday on which it will be displayed (in YYYY-MM-DD format). Its contents -are your thoughts throughout the week, separated by newlines. +are your thoughts throughout the week. + +### Making it even easier + +The think.rb script makes adding thoughts even easier (doesn't help with the +thinking thoughts part, unfortunately). It: + +* Generates the correct filename for the thoughts file based on when the next + Sunday is. +* If it already exists, copies the remote thoughts file for next Sunday from + your remote. +* Drops you into an editor to edit the thoughts file. +* Once you're done editing, copies it back to your remote. + +For it to work properly, make sure `THOUGHTS_DIR` in think.rb points correctly +to your remote thoughts directory. + +The script requires `scp` as a dependency, but that usually comes with SSH, +which you probably already have. diff --git a/sunday.rb b/sunday.rb new file mode 100755 index 0000000..43cf824 --- /dev/null +++ b/sunday.rb @@ -0,0 +1,40 @@ +#!/bin/env ruby + +require "cgi" +require "date" + +THOUGHTS_DIR = "/srv/thoughts" + +heading = <<~END +================================================================================ +Welcome to Martin's Sunday Corner! Here you will find some of my random thoughts + from throughout the past week. Available only on Sundays. + + You can host your own Sunday Corner by copying my code from + >>> git.m-chrzan.xyz/sunday <<< +================================================================================ +END +no_thoughts = <<~END + No thoughts to share this week. Check back next Sunday! +END +not_sunday = <<~END + It's not Sunday today, the corner is closed. Check back on Sunday! +END + +cgi = CGI.new +today = Date::today +thoughts_file = "#{THOUGHTS_DIR}/#{today}" + +if Date::today.sunday? + if File.exist? thoughts_file + contents = File.read thoughts_file + else + contents = no_thoughts + end +else + contents = not_sunday +end + +cgi.out "type" => "text/plain", "charset" => "utf-8" do + heading + "\n" + contents +end diff --git a/think.rb b/think.rb new file mode 100755 index 0000000..63e258a --- /dev/null +++ b/think.rb @@ -0,0 +1,24 @@ +#!/bin/env ruby + +require "date" + +THOUGHTS_DIR = "vultr:/srv/thoughts" + +today = Date::today + +# .wday returns 0 for Sunday. +# If today is Sunday, we want to put today's thoughts in next Sunday's file so +# today's Sunday Corner doesn't change mid-day. +days_till_next_sunday = 7 - today.wday + +next_sunday = today + days_till_next_sunday + +remote_file = "#{THOUGHTS_DIR}/#{next_sunday}" +local_file = "/tmp/sunday-thoughts-#{next_sunday}" + +system "touch #{local_file}" +system "scp #{remote_file} #{local_file}" +system "$VISUAL #{local_file}" +# Note that if this scp fails and you rerun the script, the first scp above (if +# it succeeds) will overwrite your current local file. +system "scp #{local_file} #{remote_file}" -- cgit v1.2.3