diff options
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/aaoth_new_post.sh | 106 | ||||
| -rwxr-xr-x | bin/gen.sh | 29 | ||||
| -rwxr-xr-x | bin/rssg | 190 | ||||
| -rwxr-xr-x | bin/ssg | 273 | ||||
| -rwxr-xr-x | bin/thumbs.sh | 60 |
5 files changed, 658 insertions, 0 deletions
diff --git a/bin/aaoth_new_post.sh b/bin/aaoth_new_post.sh new file mode 100755 index 0000000..8c2139f --- /dev/null +++ b/bin/aaoth_new_post.sh @@ -0,0 +1,106 @@ +#!/bin/sh + +# quick and dirty script to add new posts to aaoth.xyz + +SITE_DIR="$HOME/Documents/aaoth.xyz" +POSTS_DIR="$SITE_DIR/_posts" + +DATE_SHORT=$(date -I) +DATE_LONG=$(date -Isec) + +usage() { + echo "add a post to aaoth.xyz" + echo + echo "usage:" + echo " $0 [OPTIONS]" + echo + echo "options:" + echo " -t, --title <title>" + echo " specify new post title" + echo " -g, --tags <tags>" + echo " specify new post tags" + echo " -h, --help" + echo " print usage information" +} + +read_title() { + echo "new post title: " + read -r TITLE +} + +read_tags() { + echo "new post tags: " + read -r TAGS +} + +create_tag_page() { + cat <<- TAG > "$SITE_DIR/tags/$1.md" +--- +layout: tagsort +tag: $1 +title: "tags: $1" +permalink: /tags/$1/ +--- +TAG +} + +cd "$SITE_DIR" || exit 1 + +# if there are no arguments specified, run interactively +if [ $# -gt 0 ] +then + while [ -n "$1" ] + do + case "$1" in + --title|-t) + shift + TITLE=$1 + ;; + --tags|-g) + shift + TAGS=$1 + ;; + --help|-h) + usage + exit 2 + ;; + *) + usage + exit 1 + ;; + esac + shift + done +fi + +[ -z "$TITLE" ] && read_title +[ -z "$TITLE" ] && echo "title could not be empty" && exit 1 + +[ -z "$TAGS" ] && read_tags +[ -z "$TAGS" ] && echo "specify at least one tag" && exit 1 + +TITLE_FILE=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g') + +POST_FILENAME="$POSTS_DIR/$DATE_SHORT-$TITLE_FILE.md" + +for _tag in $TAGS +do + [ ! -f "./tags/$_tag.md" ] \ + && echo "tag $_tag is not present, creating one" \ + && create_tag_page "$_tag" +done + +# template is currently hardcoded +cat <<-EOF > "$POST_FILENAME" +--- +title: $TITLE +date: $DATE_LONG +author: la-ninpre +tags: $TAGS +--- + +<!--more--> + +EOF + +nvim -c "normal 6jo" -c "startinsert" "$POST_FILENAME" diff --git a/bin/gen.sh b/bin/gen.sh new file mode 100755 index 0000000..99aab55 --- /dev/null +++ b/bin/gen.sh @@ -0,0 +1,29 @@ +#!/bin/sh -ex + +srcdir="./en/visual" +categories="drawings photos logos renders" +index="index.md" + +list_images() { + # $1 -- directory + find "$1" -type f \ + | grep -E "(\.jpe?g)|(\.png)|(\.gif)" +} + +append_image() { + # $1 -- directory + c_index="$1/$index" + while read -r i; do + i=$(basename "$i") + if ! (grep "$i" "$c_index"); then + printf "\n" >> "$c_index" + echo "" >> "$c_index" + fi + done +} + +cd "$srcdir" || exit 1 +for c in $categories; +do + list_images "$c" | append_image "$c" +done diff --git a/bin/rssg b/bin/rssg new file mode 100755 index 0000000..099fe67 --- /dev/null +++ b/bin/rssg @@ -0,0 +1,190 @@ +#!/bin/sh +# +# https://www.romanzolotarev.com/bin/rssg +# Copyright 2018 Roman Zolotarev <hi@romanzolotarev.com> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# +set -e +set -x + + +main () { + test -n "$1" || usage + test -n "$2" || usage + test -f "$1" || no_file "$1" + + + index_file=$(readlink -f "$1") + test -z "${index_file##*html}" && html=$(cat "$index_file") + test -z "${index_file##*md}" && html=$(md_to_html "$index_file") + test -n "$html" || usage + + base="${index_file%/*}" + base_url="$(echo "$html" | get_url | sed 's#\(.*\)/[^/]*#\1#')" + + url=$( echo "$html" | get_url) + + title="$2" + + description=$( echo "$html" | get_description | + remove_tags | + remove_nbsp ) + + items=$( echo "$html" | get_items) + + rss=$( echo "$items" | + render_items "$base" "$base_url" | + render_feed "$url" "$title" "$description") + + >&2 echo "[rssg] ${index_file##$(pwd)/} $(echo "$rss" | grep -c '<item>') items" + echo "$rss" +} + + +usage() { + echo "usage: ${0##*/} index.{html,md} title > rss.xml" >&2 + exit 1 +} + + +no_file() { + echo "${0##*/}: $1: No such file" >&2 + exit 2 +} + + +md_to_html() { + test -x "$(which lowdown)" || exit 3 + lowdown \ + --html-no-escapehtml \ + --html-no-skiphtml \ + --parse-no-metadata \ + --parse-no-autolink "$1" +} + + +get_title() { + awk 'tolower($0)~/^<h1/{gsub(/<[^>]*>/,"",$0);print;exit}' +} + + +get_url() { + grep -i '<a .*rss.xml"' | head -1 | + sed 's#.*href="\(.*\)".*#\1#' +} + + +get_items() { + grep -i 'href=".*" title="' | + sed 's#.*href="\(.*\)" title="\(.*\)">\(.*\)</a>.*#\1 \2 \3#' +} + + +get_description() { + start='sub("^.*<"s"*"t"("s"[^>]*)?>","")' + stop='sub("</"s"*"t""s"*>.*","")&&x=1' + awk -v 's=[[:space:]]' -v 't=[Pp]' "$start,$stop;x{exit}" +} + +remove_tags() { + sed 's#<[^>]*>##g;s#</[^>]*>##g' +} + + +remove_nbsp() { + sed 's#\ # #g' +} + + +rel_to_abs_urls() { + site_url="$1" + base_url="$2" + + abs='s#(src|href)="/([^"]*)"#\1="'"$site_url"/'\2"#g' + rel='s#(src|href)="([^:/"]*)"#\1="'"$base_url"/'\2"#g' + sed -E "$abs;$rel" +} + + +date_rfc_822() { + date '+%a, %d %b %Y %H:%M:%S %z' \ + "$(echo "$1"| tr -cd '[:digit:]')0000" +} + + +render_items() { + while read -r i + do render_item "$1" "$2" "$i" + done +} + + +render_item() { + base="$1" + base_url="$2" + item="$3" + + site_url="$(echo "$base_url"| sed 's#\(.*//.*\)/.*#\1#')" + + date=$(echo "$item"|awk '{print$2}') + url=$(echo "$item"|awk '{print$1}') + + f="$base/$url" + test -f "$f" && html=$(cat "$f") + test -f "${f%\.html}.md" && html=$(md_to_html "${f%\.html}.md") + + description=$( + echo "$html" | + rel_to_abs_urls "$site_url" "$base_url" | + remove_nbsp + ) + title=$(echo "$description" | get_title) + guid="$base_url/$(echo "$url" | sed 's#^/##')" + + echo ' +<item> +<guid>'"$guid"'</guid> +<link>'"$guid"'</link> +<pubDate>'"$(date_rfc_822 "$date")"'</pubDate> +<title>'"$title"'</title> +<description><![CDATA[ + +'"$description"' + +]]></description> +</item>' +} + + +render_feed() { + url="$1" + title=$(echo "$2" | remove_nbsp) + description="$3" + + base_url="$(echo "$url" | cut -d '/' -f1-3)" + + echo '<?xml version="1.0" encoding="UTF-8"?> +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> +<channel> +<atom:link href="'"$url"'" rel="self" type="application/rss+xml" /> +<title>'"$title"'</title> +<description>'"$description"'</description> +<link>'"$base_url"'/</link> +<lastBuildDate>'"$(date_rfc_822 date)"'</lastBuildDate> +'"$(cat)"' +</channel></rss>' +} + + +main "$@" @@ -0,0 +1,273 @@ +#!/bin/sh -e +# +# https://rgz.ee/bin/ssg6 +# Copyright 2018-2019 Roman Zolotarev <hi@romanzolotarev.com> +# Copyright 2022 la-ninpre <aaoth@aaoth.xyz> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +main() { + test -n "$1" || usage + test -n "$2" || usage + test -n "$3" || usage + test -n "$4" || usage + test -n "$5" || usage + test -d "$1" || no_dir "$1" + test -d "$2" || no_dir "$2" + test -d "$3" || no_dir "$3" + + src=$(readlink_f "$1") + dst=$(readlink_f "$2") + gemdst=$(readlink_f "$3") + + IGNORE=$( + if ! test -f "$src/.ssgignore"; then + printf ' ! -path "*/.*"' + return + fi + while read -r x; do + test -n "$x" || continue + printf ' ! -path "*/%s*"' "$x" + done <"$src/.ssgignore" + ) + + # files + + title="$4" + + h_file="$src/_header.html" + f_file="$src/_footer.html" + gf_file="$src/_gemfooter.gmi" + test -f "$f_file" && FOOTER=$(cat "$f_file") && export FOOTER + test -f "$h_file" && HEADER=$(cat "$h_file") && export HEADER + test -f "$gf_file" && GEMFOOTER=$(cat "$gf_file") && export GEMFOOTER + + list_dirs "$src" | + (cd "$src" && cpio -pdu "$dst") + list_dirs "$src" | + (cd "$src" && cpio -pdu "$gemdst") + + fs=$( + if test -f "$dst/.files"; then + list_affected_files "$src" "$dst/.files" + if test -f "$gemdst/.files"; then + list_affected_files "$src" "$gemdst/.files" + fi + else + list_files "$1" + fi + ) + + if test -n "$fs"; then + echo "$fs" | tee "$dst/.files" + echo "$fs" | tee "$gemdst/.files" + + if echo "$fs" | grep -q '\.md$'; then + if test -x "$(which lowdown 2>/dev/null)"; then + echo "$fs" | grep '\.md$' | + render_md_files_lowdown "$src" "$dst" "$title" + echo "$fs" | grep '\.md$' | + render_md_files_lowdown_gemini "$src" "$gemdst" + + else + echo "couldn't find lowdown nor Markdown.pl" + exit 3 + fi + fi + + echo "$fs" | grep '\.html$' | + render_html_files "$src" "$dst" "$title" + + echo "$fs" | grep -Ev '\.md$|\.html$' | + (cd "$src" && cpio -pu "$dst") + echo "$fs" | grep -Ev '\.md$|\.gmi$' | + (cd "$src" && cpio -pu "$gemdst") + fi + + printf '[ssg] ' >&2 + print_status 'file, ' 'files, ' "$fs" >&2 + + # sitemap + + base_url="$5" + date=$(date +%Y-%m-%d) + urls=$(list_pages "$src") + + test -n "$urls" && + render_sitemap "$urls" "$base_url" "$date" >"$dst/sitemap.xml" + + print_status 'url' 'urls' "$urls" >&2 + echo >&2 +} + +readlink_f() { + file="$1" + cd "$(dirname "$file")" + file=$(basename "$file") + while test -L "$file"; do + file=$(readlink "$file") + cd "$(dirname "$file")" + file=$(basename "$file") + done + dir=$(pwd -P) + echo "$dir/$file" +} + +print_status() { + test -z "$3" && printf 'no %s' "$2" && return + + echo "$3" | awk -v singular="$1" -v plural="$2" ' + END { + if (NR==1) printf NR " " singular + if (NR>1) printf NR " " plural + }' +} + +usage() { + echo "usage: ${0##*/} src dst gemdst title base_url" >&2 + exit 1 +} + +no_dir() { + echo "${0##*/}: $1: No such directory" >&2 + exit 2 +} + +list_dirs() { + cd "$1" && eval "find . -type d ! -name '.' ! -path '*/_*' $IGNORE" +} + +list_files() { + cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE" +} + +list_dependant_files() { + e="\\( -name '*.html' -o -name '*.md' -o -name '*.css' -o -name '*.js' \\)" + cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE $e" +} + +list_newer_files() { + cd "$1" && eval "find . -type f ! -name '.' $IGNORE -newer $2" +} + +has_partials() { + grep -qE '^./_.*\.html$|^./_.*\.js$|^./_.*\.css$' +} + +list_affected_files() { + fs=$(list_newer_files "$1" "$2") + + if echo "$fs" | has_partials; then + list_dependant_files "$1" + else + echo "$fs" + fi +} + +render_html_files() { + while read -r f; do + render_html_file "$3" <"$1/$f" >"$2/$f" + done +} + +render_md_files_lowdown() { + while read -r f; do + lowdown \ + --html-no-escapehtml \ + --html-no-skiphtml \ + --parse-no-metadata \ + --parse-no-autolink <"$1/$f" | + render_html_file "$3" \ + >"$2/${f%\.md}.html" + done +} + +render_md_files_lowdown_gemini() { + while read -r f; do + lowdown \ + -Tgemini <"$1/$f" | + render_gmi_file \ + >"$2/${f%\.md}.gmi" + done +} + +render_html_file() { + # h/t Devin Teske + awk -v title="$1" ' + { body = body "\n" $0 } + END { + body = substr(body, 2) + if (body ~ /<\/?[Hh][Tt][Mm][Ll]/) { + print body + exit + } + if (match(body, /<[[:space:]]*[Hh]1(>|[[:space:]][^>]*>)/)) { + t = substr(body, RSTART + RLENGTH) + sub("<[[:space:]]*/[[:space:]]*[Hh]1.*", "", t) + gsub(/^[[:space:]]*|[[:space:]]$/, "", t) + if (t) title = t " — " title + } + n = split(ENVIRON["HEADER"], header, /\n/) + for (i = 1; i <= n; i++) { + if (match(tolower(header[i]), "<title></title>")) { + head = substr(header[i], 1, RSTART - 1) + tail = substr(header[i], RSTART + RLENGTH) + print head "<title>" title "</title>" tail + } else print header[i] + } + print body + print ENVIRON["FOOTER"] + }' +} + +render_gmi_file() { + awk ' + { body = body "\n" $0 } + END { + body = substr(body, 2) + n = split(body, body_n, /\n/) + for (i = 1; i <= n; i++) { + if (!match(body_n[i], /^=>[[:space:]]*[Hh][Tt]{2}[Pp][Ss]?:\/\/.*/)) { + sub(/\.html[[:space:]]*/, ".gmi ", body_n[i]) + } + print body_n[i] + } + #print body + print ENVIRON["GEMFOOTER"] + }' +} + +list_pages() { + e="\\( -name '*.html' -o -name '*.md' \\)" + cd "$1" && eval "find . -type f ! -path '*/.*' ! -path '*/_*' $IGNORE $e" | + sed 's#^./##;s#.md$#.html#;s#/index.html$#/#' +} + +render_sitemap() { + urls="$1" + base_url="$2" + date="$3" + + echo '<?xml version="1.0" encoding="UTF-8"?>' + echo '<urlset' + echo 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + echo 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' + echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' + echo 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' + echo "$urls" | + sed -E 's#^(.*)$#<url><loc>'"$base_url"'/\1</loc><lastmod>'"$date"'</lastmod><priority>1.0</priority></url>#' + echo '</urlset>' +} + +main "$@" diff --git a/bin/thumbs.sh b/bin/thumbs.sh new file mode 100755 index 0000000..3c1a633 --- /dev/null +++ b/bin/thumbs.sh @@ -0,0 +1,60 @@ +#!/bin/sh -ex + +_img_dirs="$( find . -maxdepth 1 -type d | sed -e '1d' -e 's/^\.\///' )" +_thumb_size="835x" +_thumbs_dir="thumbs" +_force_render=0 + +usage() { + echo "usage: $0 [-f|--force]" +} + +[ -n "$1" ] && \ + case $1 in + -f|--force) + _force_render=1 + ;; + -h|--help) + usage + exit 0 + ;; + *) + usage + exit 1 + ;; + esac + +for dir in $_img_dirs +do + cd "./$dir" || exit 1 + + mkdir -p "$_thumbs_dir" + + _imgs="$(find . -maxdepth 1 \ + -iname "*.jpg" -or \ + -iname "*.jpeg" -or \ + -iname "*.gif" -or \ + -iname "*.png" -type f | cut -b 3-)" + for _img in $_imgs + do + _ext="${_img##*.}" + _name="${_img%%.*}" + _thumb="./$_thumbs_dir/${_name}_thumb.${_ext}" + + [ "$_force_render" -eq 1 ] || [ ! -f "$_thumb" ] && \ + { + # when compressed, gifs look ugly, so just copy them + # i know this is bad, but i'll adress it later + [ "$_ext" = "gif" ] && \ + echo "copied $1 to $_thumb" && \ + cp "$_img" "$_thumb" && continue + + echo "creating thumbnail for $_name..." + convert "$_img" -resize "$_thumb_size" "$_thumb" + } + done + + cd ".." + +done + |
