noxz-sites

A collection of a builder and various scripts creating the noxz.tech sites
git clone https://noxz.tech/git/noxz-sites.git
noxz-sites

commit: 0a61268ccc8a088468e3f87f3429163bcca0e6bc
parent: f6971947dd1029810b6422a78f6b854660651f0e
author: Chris Noxz <chris@noxz.tech>
date:   Thu, 18 Aug 2022 14:51:14 +0200
implement tag system for articles
M.gitignore1+
Mbuild96+++++++++++++++-----
Mconfig/main.html6+-
Mconfig/www.conf5+
Mnoxz.tech/pub/style.css5+
5 files changed, 85 insertions(+), 28 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -3,6 +3,7 @@
 web.tar
 git.noxz.tech/*
 noxz.tech/git
+noxz.tech/articles/tags
 !git.noxz.tech/pub
 logo.png
 */pub/sitemap.xml
diff --git a/build b/build
@@ -233,17 +233,25 @@ render_main_menu_extra() {
 }
 
 render_articles() {
+	[ -z "$2" ] && printf 'You can also filter by\n.URL tags tags .\n\n'
 	echo .DIVS articles
-	find noxz.tech/articles -name .metadata | while read -r article
+	[ -n "$2" ] && printf '.ULS\n'
+	find "$1"/articles -name .metadata | while read -r article
 	do
 		grep "^.ds YEAR" "$article" | sed 's/^.ds YEAR[^0-9]*//g'
 	done | sort -ru | while read -r year
 	do
-		printf '.HnS 1\n%s\n.HnE\n.ULS\n' "$year";
-		grep -l "^.ds YEAR.*$year" noxz.tech/articles/*/.metadata           \
-		| xargs -I{} awk -v name="{}" '
+		[ -z "$2" ] && printf '.HnS 1\n%s\n.HnE\n\.ULS\n' "$year"
+		if [ -z "$2" ]; then
+			grep -l "^.ds YEAR.*$year" "$1"/articles/*/.metadata
+		else # filter by tag
+			grep -l "^${2}$" "$1"/articles/*/.tags                          \
+				| sed 's/\.tags$/.metadata/g'                               \
+				| xargs -I{} grep -l "^.ds YEAR.*$year" "{}"
+		fi | xargs -I{} awk -v name="{}" -v tag="${2}" '
 			BEGIN {
-			title = ""
+				title = ""
+				year = ""
 				month = ""
 				day = ""
 				gsub(/\/.metadata/,"",name)
@@ -257,6 +265,9 @@ render_articles() {
 					title = title" "
 				}
 			}
+			/^\.ds YEAR.*$/ {
+				year = $3
+			}
 			/^\.ds MONTH.*$/ {
 				month = substr($3,1,3)
 			}
@@ -264,11 +275,15 @@ render_articles() {
 				day = $3
 			}
 			END {
-				printf "%s %s \\(em\\n .URL \"%s\" \"%s\"\n", day, month, name, title
+				if (tag != "")
+					printf "%s %s, %s \\(em\\n .URL \"../../%s\" \"%s\"\n", day, month, year, name, title
+				else
+					printf "%s %s \\(em\\n .URL \"%s\" \"%s\"\n", day, month, name, title
 			}
 		' {} | sort -k 3Mr -k 2nr | sed 's/\\n /\n/g'
-		printf '.ULE\n';
+		[ -z "$2" ] && printf '.ULE\n'
 	done
+	[ -n "$2" ] && printf '.ULE\n'
 	echo .DIVE
 }
 
@@ -289,12 +304,18 @@ prerender() {
 	[ -n "$date" ] && printf '.P article-date\n\\*[DATE]\n\n'
 
 	cat - | while read -r line; do
-		if [ "${line}" = "{:articles:}" ]; then
-			render_articles
+		if echo "${line}" | grep -E '\{:articles(#[^#]*)?:\}' 2>/dev/null 1>&2; then
+			render_articles "$1"                                            \
+				"$(echo "${line}" | sed 's/\([^#]*#\?\)\(.*\)\(:.*$\)/\2/')"
 		else
 			printf '%s\n' "${line}"
 		fi
 	done
+
+	# print tags, if existing, at bottom of page
+	[ -f "${base_path}"/.tags ] && printf '\ntags:\n' && while read -r tag; do
+		printf '.URL "../tags/%s" "#%s"\n' "${tag}" "${tag}"
+	done < "${base_path}"/.tags
 }
 
 render_main_content() {
@@ -347,7 +368,7 @@ render_main_content() {
 			print
 		}
 	' "${SITE_WWW}"                                                         \
-	| prerender                                                             \
+	| prerender "$1"                                                        \
 	| cat "${CFG_GROFF_WWW_MACRO}" -                                        \
 	| groff                                                                 \
 		-Thtml -e -mwww -k                                                  \
@@ -464,7 +485,7 @@ render_main() {
 		-v var_main_title="$(awk_safe "${SITE_MAIN_TITLE}")"                \
 		-v var_menu="$(render_main_menu "$1" "$2" | awk_safe)"              \
 		-v var_menu_extra="$(render_main_menu_extra | awk_safe)"            \
-		-v var_content="$(render_main_content 2>/dev/null | awk_safe)"      \
+		-v var_content="$(render_main_content "$1" 2>/dev/null | awk_safe)" \
 		-v var_year="$(date +%Y)"                                           \
 	'
 		/\{:menu:\}/{
@@ -876,7 +897,7 @@ render_git_commits() {
 	| sort -r                                                               \
 	| sed "${SITE_GIT_COMMIT_COUNT}q"                                       \
 	| sed -e 's/^[^:]*://g'                                                 \
-	| awk '
+	| awk -v var_domain="$1" '
 		BEGIN {
 			print "<ul class=\"repo-log\">";
 		}
@@ -892,7 +913,7 @@ render_git_commits() {
 		}
 
 		END {
-			print "<li>[<a href=\"//noxz.tech/git/\">...</a>]</li></ul>"
+			printf "<li>[<a href=\"//%s/git/\">...</a>]</li></ul>\n",var_domain
 		}
 	' > "${_tmp_file}"
 
@@ -941,19 +962,42 @@ main() {
 	[ -n "$1" ] && [ ! -d "$1" ] && return
 	[ -n "$2" ] && [ ! -d "$2" ] && return
 
-	# make sure site/git is removed before rendering (to avoid it in menu)
-	rm -rf "${1}/git"
+	if [ -z "$2" ]; then
+		print_loading "Running the prerequisite script"
+		# make sure site/git is removed before rendering (to avoid it in menu)
+		rm -rf "${1}/git"
+
+		# make sure tag pages are removed before creating them
+		rm -rf "${1}"/articles/tags
+
+		# create tags directory
+		mkdir -p "${1}"/articles/tags
+		touch "${1}"/articles/tags/.buildignore
+		echo "index.html" > "${1}"/articles/tags/.assemble
+		echo ".ds TITLE   Tags" > "${1}"/articles/tags/.metadata
+
+		# manage tags
+		find "${1}"/articles -name .tags -exec cat {} \; | sort -u | while read -r tag
+		do
+			mkdir -p "${1}"/articles/tags/${tag}
+			touch "${1}"/articles/tags/${tag}/.buildignore
+			printf '.URL ../.. Articles\ntagged "%s"\n' "${tag}" > "${1}"/articles/tags/${tag}/index.www
+			echo "{:articles#${tag}:}" >> "${1}"/articles/tags/${tag}/index.www
+			echo "index.html" > "${1}"/articles/tags/${tag}/.assemble
+			echo ".ds TITLE   #${tag}" > "${1}"/articles/tags/${tag}/.metadata
+
+			# append tag to tags page
+			printf '.ARTTAG %s %s\n'                                           \
+				"${tag}" "$(grep ^${tag}$ "${1}"/articles/*/.tags | wc -l)" \
+				>> "${1}"/articles/tags/index.www
+		done
+		print_done
 
