diff options
| Commit | 98b9c619ec8b39942300b39252e90f017efe9529 (patch) | |
|---|---|---|
| Author | 2023-10-16 19:49:22 +0300 | |
| Committer | 2023-10-16 19:49:22 +0300 | |
| Files | c77402a0af502471c56f3e2ba075aeaf7621f254 | |
| csg-98b9c619ec8b39942300b39252e90f017efe9529.tar csg-98b9c619ec8b39942300b39252e90f017efe9529.tar.gz csg-98b9c619ec8b39942300b39252e90f017efe9529.zip | ||
| Parent | 8879e8c691b94710c0e1903f685c99d7c56e15e1 (diff) | |
(csg) Added help menu, a lot of variable checks and abstracted awk and lowdown into variables
1 files changed, 91 insertions, 27 deletions
| -rwxr-xr-x | csg | 118 |
@@ -1,12 +1,5 @@ #!/bin/bash -AWKS="$1" -SOURCE="$2" -OUTPUT="$(realpath "$3")" -TITLEF="$4" - -TIMESTAMPS="$(realpath ./timestamps.txt)" - ### Helper functions isCommand() { @@ -27,6 +20,83 @@ endTimer() { printf "\e[1A\e[K$1 %.3fs\n" $_dur } +### Parameters and variables + +csg_help() { + echo 'Usage: csg <source_dir> <output_dir> <title_format> [<scripts_dir>] + +Arguments: + <source_dir> Folder which contains source files + <output_dir> Folder in which the generated site will be put + <title_format> Format string for page title + Accepts any text or HTML, & is replaced with contents + title (first h1 header). + <scripts_dir> Optional, folder which contains awk script for editing of + generated webpages. + +Variables: + CSG_AWK + CSG_MD_TO_HTML + CSG_TIMESTAMPS_FILE + CSG_TEMP_DIR + +Exit status: + 1 Missing dependency + 2 Bad file type or missing read/write permissions + +Dependencies: + bash + awk' + exit $1 +} + +[ -z "$1" -o "$1" = '-h' -o "$1" = '-?' -o "$1" = '--help' ] && csg_help 0 + +SOURCE="$1" +[ ! -d "$SOURCE" ] && echo "[Error] Passed $SOURCE isn't a directory or doesn't exist!" \ + && csg_help 2 +[ ! -r "$SOURCE" ] && echo "[Error] No read access to $SOURCE!" \ + && csg_help 2 + +OUTPUT="$(realpath "$2")" +TITLEF="$3" + +AWKSCRIPTS="$4" +[ ! -d "$AWKSCRIPTS" ] && echo '[Error] Passed <scripts_dir> isn'\''t a directory or doesn'\''t exist!' \ + && csg_help 2 + +if [ -n "$CSG_AWK" ] +then : +elif isCommand 'gawk' +then + CSG_AWK='gawk --posix' +else + echo '[Error] Couldn'\''t find an awk implementation!' + csg_help 1 +fi + +if [ -n "$CSG_MD_TO_HTML" ] +then : +elif isCommand 'lowdown' +then + CSG_MD_TO_HTML='lowdown \ + --html-no-escapehtml \ + --html-no-skiphtml \ + --parse-no-metadata \ + --parse-no-autolink' +else + echo '[Error] Couldn'\''t find a Markdown to HTML convertion program!' + csg_help 1 +fi + +: ${CSG_TIMESTAMP_FILE:="$(realpath ./timestamps.txt)"} + +: ${CSG_TMP_DIR:='/tmp'} +[ ! -d "$CSG_TMP_DIR" ] && echo "[Error] Temporary directory $CSG_TMP_DIR doesn't exist!" \ + && csg_help 2 +[ ! -r "$CSG_TMP_DIR" -o ! -w "$CSG_TMP_DIR" ] && echo "[Error] No read or write permissions to temporary directory $CSG_TMP_DIR!" \ + && csg_help 2 + ### Stage 0 ### Create timestamps.txt file, if necessary @@ -37,15 +107,15 @@ then do dates=($(git log --follow --pretty=format:%ad --date="format:%d %B %Y" -- "$file")) line="$OUTPUT${file:1:-2}html\t${dates[@]: -3:3}\t${dates[@]:0:3}" - echo -e $line >> "$TIMESTAMPS" + echo -e $line >> "$CSG_TIMESTAMP_FILE" done } headc=$(git rev-parse HEAD) - if [ ! -f "$TIMESTAMPS" ] || [ "$headc" != "$(head -1 "$TIMESTAMPS")" ] + if [ ! -f "$CSG_TIMESTAMP_FILE" ] || [ "$headc" != "$(head -1 "$CSG_TIMESTAMP_FILE")" ] then startTimer "[Stage 0] Creating timestamps file" - echo "$headc" > "$TIMESTAMPS" + echo "$headc" > "$CSG_TIMESTAMP_FILE" (cd "$SOURCE" && timestamps_writeDates) endTimer "[Stage 0] Done" fi @@ -70,10 +140,10 @@ scr_path=() scr_recip=() while read -r reg; read -r scripts do - cat $scripts > /tmp/csg-${#scr_path[@]}.awk - scr_path+=("/tmp/csg-${#scr_path[@]}.awk") + cat $scripts > $CSG_TMP_DIR/csg-${#scr_path[@]}.awk + scr_path+=("$CSG_TMP_DIR/csg-${#scr_path[@]}.awk") scr_recip+=("$(find $OUTPUT -type f -regex "$reg")") -done < <(awk ' +done < <($CSG_AWK ' { # Remove first field $1=""; sub("^["FS"]*", "", $0) @@ -89,7 +159,7 @@ END { print awks[reg] } } -' $(find $AWKS -type f -name "[A-Z]*.awk")) +' $(find $AWKSCRIPTS -type f -name "[A-Z]*.awk")) overwriteLine "[Stage 1] Finding awk libraries" @@ -97,7 +167,7 @@ scr_libs="" while read -r file do scr_libs+=" -f $file " -done < <(find $AWKS -type f -name "#*.awk") +done < <(find $AWKSCRIPTS -type f -name "#*.awk") endTimer "[Stage 1] Done" @@ -111,8 +181,8 @@ transform-file() { local scripts="$2" # Markdown stats for a file: title, description, words, date created, date edited - local title="$(awk '/^# / {print substr($0, 2); exit}' "$file")" - local descr="$(awk ' + local title="$($CSG_AWK '/^# / {print substr($0, 2); exit}' "$file")" + local descr="$($CSG_AWK ' /^[[:alpha:]]/ { startedGathering = 1 # Take only anchor text @@ -130,27 +200,21 @@ transform-file() { 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" + $CSG_MD_TO_HTML "$file" > "$outf" # Run custom awk scripts - awk --posix \ + $CSG_AWK \ -v title="$title" \ -v description="$descr" \ -v words="$words" \ - $scr_libs $scripts -f '/tmp/csg-print.awk' \ + $scr_libs $scripts -f '$CSG_TMP_DIR/csg-print.awk' \ "$outf" > "$outf.tmp" mv "$outf.tmp" "$outf" # Create final file - awk -v title="$title" -v description="$descr" ' + $CSG_AWK -v title="$title" -v description="$descr" ' /\[DESCRIPTION\]/ { gsub(/\[DESCRIPTION\]/, description) } |
