commit 495ecf264056bf686418f4aa512cb60692301d3a
parent 9712969be799967ddd4b2cc9475158d1268bb757
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Thu, 10 Sep 2020 00:35:09 -0700
Rewrite feed
a seperate awk script now handles formatting the sfeed text.
much fewer and simpler arguments.
more readable shell script.
Diffstat:
2 files changed, 75 insertions(+), 18 deletions(-)
diff --git a/scripts/.local/bin/feed b/scripts/.local/bin/feed
@@ -1,27 +1,50 @@
#!/usr/bin/env dash
-# manage viewing text/html rss feeds formatted by sfeed(1) in RSSTEXT
-# directory.
+# manage searching and viewing sfeed formatted files in $RSSTEXT directory.
-set -e
+set -eu
-# this function is getting out of hand, going to rewrite this program in C soon
-# as I find I like it.
-ffmt()
+# take sfeed formatted text returning html or plain-text.
+format()
{
- awk -F'\n' '{ gsub("\t", "\n", $0) \
- gsub(/\\t/, "\t", $0); gsub(/\\n/, " ", $0); \
- printf "<h1>%s</h1><article>%s</article>\n", $1, $2 }'
+ awk -f `which sfeed.awk` $@
}
-set -e
+# return feeds in 'filename index: title` format
+show()
+{
+ awk -F"\t" '{ printf "%s %d: %s\n", FILENAME, FNR, $2 }' $@
+}
+
+# return feed filenames unless filename(s) specified in which case call show
+list()
+{
+ if [ $# -eq 0 ]; then
+ ls -1
+ else
+ show $@
+ fi
+}
+
+# returns the nth feed in all feeds or specified feeds
+nth()
+{
+ local n=$1
+ shift
+ for feed in ${@:-`ls -1`}
+ do
+ head -$n $feed | tail -n1
+ done
+}
+
+if [ $# -gt 0 ]; then
+ cmd=$1
+ shift
+fi
-case $1
+cd $RSSTEXT
+case "${cmd:=}"
in
- -h) echo "usage: `basename $0` -h | -l | -d | -n index | -t regex | regex | [file]";;
- -l) ls -1 $RSSTEXT;;
- -d) cut -f2 $RSSTEXT/${2:-*};;
- -n) cat $RSSTEXT/${3:-*} | head -n "$2" | tail -1 | cut -f2,4 | ffmt;;
- -t) awk -vOFS="\t" -vFS="\t" "match(tolower(\$2), \"$2\") > 0 \
- { print \$2, \$4 }" $RSSTEXT/${3:-*} | ffmt;;
- *) grep -i "$1" $RSSTEXT/${2:-*} | cut -f2,4 | ffmt;;
+ -l) list $@;;
+ -n) nth $@ | format;;
+ *) egrep -Ri "$cmd" $@ | format;;
esac
diff --git a/scripts/.local/bin/sfeed.awk b/scripts/.local/bin/sfeed.awk
@@ -0,0 +1,34 @@
+#!/usr/bin/awk -f
+# display sfeed(1) formatted text as either html or plain-text.
+
+BEGIN {
+ FS = "\t"
+}
+
+function basename(path) {
+ n = split(path, a, "/");
+ return a[n];
+}
+
+function plain(title, author, content) {
+ gsub(/\\n/, "\n", content);
+ gsub(/\\t/, " ", content);
+ gsub("\n", "\n\t", content);
+ printf "%s\nby %s\n\n\t%s\n", title, author, content
+}
+
+function html(title, link, author, content) {
+ gsub(/\\[nt]/, " ", content);
+ printf "<article><head><h1><a href=%s>%s</a></h1><h2>by %s</h2></head>%s</article>\n", link, title, author, content
+}
+
+{
+ # TODO: calculate filename only once when it's opened.
+ if ($7 == "") { author = basename(FILENAME); } else { author = $7; }
+ if ($5 == "plain") {
+ plain($2, author, $4);
+ print "\n";
+ } else {
+ html($2, $3, author, $4);
+ }
+}