-	# main render
-	[ -z "$2" ]                                                             \
-	&& find "$1" -depth -type d                                             \
-		-exec test -e '{}/index.www' \;                                     \
-		-exec "$0" "$1" {} \;
-	[ -n "$2" ]                                                             \
-	&& render_main "$1" "$2"
+		# main render, executes sub render (see below)
+		find "$1" -depth -type d                                            \
+			-exec test -e '{}/index.www' \;                                 \
+			-exec "$0" "$1" {} \;
 
-	# post main render
-	if [ -z "$2" ]; then
 		# render git commits into place holders
 		print_loading "Rendering git commits"
 		render_git_commits "$1"
@@ -1009,7 +1053,9 @@ main() {
 		print_loading "Generating sitemaps"
 		generate_sitemap "${1}"
 		print_done
-
+	elif [ -n "$2" ]; then
+		# sub render
+		render_main "$1" "$2"
 	fi
 }
 
diff --git a/config/main.html b/config/main.html
@@ -44,9 +44,9 @@
 		<div>
 		<span class="right">
 			Copyright &copy; 2006-{:year:} Chris Noxz
-			| <a href="//noxz.tech/about/changelog">changelog</a>
-			| <a href="//noxz.tech/about/copying_policy">copying policy</a>
-			| <a href="//noxz.tech/git">source</a>
+			&bull; <a href="//noxz.tech/about/changelog">changelog</a>
+			&bull; <a href="//noxz.tech/about/copying_policy">copying policy</a>
+			&bull; <a href="//noxz.tech/git">source</a>
 		</span>
 		</div>
 	</div>
diff --git a/config/www.conf b/config/www.conf
@@ -74,6 +74,11 @@
 .       HTML <p class="\\$1">
 .   \}
 ..
+.de ARTTAG
+.   ie \\n[www-html] \{\
+.       HTML <span class="art-tag"><a href="\\$1">\\$1</a> (\\$2)</span>\\$3
+.   \}
+..
 .\" --------------------------------------------------------------------------
 .\" DIVS classname
 .\"
diff --git a/noxz.tech/pub/style.css b/noxz.tech/pub/style.css
@@ -271,6 +271,11 @@ span.right {
 	margin-bottom   : 2em;
 }
 
+.art-tag {
+	background      : #fafafa;
+	padding         : 0.2em 0.4em;
+}
+
 .spoiler {
 	color           : #000;
 	background      : #000;