Compare commits
10 Commits
45a136dfc3
...
d098c7c290
Author | SHA1 | Date | |
---|---|---|---|
d098c7c290 | |||
0cb59bd30b | |||
1f05a69106 | |||
0023d8ec7f | |||
554071f7f4 | |||
cd891e38e9 | |||
c965b825fb | |||
1bf8064ac9 | |||
568050cb53 | |||
c82b13f3ca |
127
build.sh
127
build.sh
@@ -1,5 +1,29 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# TODO: Add a comment block at the top of the script, describing what it does.
|
||||||
|
# TODO: Add a '-h' or '--help' flag, that displays information about the script, and how to use it.
|
||||||
|
|
||||||
|
|
||||||
|
# s4g - Stupid Simple Static-Site Generator
|
||||||
|
# ---------------------------------------------
|
||||||
|
#
|
||||||
|
# This is a static-site generator, that can be used to convert Markdown files into HTML.
|
||||||
|
# It is extremely simple and extremely opinionated, as you can see if you read the code below.
|
||||||
|
# A lot of paths and directories are hard-coded, to suit my workflow.
|
||||||
|
#
|
||||||
|
# To use it, create a directory for your project (e.g. 'website'). Inside 'website', create
|
||||||
|
# two directories: 'source' (which holds your Markdown files) and 'output' (which holds the
|
||||||
|
# converted CSS.
|
||||||
|
#
|
||||||
|
# In addition to these directories, three files are needed in 'website':
|
||||||
|
# 1. 'header.html' - A header, which is prepended to every source file. Unfortunately, this must
|
||||||
|
# be written in regular HTML.
|
||||||
|
# 2. 'footer.html' - A footer, which is appended to every source file. Also must be written in HTML.
|
||||||
|
# 3. 'styles.css' - A global stylesheet.
|
||||||
|
#
|
||||||
|
# If you have any comments or questions, please email aadhavan@twomorecents.org.
|
||||||
|
|
||||||
|
|
||||||
set -o errexit # Stop executing when a command fails
|
set -o errexit # Stop executing when a command fails
|
||||||
set -o nounset # Stop executing when accessing an unset variable
|
set -o nounset # Stop executing when accessing an unset variable
|
||||||
set -o pipefail # Treat a pipeline as failing, even if one command in the pipeline fails
|
set -o pipefail # Treat a pipeline as failing, even if one command in the pipeline fails
|
||||||
@@ -39,7 +63,7 @@ setup_temp_dir() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setup_output_dir() {
|
setup_output_dir() {
|
||||||
rm -r "${BASE_PATH}/output" # Delete existing 'output' directory
|
rm -rf "${BASE_PATH}/output" # Delete existing 'output' directory
|
||||||
cp -r "${BASE_PATH}/source" "${BASE_PATH}/output" #Copy directory structure from 'source' to 'output'
|
cp -r "${BASE_PATH}/source" "${BASE_PATH}/output" #Copy directory structure from 'source' to 'output'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +77,9 @@ read_metadata() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
convert_to_array() {
|
convert_to_array() {
|
||||||
|
local meta_key
|
||||||
|
local meta_value
|
||||||
|
|
||||||
# Converts the metadata into two arrays: one with the key, and the other with the value.
|
# Converts the metadata into two arrays: one with the key, and the other with the value.
|
||||||
readarray -t meta_key < <(echo -e "$1" | awk -F: '{print $1}')
|
readarray -t meta_key < <(echo -e "$1" | awk -F: '{print $1}')
|
||||||
readarray -t meta_value < <(echo -e "$1" | awk -F: '{st = index($0,":"); values = substr($0,st+1); print values}' | cut -c 2-)
|
readarray -t meta_value < <(echo -e "$1" | awk -F: '{st = index($0,":"); values = substr($0,st+1); print values}' | cut -c 2-)
|
||||||
@@ -71,39 +98,41 @@ add_date_to_array() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
add_header_and_footer() {
|
add_header_and_footer() {
|
||||||
# Copy header to temporary location
|
# Copy header to temporary location - 'parent_dir' is used to ensure that
|
||||||
cp "$BASE_PATH/header.html" "$BASE_PATH/temp/temp_header.html"
|
# each temporary header is in its own directory
|
||||||
|
cp "$BASE_PATH/header.html" "$BASE_PATH/temp/$parent_dir/temp_header.html"
|
||||||
|
|
||||||
# Check for relevant metadata, and perform corresponding action
|
# Check for relevant metadata, and perform corresponding action
|
||||||
if [[ ! -v "meta_array["date"]" ]]; then # If there is no date
|
if [[ ! -v "meta_array["date"]" ]]; then # If there is no date
|
||||||
sed -i '$ d' "$BASE_PATH/temp/temp_header.html" # remove the 'date published' section of the header
|
sed -i '$ d' "$BASE_PATH/temp/$parent_dir/temp_header.html" # remove the 'date published' section of the header
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${meta_array[noappend]-}" == "true" ]]; then
|
if [[ "${meta_array[noappend]-}" == "true" ]]; then
|
||||||
sed -i 's/ - Two More Cents//g' "$BASE_PATH/temp/temp_header.html" # 'noappend' removes the suffix from the title
|
sed -i 's/ - Two More Cents//g' "$BASE_PATH/temp/$parent_dir/temp_header.html" # 'noappend' removes the suffix from the title
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add header
|
# Add header
|
||||||
cat "$BASE_PATH/temp/temp_header.html" | cat - "$1" > "$BASE_PATH/temp/temp.html"
|
cat "$BASE_PATH/temp/$parent_dir/temp_header.html" | cat - "$1" > "$BASE_PATH/temp/$parent_dir/temp.html"
|
||||||
|
|
||||||
# Add footer
|
# Add footer
|
||||||
echo >> "$BASE_PATH/temp/temp.html" # Add newline
|
echo >> "$BASE_PATH/temp/$parent_dir/temp.html" # Add newline
|
||||||
cat "$BASE_PATH/footer.html" >> "$BASE_PATH/temp/temp.html"
|
cat "$BASE_PATH/footer.html" >> "$BASE_PATH/temp/$parent_dir/temp.html"
|
||||||
|
|
||||||
# Move temp file to original location
|
# Move temp file to original location
|
||||||
mv "$BASE_PATH/temp/temp.html" "$1"
|
mv "$BASE_PATH/temp/$parent_dir/temp.html" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
add_header_and_footer_to_index() {
|
add_header_and_footer_to_index() {
|
||||||
|
mkdir "$BASE_PATH/temp/index_page"
|
||||||
# Add header
|
# Add header
|
||||||
cat "$BASE_PATH/header.html" | head -n -1 | cat - "$1" > "$BASE_PATH/temp/temp.html" # For the index page, remove the last line of the header (date published)
|
cat "$BASE_PATH/header.html" | head -n -1 | cat - "$1" > "$BASE_PATH/temp/index_page/temp.html" # For the index page, remove the last line of the header (date published)
|
||||||
|
|
||||||
# Add footer
|
# Add footer
|
||||||
echo >> "$BASE_PATH/temp/temp.html" # Add newline
|
echo >> "$BASE_PATH/temp/index_page/temp.html" # Add newline
|
||||||
cat "$BASE_PATH/footer.html" >> "$BASE_PATH/temp/temp.html"
|
cat "$BASE_PATH/footer.html" >> "$BASE_PATH/temp/index_page/temp.html"
|
||||||
|
|
||||||
# Move temp file to original location
|
# Move temp file to original location
|
||||||
mv "$BASE_PATH/temp/temp.html" "$1"
|
mv "$BASE_PATH/temp/index_page/temp.html" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
replace_vars() {
|
replace_vars() {
|
||||||
@@ -115,41 +144,63 @@ replace_vars() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
md_to_html() {
|
convert_file() {
|
||||||
# Convert .md files from 'source' and place them into the correct locations into 'output'
|
# Helper function for md_to_html(). It takes in the file to convert as an argument,
|
||||||
|
# and converts that file.
|
||||||
|
file_to_conv="$1"
|
||||||
|
echo "Converting $file_to_conv"
|
||||||
|
|
||||||
files=$(find "$BASE_PATH/source" -name "*.md")
|
read_metadata "$file_to_conv" # Sets the 'metadata' variable
|
||||||
|
|
||||||
for file in $files; do
|
# Generate a random 8-character alphabetic string, until we find
|
||||||
read_metadata "$file" # Sets the 'metadata' variable
|
# one that doesn't exist in the 'temp' directory. This string
|
||||||
|
# will serve as the parent directory of our file.
|
||||||
convert_to_array "$metadata" #Sets the 'meta_array' array
|
parent_dir="$(tr -dc A-Za-z </dev/urandom | head -c 8; echo)"
|
||||||
add_date_to_array "$file" #Uses 'meta_array' array
|
while ls -r temp | grep -q "$parent_dir" ; do
|
||||||
|
parent_dir="$(tr -dc A-Za-z </dev/urandom | head -c 8; echo)"
|
||||||
|
done
|
||||||
|
|
||||||
# Copy file to temp dir and strip metadata
|
# Copy file to temp dir and strip metadata
|
||||||
cp "$file" "$BASE_PATH/temp/"
|
mkdir -p "$BASE_PATH/temp/$parent_dir/"
|
||||||
|
cp "$file_to_conv" "$BASE_PATH/temp/$parent_dir/"
|
||||||
let num_lines=$(echo "$metadata" | wc -l)+1
|
let num_lines=$(echo "$metadata" | wc -l)+1
|
||||||
sed -i "1,${num_lines}d" "$BASE_PATH/temp/$(basename "$file")"
|
sed -i "1,${num_lines}d" "$BASE_PATH/temp/$parent_dir/$(basename "$file_to_conv")"
|
||||||
|
|
||||||
# Construct path for output file
|
# Construct path for output file
|
||||||
path_for_output=$(realpath --relative-to="${BASE_PATH}/source" "$file")
|
local path_for_output=$(realpath --relative-to="${BASE_PATH}/source" "$file_to_conv")
|
||||||
path_for_output="${BASE_PATH}/output/${path_for_output}"
|
path_for_output="${BASE_PATH}/output/${path_for_output}"
|
||||||
path_for_output="$(dirname $path_for_output)/$(basename $path_for_output .md).html"
|
path_for_output="$(dirname $path_for_output)/$(basename $path_for_output .md).html"
|
||||||
|
|
||||||
# Convert the file (using the given filters), and place the output in the correct location
|
# Convert the file (using the given filters), and place the output in the correct location.
|
||||||
pandoc --lua-filter "$BASE_PATH"/pandoc_filters/* -f markdown --wrap=preserve "$BASE_PATH/temp/$(basename "$file")" > "${path_for_output}"
|
pandoc --lua-filter "$BASE_PATH"/pandoc_filters/* -f markdown --wrap=preserve "$BASE_PATH/temp/$parent_dir/$(basename "$file_to_conv")" > "${path_for_output}"
|
||||||
rm "$BASE_PATH"/temp/*
|
|
||||||
|
|
||||||
add_header_and_footer "$path_for_output" # Uses 'meta_array' array
|
convert_to_array "$metadata" #Sets the 'meta_array' array
|
||||||
|
|
||||||
|
add_date_to_array "$file_to_conv" #Uses 'meta_array' array
|
||||||
|
add_header_and_footer "$path_for_output" # Uses 'meta_array' array and 'parent_dir' variable
|
||||||
replace_vars "$path_for_output" #Uses 'meta_array' array
|
replace_vars "$path_for_output" #Uses 'meta_array' array
|
||||||
|
|
||||||
unset metadata meta_key meta_value meta_array
|
unset metadata meta_key meta_value meta_array path_for_output
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
md_to_html() {
|
||||||
|
# Convert .md files from 'source' and place them into the correct locations into 'output'
|
||||||
|
local files=$(find "$BASE_PATH/source" -name "*.md")
|
||||||
|
|
||||||
|
# Concurrently convert each document
|
||||||
|
for file in $files; do
|
||||||
|
(convert_file "$file") &
|
||||||
|
unset metadata path_for_output
|
||||||
|
done
|
||||||
|
|
||||||
|
# Wait for all documents to finish converting, then remove all temporary files.
|
||||||
|
wait
|
||||||
|
rm -rf "$BASE_PATH"/temp/*
|
||||||
|
}
|
||||||
|
|
||||||
gen_sorted_file_list() { # Generate a list of the HTMl files, sorted by when they were last modified (read from the contents of the HTML file)
|
gen_sorted_file_list() { # Generate a list of the HTMl files, sorted by when they were last modified (read from the contents of the HTML file)
|
||||||
files=$(find "$BASE_PATH/output" -name "*.html")
|
local files=$(find "$BASE_PATH/output" -name "*.html")
|
||||||
local date_mod
|
local date_mod
|
||||||
|
|
||||||
for file in $files; do
|
for file in $files; do
|
||||||
@@ -179,19 +230,21 @@ gen_sorted_file_list() { # Generate a list of the HTMl files, sorted by when the
|
|||||||
|
|
||||||
gen_index_page() { # Generate an index page (site map) that includes links to the other pages
|
gen_index_page() { # Generate an index page (site map) that includes links to the other pages
|
||||||
|
|
||||||
index_file_html="<nav class=\"toc\">"$'\n' # Variable to store the body HTML of the index page; enclose the list in a nav
|
echo "Generating index page..."
|
||||||
|
|
||||||
|
local index_file_html="<nav class=\"toc\">"$'\n' # Variable to store the body HTML of the index page; enclose the list in a nav
|
||||||
|
|
||||||
for file in $1; do
|
for file in $1; do
|
||||||
title=$(cat "$file" | grep "<title>" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the title of the web page
|
local title=$(cat "$file" | grep "<title>" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the title of the web page
|
||||||
suffix=" - Two More Cents"
|
local suffix=" - Two More Cents"
|
||||||
title="${title%"$suffix"}" # Remove the website name from it
|
title="${title%"$suffix"}" # Remove the website name from it
|
||||||
|
|
||||||
pub_date=$(cat "$file" | grep "date-published" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the date published
|
local pub_date=$(cat "$file" | grep "date-published" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the date published
|
||||||
prefix="Published on " # Find date published of webpage
|
prefix="Published on " # Find date published of webpage
|
||||||
pub_date="${pub_date#"$prefix"}" # Remove the prefix from it
|
pub_date="${pub_date#"$prefix"}" # Remove the prefix from it
|
||||||
pub_date=$(echo "$pub_date" | tr -d "," | awk '{print $2" "$1" "$3}' | date -f - +"%m/%d/%Y") # Re-arrange the date and convert to mm/dd/yy
|
pub_date=$(echo "$pub_date" | tr -d "," | awk '{print $2" "$1" "$3}' | date -f - +"%m/%d/%Y") # Re-arrange the date and convert to mm/dd/yy
|
||||||
|
|
||||||
file_path=$(realpath --relative-to="${BASE_PATH}/output" "$file")
|
local file_path=$(realpath --relative-to="${BASE_PATH}/output" "$file")
|
||||||
|
|
||||||
index_file_html+="<li><time>${pub_date}</time> - <a href=\"$file_path\">$title</a></li>" # Add a line of HTML containing the date and title of the article
|
index_file_html+="<li><time>${pub_date}</time> - <a href=\"$file_path\">$title</a></li>" # Add a line of HTML containing the date and title of the article
|
||||||
index_file_html+=$'\n'
|
index_file_html+=$'\n'
|
||||||
@@ -204,7 +257,7 @@ gen_index_page() { # Generate an index page (site map) that includes links to th
|
|||||||
echo "$index_file_html" > "$path_for_output" # Output variable to file
|
echo "$index_file_html" > "$path_for_output" # Output variable to file
|
||||||
|
|
||||||
add_header_and_footer_to_index "$path_for_output" # Add header and footer to index file
|
add_header_and_footer_to_index "$path_for_output" # Add header and footer to index file
|
||||||
sed -i 's/[\$][\$]title[\$][\$]/Site Map - Two More Cents/g' "$path_for_output" # Replace title variable with 'site map' title
|
sed -i 's/[\$][\$]title[\$][\$]/Site Map/g' "$path_for_output" # Replace title variable with 'site map' title
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user