diff options
| Commit | f8266b610f9787689a4f121c57c039952ca9cec2 (patch) | |
|---|---|---|
| Author | 2024-04-30 08:37:11 +0300 | |
| Committer | 2024-04-30 08:43:14 +0300 | |
| Files | 1274766dbe96116de8a39e0cb4fa07aff5065bec | |
| csg-main.tar csg-main.tar.gz csg-main.zip | ||
| Parent | 4409b3c0998d06ef3f06f40d1f691e8bf0328e2d (diff) | |
Now, rather than having all scripts at once, there are 6 types of script, running at 6 different "stages"
1 files changed, 117 insertions, 106 deletions
| -rwxr-xr-x | csg | 223 |
@@ -65,8 +65,28 @@ URL="$4" [ -z "$URL" ] && echo "[Error] <base_url> cannot be an empty string!" \ && csg_help 3 -AWKSCRIPTS="$5" -[ -n "$AWKSCRIPTS" -a ! -d "$AWKSCRIPTS" ] && echo "[Error] $AWKSCRIPTS (<scripts_dir>) isn't a directory or doesn't exist!" \ +LIB_SCRIPT="$5" +[ -n "$LIB_SCRIPT" -a ! -d "$LIB_SCRIPT" ] && echo "[Error] $LIB_SCRIPT (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +PREV_ALL="$6" +[ -n "$PREV_ALL" -a ! -d "$PREV_ALL" ] && echo "[Error] $PREV_ALL (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +PREV_MD="$7" +[ -n "$PREV_MD" -a ! -d "$PREV_MD" ] && echo "[Error] $PREV_MD (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +ON_MD="$8" +[ -n "$ON_MD" -a ! -d "$ON_MD" ] && echo "[Error] $ON_MD (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +ON_HTML="$9" +[ -n "$ON_HTML" -a ! -d "$ON_HTML" ] && echo "[Error] $ON_HTML (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +ON_OTHER="${10}" +[ -n "$ON_OTHER" -a ! -d "$ON_OTHER" ] && echo "[Error] $ON_OTHER (<scripts_dir>) isn't a directory or doesn't exist!" \ && csg_help 1 ### Preprocessing 3 @@ -89,7 +109,8 @@ if [ -n "$CSG_AWK" ] then : elif isCommand 'gawk' then - CSG_AWK='gawk --posix' + CSG_AWK='gawk + --posix' else echo '[Error] Couldn'\''t find an awk implementation!' csg_help 4 @@ -99,10 +120,10 @@ 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 \ + 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!' @@ -168,89 +189,59 @@ done << EOF $(find "$DESTINATION" -type f -name "*index.md" -links +1) EOF -if [ -n "$AWKSCRIPTS" ] && [ -n "$(find $AWKSCRIPTS -type f -name "[A-Z]*.awk")" ] -then - overwriteLine "[Stage 1] Combining awk scripts" +overwriteLine "[Stage 1] Combining awk scripts" - awk_scr="$($CSG_AWK -v TMP_DIR="$CSG_TMP_DIR" ' - { - # Remove first field - $1=""; sub(FS, "") - # Associate current file with regex - if (awks[$0] == "") { - awks[$0] = FILENAME - regexes[regexcount++] = $0 - } - else { - awks[$0] = awks[$0] " " FILENAME - } +combine_scripts() { + rm "$CSG_TMP_DIR/$2" >/dev/null 2>&1 - nextfile - } - END { - # Regexes are outputed in file alphabetic order - # Since certain scripts may require specific ordering - # between their rules, this is a crude way to allow that - for (regi = 0; regi < regexcount; regi++) { - print regexes[regi] - outf = TMP_DIR "/csg-" (awkcount++) ".awk" - print outf - system("cat " awks[regexes[regi]] " > " outf) - } - } - ' $(find $AWKSCRIPTS -type f -name "[A-Z]*.awk" | sort))" + [ -z "$1" ] && echo "" && return - overwriteLine "[Stage 1] Finding awk libraries" + scripts=$(find "$1" -type f -name "*.awk" | sort) + [ -z "$scripts" ] && echo "" && return - scr_libs="" - while read -r file - do - [ -n "$file" ] && scr_libs="$scr_libs -f $file " - done << EOF -$(find $AWKSCRIPTS -type f -name "#*.awk") -EOF -fi + cat $scripts > "$CSG_TMP_DIR/$2" + echo "-f $CSG_TMP_DIR/$2" +} +lib_script="$(combine_scripts "$LIB_SCRIPT" "csg-lib_script.awk")" +on_md="$(combine_scripts "$ON_MD" "csg-on_md.awk")" +on_html="$(combine_scripts "$ON_HTML" "csg-on_html.awk")" +on_other="$(combine_scripts "$ON_OTHER" "csg-on_other.awk")" +prev_all="$(combine_scripts "$PREV_ALL" "csg-prev_all.awk")" +prev_md="$(combine_scripts "$PREV_MD" "csg-prev_md.awk")" + +echo endTimer "[Stage 1] Done" ### Stage 2 ### Transforming Markdown startTimer "[Stage 2] Transforming Markdown to HTML" -transform_file() { - local file="$1" - local outf="${file%md}html" - - # Gets special information from the file - local title descr words - { read -r title; read -r descr; read -r words; } << EOF -$($CSG_AWK -v titlef="$TITLEF" ' - title == "" && /^# / { - # Remove first field - $1 = ""; sub(FS, "") - # Output title - sub(/.*/, titlef) - title = $0 - next +# prev_all +cat << EOF > "$CSG_TMP_DIR/csg-export.awk" +function export(name, value) { + if (name == "" || value == "") { + return } - stopdesc == 0 && /^[[: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) "..." - stopdesc = 1 - } + # if is string + if (value + 0 != value) { + gsub(/"/, "\\\\\\"", value) } - stopdesc == 0 && startedGathering == 1 && /^$/ { stopdesc = 1 } - { words += NF } - END { print title ORS desc ORS words }' "$file") + + printf " -v " name "=" ((value + 0 == value) ? value : ("\\"" value "\\"")) " " +} EOF +exports=$($CSG_AWK \ + -f "$CSG_TMP_DIR/csg-export.awk" \ + $lib_script $prev_all \ + $(find "$DESTINATION" -type f -name "*.md")) + +echo '$0 != 0 {print}' > "$CSG_TMP_DIR/csg-print.awk" +echo 'BEGIN { FILENAME = truefilename } NR == 1 { FILENAME = truefilename }' > "$CSG_TMP_DIR/csg-filename.awk" + +transform_file() { + local file="$1" + local outf="${file%md}html" local created edited if [ -f "$CSG_TIMESTAMP_FILE" ] @@ -265,50 +256,70 @@ $($CSG_AWK -v file="$outf" ' EOF fi - # Generate - $CSG_MD_TO_HTML "$file" > "$outf" - - # Run custom awk scripts - local scripts="$(printf "$awk_scr" | $CSG_AWK -v file="$outf" ' - capture == 1 { capture = 0; printf " -f " $0; next; } - capture == 2 { capture = 0; next; } - { capture = 1 + !match(file, $0) } - ')" + # prev_md + local_exports=$($CSG_AWK\ + -f "$CSG_TMP_DIR/csg-export.awk"\ + $lib_script $prev_md\ + "$file") + # on_md and on_html $CSG_AWK \ - -v title="$title" \ - -v description="$descr" \ - -v words="$words" \ -v created="$created" \ -v edited="$edited" \ - $scr_libs $scripts -f "$CSG_TMP_DIR/csg-print.awk" \ - "$outf" > "$outf.tmp" - mv "$outf.tmp" "$outf" + $exports $local_exports \ + $lib_script $on_md -f "$CSG_TMP_DIR/csg-print.awk" \ + "$file" \ + | $CSG_MD_TO_HTML \ + | cat "$SOURCE/_header.html" - "$SOURCE/_footer.html" \ + | $CSG_AWK \ + -v created="$created" \ + -v edited="$edited" \ + $exports $local_exports \ + -v truefilename="$outf" -f "$CSG_TMP_DIR/csg-filename.awk" \ + $lib_script $on_html -f "$CSG_TMP_DIR/csg-print.awk" \ + > "$outf" - # Create final file - $CSG_AWK \ - -v title="$title" \ - -v description="$descr" ' - BEGIN { gsub(/&/, "\\\\&", title) } - /\[DESCRIPTION\]/ { - gsub(/\[DESCRIPTION\]/, description) - } - /\[TITLE\]/ { - gsub(/\[TITLE\]/, title) - } - {print}' "$SOURCE/_header.html" > "$file" - cat "$outf" "$SOURCE/_footer.html" >> "$file" + # For hardlinks + cat "$outf" > "$file" mv "$file" "$outf" } -echo '$0 != 0 {print}' > "$CSG_TMP_DIR/csg-print.awk" +tempifs=$IFS +IFS=' + ' while read -r file do - transform_file "$file" "$scripts" & + transform_file "$file" & done << EOF $(find "$DESTINATION" -type f -name "*.md") EOF + +IFS=$tempifs + +if [ -n "$on_other" ] +then + transform_other() { + local file="$1" + + $CSG_AWK \ + $exports $local_exports \ + $lib_script $on_other -f "$CSG_TMP_DIR/csg-print.awk" \ + "$file" > temp + mv temp "$file" + } + + while read -r file + do + if [ -f "$file" ] + then + transform_other "$file" & + fi + done << EOF +$(find "$DESTINATION" -type f -not -name "*.md") +EOF +fi + wait overwriteLine "[Stage 2] Generating sitemap" |
