]> juplo.de Git - maven-thymeleaf-skin/commitdiff
`import-in-hugo.sh` renders _only_ current _or_ archived hugo
authorKai Moritz <kai@juplo.de>
Wed, 29 Apr 2026 09:37:05 +0000 (11:37 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 29 Apr 2026 11:30:43 +0000 (13:30 +0200)
* The artifact-id and the version of the project are _always_ added to the
  specified base-path.
* If a *current* version is copied the version is dropped from the path
  that is added as `url` to front-matter.
* If a *archived* version is copied, the path of the current version is
  added as `params.canonical` to front-matter.

src/main/resources/import-in-hugo.sh [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index ee63d30..80c14a2
@@ -3,40 +3,43 @@ set -euo pipefail
 
 if [ $# -lt 1 ]
 then
-  echo "Usage: $0 <HUGO_ROOT> [ --current [<path>]] | --archived <path> ] [--canonical <path>]"
+  echo "Usage: $0 <HUGO_ROOT> [--base <path>] [ [ --current [<canonical>]] | [--archived [<canonical>]] ]" 
   echo
-  echo "--current  : [DEFAULT] specifies the path inside the hugo-project"
-  echo "             if the path is omitted, the artifact-id is used as path"
-  echo "--archived : specifies the base-path for an archived version"
-  echo "             the artifact-id and the version are added to the path"
-  echo "--canonical: specifies the path for canonical links in front-matter"
+  echo "--base     : Specifies the base-path for the rendered site."
+  echo "             If not specifed, the site is rendered as a root section."
+  echo "             The artifact-id and the version are always added to the path."
+  echo "--current  : [DEFAULT]"
+  echo "             Adds "url: <canonical>/<path_in_site>/" to front-matter."
+  echo "             If "<cononical>" is not specified, "<path>/<project>/" is used."
+  echo "             Only one of "--current" and "--archived" can be used!"
+  echo "--archived : Adds "params.canonical=<canonical>/<path_in_site>/" to front-matter."
+  echo "             If "<cononical>" is not specified, "<path>/<project>/" is used."
+  echo "             Only one of "--current" and "--archived" can be used!"
   exit 1
 fi
 
 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
+
 HUGO_ROOT="${1%%/}"
 shift
-CANONICAL_PATH=""
-CURRENT=1
-CANONICAL=0
+
+CURRENT=0
+ARCHIVED=0
+CANONICAL=""
 
 command -v jq >/dev/null 2>&1 || { echo "ERROR -- jq is required"; exit 1; }
 
 JSON=$(sed -n '/<script id="sili-json" type="application\/json">/,/<\/script>/p' ${SCRIPT_DIR}/index.html | sed '1d;$d')
 
-PROJECT=$(echo "$JSON" | jq -r '.artifactId')
-VERSION=$(echo "$JSON" | jq -r '.version')
-
-BASE="/$PROJECT"
+BASE=""
 
 while [[ $# -gt 0 ]]; do
   case "$1" in
-    --archived)
-      CURRENT=0
+    --base)
       if [[ $# -lt 2 ]]
       then
         echo "ERROR -- Parameter for --base is missing!"
-        exit 0
+        exit 1
       fi
       BASE="${2%/}"
       BASE="${BASE#/}"
@@ -46,45 +49,61 @@ while [[ $# -gt 0 ]]; do
       fi
       shift 2
       ;;
-    --canonical)
-      CANONICAL=1
-      if [[ $# -gt 1 ]]
+    --current)
+      CURRENT=1
+      if [[ $# -lt 2 ]]
       then
-        CANONICAL_PATH="$2"
-        CANONICAL_PATH="${CANONICAL_PATH%/}"
-        CANONICAL_PATH="${CANONICAL_PATH#/}"
-        if [[ -n "$CANONICAL_PATH" ]]
-        then
-          CANONICAL_PATH="/$CANONICAL_PATH"
-        fi
         shift
+      else
+        CANONICAL="${2%%/}"
+        CANONICAL="${CANONICAL##/}"
+        CANONICAL="/$CANONICAL"
+        shift 2
       fi
-      shift
       ;;
-    --current)
+    --archived)
+      ARCHIVED=1
       if [[ $# -lt 2 ]]
       then
-        echo "Using the artifact-id as path: $BASE"
         shift
       else
-        BASE="${2%%/}"
-        BASE="${BASE##/}"
-        if [[ -n "$BASE" ]]
-        then
-          BASE="/$BASE"
-        fi
+        CANONICAL="${2%%/}"
+        CANONICAL="${CANONICAL##/}"
+        CANONICAL="/$CANONICAL"
         shift 2
       fi
       ;;
     *)
-      echo "Unbekannter Parameter: $1"
+      echo "Unknown parameter: $1"
       exit 1
       ;;
   esac
 done
 
+if [[ "$CURRENT" -eq 1 && "$ARCHIVED" -eq 1 ]]
+then
+  echo "ERROR -- Only one of "--current" and "--archived" can be specified!"
+  exit 1
+fi
+if [[ "$CURRENT" -eq 0 && "$ARCHIVED" -eq 0 ]]
+then
+  CURRENT=1
+fi
+
 echo "$JSON" | jq -C .
 
+PROJECT=$(echo "$JSON" | jq -r '.artifactId')
+VERSION=$(echo "$JSON" | jq -r '.version')
+
+if [[ -n "$CANONICAL" ]]
+then
+  CANONICAL="${CANONICAL%/}"
+else
+  CANONICAL="$BASE/$PROJECT"
+fi
+
+BASE="$BASE/$PROJECT/$VERSION"
+
 for i in $(find "$SCRIPT_DIR" -maxdepth 1 -mindepth 1 -type f -name '*.html')
 do
   SOURCE="$i"
@@ -124,19 +143,21 @@ do
   echo "weight: $(echo "$PAGE_ENTRY" | jq -r .key)" >> $TARGET
   echo "outputs:" >> $TARGET
   echo "  - html" >> $TARGET
-  echo "url: $BASE$URL" >> $TARGET
-  echo "layout: article" >> $TARGET
-  if [[ -"$CANONICAL" -eq 1 || "$CURRENT" -eq 1 ]]
+  if [[ "$CURRENT" -eq 1 ]]
   then
-    echo "params:" >> $TARGET
+    echo "url: $CANONICAL$URL" >> $TARGET
+  else
+    echo "url: $BASE$URL" >> $TARGET
   fi
-  if [[ "$CANONICAL" -eq 1 ]]
+  echo "layout: article" >> $TARGET
+  echo "params:" >> $TARGET
+  if [[ "$CURRENT" -eq 1 ]]
   then
-    echo "  - cannonical: $CANONICAL_PATH$URL" >> $TARGET
+    echo "  current: true" >> $TARGET
   fi
-  if [[ "$CURRENT" -eq 1 ]]
+  if [[ "$ARCHIVED" -eq 1 ]]
   then
-    echo "  - current: true" >> $TARGET
+    echo "  canonical: $CANONICAL$URL" >> $TARGET
   fi
   echo "---" >> $TARGET
   sed -n '/<script id="sili-json" type="application\/json">/q;p' "$SOURCE" \
@@ -149,7 +170,7 @@ for i in $(find "$SCRIPT_DIR" -maxdepth 1 -mindepth 1 -type d)
 do
   DIR=$(basename $i $SCRIPT_DIR)
   echo "Copying additionally static content from $DIR"
-  cp -a "$i" "$HUGO_ROOT/static$BASE"
+  cp -av "$i" "$HUGO_ROOT/static$BASE"
   PAGE_ENTRY=$(echo "$JSON" | jq '.pages|to_entries[]|select(.value.href|test("^'"$DIR"'/"))')
   if [[ "$PAGE_ENTRY" != "" ]]
   then