Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitd3cd973232364a9a2f6a8d42f792b7d7cc3b47a5 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-10-09 19:04:59 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-10-09 19:04:59 +0300
Filesc30a05e6a5d523592b9725ece0775bdf23013de3
csg-d3cd973232364a9a2f6a8d42f792b7d7cc3b47a5.tar
csg-d3cd973232364a9a2f6a8d42f792b7d7cc3b47a5.tar.gz
csg-d3cd973232364a9a2f6a8d42f792b7d7cc3b47a5.zip
Parent191669536ce426cea684a99462c89a2a56c17cd9 (diff)
[csg] Added initial proof of concept
1 files changed, 88 insertions, 0 deletions
-rwxr-xr-xcsg88
diff --git a/csg b/csg
new file mode 100755
index 0000000..556254e
--- /dev/null
+++ b/csg
@@ -0,0 +1,88 @@
+#!/bin/bash
+
+AWKS="$1"
+SOURCE="$2"
+OUTPUT="$(realpath "$3")"
+TITLEF="$4"
+
+TIMESTAMPS="timestamps.txt"
+
+# $1 - file path
+# $2 - stat file
+# Line is in the format: name¶created¶edited
+gitStats() {
+ local dates=($(git log --follow --pretty=format:%ad --date="format:%d %B %Y" -- $1))
+ local line="./website${file:5:-2}html¶${dates[@]: -3:3}¶${dates[@]:0:3}"
+ echo -e $line >> $2
+}
+
+headc=$(git rev-parse HEAD)
+update_timestamps=""
+if [ ! -f "$TIMESTAMPS" ] || [ "$headc" != "$(head -1 "$TIMESTAMPS")" ]; then
+ update_timestamps="1"
+ echo "$headc" > "$TIMESTAMPS"
+fi
+
+# Copy from SOURCE to OUTPUT
+(cd "$SOURCE" && \
+ find . -type f -not -name "\.*" -not -name '_header.html' -not -name '_footer.html' | \
+ cpio -pdu "$OUTPUT")
+
+find "$OUTPUT" -type f -name "*index.md" -links +1 | while read -r file; do
+ mv "$file" "${file%md}html"
+done
+
+transform-file() {
+ local file="$1"
+ local outf="${file%md}html"
+
+ # Markdown stats for a file: title, description, words, date created, date edited
+ local title="$(grep -m 1 '^# ' "$file")"
+ local title="${title:2}"
+ local descr="$(awk '
+ /^[[:alpha:]]/ {
+ startedGathering = 1
+ # Take only anchor text
+ gsub(/\]\([^)]*\)/, "")
+ gsub(/\[/, "")
+ # Append line
+ if (desc == "") desc = $0
+ else desc = desc " " $0
+ # Trim line to 200 characters
+ if (length(desc) > 200) {
+ desc = substr(desc, 1, 197) "..."
+ exit
+ }
+ }
+ startedGathering && /^$/ { exit }
+ END { print desc }' "$file")"
+ local words="$(wc -w "$file" | cut -d ' ' -f 1)"
+ [ -n "$update_timestamps" ] && gitStats "$file" "$TIMESTAMPS"
+
+ # Generate
+ lowdown \
+ --html-no-escapehtml \
+ --html-no-skiphtml \
+ --parse-no-metadata \
+ --parse-no-autolink \
+ "$file" > "$outf"
+
+ # Run custom awk scripts
+
+ # Create final file
+ awk -v title="$title" -v description="$descr" '
+ /\[DESCRIPTION\]/ {
+ gsub(/\[DESCRIPTION\]/, description)
+ }
+ /\[TITLE\]/ {
+ gsub(/\[TITLE\]/, title)
+ }
+ {print}' "$SOURCE/_header.html" > "$file"
+ cat "$outf" "$SOURCE/_footer.html" >> "$file"
+ mv "$file" "$outf"
+}
+
+while read -r file; do
+ transform-file "$file" &
+done < <(find "$OUTPUT" -type f -name "*.md")
+wait