qsgen2: Clean up tabs and spaces

This commit is contained in:
Stig-Ørjan Smelror 2024-02-05 20:55:22 +01:00
parent 45d1d31356
commit 535a5ec0b8

70
qsgen2
View File

@ -40,6 +40,7 @@ include common/colors
echo "${magenta}${blue_bg} ${QSGEN} ${end}${bold_white}${blue_bg}${VERSION} ${end}" echo "${magenta}${blue_bg} ${QSGEN} ${end}${bold_white}${blue_bg}${VERSION} ${end}"
_add_site() { _add_site() {
local site_name="${1}" local site_name="${1}"
local project_dir="${2}" local project_dir="${2}"
local sites_file="${HOME}/.config/qsgen2/sites.qsc" local sites_file="${HOME}/.config/qsgen2/sites.qsc"
@ -65,9 +66,11 @@ _add_site() {
# Add the site to the sites file # Add the site to the sites file
echo "${site_number}|${site_name}|${project_dir}" >> "${sites_file}" echo "${site_number}|${site_name}|${project_dir}" >> "${sites_file}"
echo "Site '${site_name}' added successfully." echo "Site '${site_name}' added successfully."
} }
_list_sites() { _list_sites() {
local config_dir="${HOME}/.config/qsgen2" local config_dir="${HOME}/.config/qsgen2"
local sites_file="${config_dir}/sites.qsc" local sites_file="${config_dir}/sites.qsc"
@ -84,6 +87,7 @@ _list_sites() {
else else
echo "No sites registered." echo "No sites registered."
fi fi
} }
@ -340,6 +344,7 @@ function _pages_cache() {
# If you want to debug this function only, set this to true # If you want to debug this function only, set this to true
local debug=false local debug=false
fi fi
# Create an associative array for the pages cache # Create an associative array for the pages cache
typeset -A pages_cache typeset -A pages_cache
@ -368,6 +373,7 @@ function _pages_cache() {
if [[ ${pages_cache[$file]} != "$current_hash" ]]; then if [[ ${pages_cache[$file]} != "$current_hash" ]]; then
if (${debug}) _msg debug "2. _pages_cache: pages_file: ${pages_cache[$file]}" if (${debug}) _msg debug "2. _pages_cache: pages_file: ${pages_cache[$file]}"
if (${debug}) _msg debug "2. _pages_cache: current_cache: ${current_hash}" if (${debug}) _msg debug "2. _pages_cache: current_cache: ${current_hash}"
# Pages file is new or has changed; add it to the processing array # Pages file is new or has changed; add it to the processing array
pages_array+=("$file") pages_array+=("$file")
@ -406,6 +412,7 @@ function _last_updated() {
# Return the updated content # Return the updated content
echo "${updated_content}" echo "${updated_content}"
} }
function _f_last_updated() { function _f_last_updated() {
@ -472,9 +479,9 @@ function _pages() {
# If pages_array is not empty, we do work # If pages_array is not empty, we do work
if (${debug}) _msg debug "_pages: pages_array is not empty" if (${debug}) _msg debug "_pages: pages_array is not empty"
for pages_in_array in ${pages_array[@]} for pages_in_array in ${pages_array[@]}
do do
if (${debug}) _msg debug "_pages: Setting Pages template" if (${debug}) _msg debug "_pages: Setting Pages template"
local pages=${project_dir}/themes/${theme}/pages.tpl local pages=${project_dir}/themes/${theme}/pages.tpl
@ -564,7 +571,6 @@ function _pages() {
export new_updated_pages=true export new_updated_pages=true
else else
# Insert the blog to the front page is blog_in_index is true and the file in the array is index.file_ext # Insert the blog to the front page is blog_in_index is true and the file in the array is index.file_ext
# and if index.tmp.html exist and is not empty # and if index.tmp.html exist and is not empty
if [[ ${blog_in_index} == "true" && -s "${project_dir}/blog/index.tmp.html" ]]; then if [[ ${blog_in_index} == "true" && -s "${project_dir}/blog/index.tmp.html" ]]; then
@ -614,7 +620,6 @@ function _blogs() {
BLOG_META_STR_ARRAY=() BLOG_META_STR_ARRAY=()
# Regular blog creation process # Regular blog creation process
#if [[ ! ${mode} ]]; then
if [[ -f ${project_dir}/themes/${theme}/blogs.tpl ]]; then if [[ -f ${project_dir}/themes/${theme}/blogs.tpl ]]; then
local blog_tpl=$(<"${project_dir}/themes/${theme}/blogs.tpl") local blog_tpl=$(<"${project_dir}/themes/${theme}/blogs.tpl")
@ -716,6 +721,7 @@ function _blogs() {
_msg sub "- No new or updated Blogs found" _msg sub "- No new or updated Blogs found"
export new_updated_blogs=false export new_updated_blogs=false
fi fi
if [[ ${new_updated_blogs} == "true" ]]; then if [[ ${new_updated_blogs} == "true" ]]; then
if (${debug}) _msg sub "* _blogs: Running _blog_idx_for_index" if (${debug}) _msg sub "* _blogs: Running _blog_idx_for_index"
_blog_idx_for_index _blog_idx_for_index
@ -759,8 +765,8 @@ function _blog_idx_for_index() {
# Iterate over each component and extract information # Iterate over each component and extract information
if (${debug}) _msg debug "_blog_idx_for_index: Iterate over each component and extract information" if (${debug}) _msg debug "_blog_idx_for_index: Iterate over each component and extract information"
for component in "${meta_array[@]}"; do for component in "${meta_array[@]}"
do
case "${component}" in case "${component}" in
SDATE:*) sdate=${component#SDATE: } ;; SDATE:*) sdate=${component#SDATE: } ;;
BTITLE:*) btitle=${component#BTITLE: } ;; BTITLE:*) btitle=${component#BTITLE: } ;;
@ -789,6 +795,7 @@ function _blog_idx_for_index() {
") ")
done done
unset sdate btitle ingress url unset sdate btitle ingress url
if (${debug}) _msg debug "_blog_idx_for_index: Running ${engine} on content to catch QStags in Ingress" if (${debug}) _msg debug "_blog_idx_for_index: Running ${engine} on content to catch QStags in Ingress"
@ -842,17 +849,22 @@ function _add_blog_list_to_index() {
fi fi
# Let's find the file 'index.qst' and add the blog if blog_in_index is true # Let's find the file 'index.qst' and add the blog if blog_in_index is true
#if [[ ${new_updated_blogs} == "true" ]] && [[ ${blog_in_index} == "true" ]]; then
if (${debug}) _msg debug "_add_blog_list_to_index: Inserting blog list to index.html" if (${debug}) _msg debug "_add_blog_list_to_index: Inserting blog list to index.html"
local blog_index_list=$(<${project_dir}/blog/index.tmp.html) local blog_index_list=$(<${project_dir}/blog/index.tmp.html)
local site_index_file=$(<${www_root}/index.html) local site_index_file=$(<${www_root}/index.html)
echo "${site_index_file}" | awk -v new_body="${blog_index_list}" '{sub(/BLOGINDEX/, new_body)} 1' > "${www_root}/index.html" echo "${site_index_file}" | awk -v new_body="${blog_index_list}" '{sub(/BLOGINDEX/, new_body)} 1' > "${www_root}/index.html"
#fi
} }
function _sitemap() { function _sitemap() {
if [[ ${globaldebug} == "true" ]]; then
local debug=true
else
# If you want to debug this function only, set this to true
local debug=false
fi
# Check if sitemap is set to true and if there are updated Blogs or Pages before updating the sitemap.xml file. # Check if sitemap is set to true and if there are updated Blogs or Pages before updating the sitemap.xml file.
if [[ ${sitemap} == "true" ]] && ( [[ ${new_updated_blogs} == "true" ]] || [[ ${new_updated_pages} == "true" ]] ); then if [[ ${sitemap} == "true" ]] && ( [[ ${new_updated_blogs} == "true" ]] || [[ ${new_updated_pages} == "true" ]] ); then
@ -874,7 +886,8 @@ if [[ ${sitemap} == "true" ]] && ( [[ ${new_updated_blogs} == "true" ]] || [[ ${
echo '>' >> ${sitemap_file} echo '>' >> ${sitemap_file}
# Add each URL to the sitemap # Add each URL to the sitemap
for file in "${html_files[@]}"; do for file in "${html_files[@]}"
do
# Remove www_root from the path and prepend site_url # Remove www_root from the path and prepend site_url
local url="${site_url}${file#$www_root}" local url="${site_url}${file#$www_root}"
local lastmod=$(stat -c %y "${file}" 2>/dev/null | cut -d' ' -f1,2 | sed 's/ /T/' | sed 's/\..*$//') local lastmod=$(stat -c %y "${file}" 2>/dev/null | cut -d' ' -f1,2 | sed 's/ /T/' | sed 's/\..*$//')
@ -899,8 +912,6 @@ fi
function _link() { function _link() {
# This converts #link tags to actual clickable links in a provided string # This converts #link tags to actual clickable links in a provided string
local content="${1}"
local modified_content=""
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -908,6 +919,9 @@ function _link() {
local debug=false local debug=false
fi fi
local content="${1}"
local modified_content=""
# Process the content line by line # Process the content line by line
echo "${content}" | while IFS= read -r line; do echo "${content}" | while IFS= read -r line; do
if [[ ${line} == *"#link"* ]]; then if [[ ${line} == *"#link"* ]]; then
@ -935,13 +949,12 @@ function _link() {
# Return the modified content # Return the modified content
echo -e "${modified_content}" echo -e "${modified_content}"
} }
function _image() { function _image() {
# This replaces #showimg tags with actual HTML img tags in a provided string # This replaces #showimg tags with actual HTML img tags in a provided string
local content="${1}"
local modified_content=""
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -949,6 +962,9 @@ function _image() {
local debug=false local debug=false
fi fi
local content="${1}"
local modified_content=""
# Process the content line by line # Process the content line by line
echo "${content}" | while IFS= read -r line; do echo "${content}" | while IFS= read -r line; do
if [[ ${line} == *"#showimg"* ]]; then if [[ ${line} == *"#showimg"* ]]; then
@ -978,13 +994,12 @@ function _image() {
# Return the modified content # Return the modified content
echo -e "${modified_content}" echo -e "${modified_content}"
} }
function _youtube() { function _youtube() {
# This embeds a YouTube video in a provided string # This embeds a YouTube video in a provided string
local content="${1}"
local modified_content=""
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -992,6 +1007,9 @@ function _youtube() {
local debug=false local debug=false
fi fi
local content="${1}"
local modified_content=""
# Process the content line by line # Process the content line by line
echo "${content}" | while IFS= read -r line; do echo "${content}" | while IFS= read -r line; do
if [[ ${line} == *"#ytvideo"* ]]; then if [[ ${line} == *"#ytvideo"* ]]; then
@ -1009,12 +1027,12 @@ function _youtube() {
# Return the modified content # Return the modified content
echo -e "${modified_content}" echo -e "${modified_content}"
} }
function _cleanup() { function _cleanup() {
# This removes tags used in the templates that may be left over for some reason # This removes tags used in the templates that may be left over for some reason
local content="${1}"
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -1022,6 +1040,8 @@ function _cleanup() {
local debug=false local debug=false
fi fi
local content="${1}"
if (${debug}) _msg debug "_cleanup: Cleaning up tags in content" if (${debug}) _msg debug "_cleanup: Cleaning up tags in content"
# Perform the cleanup # Perform the cleanup
@ -1035,10 +1055,11 @@ function _cleanup() {
# Return the cleaned content # Return the cleaned content
echo "${cleaned_content}" echo "${cleaned_content}"
} }
function _html() { function _html() {
local content="$1"
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -1046,8 +1067,10 @@ function _html() {
local debug=false local debug=false
fi fi
local content="$1"
if ${debug}; then if ${debug}; then
echo "_html: Converting QStags in content" _msg debug "_html: Converting QStags in content"
fi fi
# Use perl to convert QStags to HTML # Use perl to convert QStags to HTML
@ -1088,18 +1111,19 @@ function _html() {
"#LT", "&lt;", "#GT", "&gt;", "#NUM", "&num;" "#LT", "&lt;", "#GT", "&gt;", "#NUM", "&num;"
); );
} }
for (my $i = 0; $i < $#tags; $i += 2) { for (my $i = 0; $i < $#tags; $i += 2) {
my $tag = $tags[$i]; my $tag = $tags[$i];
my $html = $tags[$i + 1]; my $html = $tags[$i + 1];
s/\Q$tag\E/$html/g; s/\Q$tag\E/$html/g;
} }
' <<< "$content" ' <<< "$content"
} }
function _zhtml() { function _zhtml() {
# This function uses the regex module from Zsh to parse the QStags # This function uses the regex module from Zsh to parse the QStags
local content="$1"
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
local debug=true local debug=true
else else
@ -1107,6 +1131,8 @@ function _zhtml() {
local debug=false local debug=false
fi fi
local content="$1"
if ${debug}; then if ${debug}; then
echo "_html: Converting QStags in content" echo "_html: Converting QStags in content"
fi fi
@ -1150,7 +1176,8 @@ function _zhtml() {
"#LT" "&lt;" "#GT" "&gt;" "#NUM" "&num;" "#LT" "&lt;" "#GT" "&gt;" "#NUM" "&num;"
) )
for tag html (${(kv)tags}); do for tag html (${(kv)tags})
do
# Escape tag for regex use # Escape tag for regex use
local escapedTag=$(printf '%s' "$tag" | sed 's/[].\[^$*]/\\&/g') local escapedTag=$(printf '%s' "$tag" | sed 's/[].\[^$*]/\\&/g')
if [[ "$content" =~ "$escapedTag" ]]; then if [[ "$content" =~ "$escapedTag" ]]; then
@ -1159,12 +1186,9 @@ function _zhtml() {
done done
echo "$content" echo "$content"
} }
# Time to run the first function
_blogs _blogs
# _add_blog_list_to_index
_pages _pages
_sitemap _sitemap