Last active
April 8, 2025 14:15
-
-
Save NiceRath/0f1e20bef3d0eea993f7439bc8fc96e6 to your computer and use it in GitHub Desktop.
Sphinx - Add Create/Modify Timestamps (GIT-REPO)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SCRIPT 1: | |
# BUILDS LIST OF FILE-to-TIMESTAMP CSV | |
# EXECUTE IN GIT REPO | |
# NOTE: if you also build the docs on your source-system, you can merge it with script #2 | |
#!/usr/bin/env bash | |
set -e | |
cd "$(dirname "$0")" | |
MAX_PARALLEL=$(nproc) # * 2 | |
export TS_CACHE_DIR='build/ts_cache/' | |
export TS_CACHE='source/ts_cache.txt' | |
mkdir -p "$TS_CACHE_DIR" | |
rm -f "$TS_CACHE" | |
rm -f "$TS_CACHE_DIR"* | |
function add_timestamps() { | |
src_file="$1" | |
file_base="$(echo "$src_file" | cut -d '/' -f 2- | sed 's|\.md||g' | sed 's|\.rst||g' | sed 's|,||g')" | |
dst_file="${file_base}.html" | |
create_date="$(git log -n 1 --diff-filter=A --follow --pretty=format:%aI "$src_file" | cut -d '+' -f1 | sed 's|T| |g')" | |
update_date="$(git log -n 1 --follow --pretty=format:%aI "$src_file" | cut -d '+' -f1 | sed 's|T| |g')" | |
# echo "$src_file $dst_file" | |
# echo "$create_date $update_date" | |
dst_md5="$(echo "$dst_file" | md5sum | cut -d ' ' -f1)" | |
echo "${dst_file},${create_date},${update_date}" >> "${TS_CACHE_DIR}${dst_md5}.txt" | |
} | |
echo '### BUILD CREATE/UPDATE TIMESTAMPS ###' | |
export -f add_timestamps | |
find source/ -name '*.md' -print0 | xargs -0 -P "$MAX_PARALLEL" -I {} bash -c 'add_timestamps "{}"' | |
find source/ -name '*.rst' -print0 | xargs -0 -P "$MAX_PARALLEL" -I {} bash -c 'add_timestamps "{}"' | |
cat "$TS_CACHE_DIR"* >> "$TS_CACHE" | |
# SCRIPT 2: | |
# BUILD SPHINX AND INSERT TIMESTAMPS-HTML (FROM CACHE-FILE) | |
# NOTE: you might need to change '/contentinfo\">' to whatever element you want to append the timestamps to | |
# bash html.sh <BASE_DIR> <SOURCE_DIR> <DEST_DIR> | |
#!/usr/bin/env bash | |
set -e | |
BASE_DIR="$(dirname "$0")" | |
SRC='source' | |
export DST='build' | |
if [[ -n "$1" ]] | |
then | |
BASE_DIR="$1" | |
fi | |
if [[ -n "$2" ]] | |
then | |
SRC="$2" | |
fi | |
if [[ -n "$3" ]] | |
then | |
DST="$3" | |
fi | |
cd "$BASE_DIR" | |
TS_CACHE="${SRC}/ts_cache.txt" | |
rm -rf build | |
mkdir -p build | |
sphinx-build -b html "$SRC" "$DST" | |
function add_timestamps() { | |
IFS=',' read -ra array <<< "$1" | |
dst_file="${DST}/${array[0]}" | |
create_date="${array[1]}" | |
update_date="${array[2]}" | |
if [[ ! -f "$dst_file" ]] | |
then | |
echo "FILE NOT FOUND: ${dst_file} (TIMESTAMPS)" | |
return | |
fi | |
sed -i "/contentinfo\">/a <p><b>Created</b>: ${create_date}</p>" "$dst_file" | |
sed -i "/contentinfo\">/a <p><b>Changed</b>: ${update_date}</p>" "$dst_file" | |
} | |
echo '### ADD CREATE/UPDATE TIMESTAMPS ###' | |
if [[ -f "$TS_CACHE" ]] | |
then | |
while IFS= read -r line | |
do | |
add_timestamps "$line" | |
done < "$TS_CACHE" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment