A first try to get the _pages function working

This commit is contained in:
Stig-Ørjan Smelror 2024-01-29 21:40:26 +01:00
parent f155a67696
commit a8648b126e

168
qsgen2
View File

@ -116,6 +116,13 @@ if (${debug}); then
echo "${yellow} - generator=${generator}${end}" echo "${yellow} - generator=${generator}${end}"
fi fi
# Let's check if qsgen2 can generate this site by checking if 'generator' is available
if [[ ! ${generator} ]]; then
echo "${0:t} cannot parse this site. Exiting."
exit
fi
# We define the variable 'engine' based on what's in the 'config' file.
if [[ ${generator} == "native" ]]; then if [[ ${generator} == "native" ]]; then
# Usage: ${engine} ${1} - Where 1 is the file you want to convert # Usage: ${engine} ${1} - Where 1 is the file you want to convert
engine=_html engine=_html
@ -124,8 +131,15 @@ elif [[ ${generator} == "markdown" ]]; then
echo "Please install the 'discount' package to use Markdown." echo "Please install the 'discount' package to use Markdown."
exit exit
else else
# Usage: ${engine} ${1} ${2} - Where 1 is the input file and 2 is the html www_root file and location # Let's make sure that the Markdown executable gets all its variables: 1 and 2
engine=$( /usr/bin/markdown -o ${2} ${1} ) if [[ ! ${2} ]] || [[ ${2} == "" ]]; then
echo "Engine: To use Markdown, please provide a second variable, the output file."
echo "Engine: Example: engine file.tpl www_root/file.html"
exit
else
# Usage: ${engine} ${1} - Where 1 is the file you want parsed
engine=$( /usr/bin/markdown ${1} -d )
fi
fi fi
fi fi
if (${debug}) echo "${red}Using the ${generator} engine${end}" if (${debug}) echo "${red}Using the ${generator} engine${end}"
@ -160,23 +174,23 @@ function _list_blog() {
# BLOG CACHE # BLOG CACHE
function _blog_cache() { function _blog_cache() {
local debug=false local debug=false
# Create an associative array for the blog cache # Create an associative array for the blog cache
typeset -A blog_cache typeset -A blog_cache
# Load the existing blog cache # Load the existing blog cache
if [[ -f $blog_cache_file ]]; then if [[ -f $blog_cache_file ]]; then
while IFS=':' read -r name hash; do while IFS=':' read -r name hash; do
blog_cache[$name]=$hash blog_cache[$name]=$hash
if (${debug}) echo "${red}HASH VALUE: ${blog_cache[${name}]}${end}" if (${debug}) echo "${red}HASH VALUE: ${blog_cache[${name}]}${end}"
done < "$blog_cache_file" done < "$blog_cache_file"
fi fi
# Initialize the array for storing blog files to process # Initialize the array for storing blog files to process
make_blog_array=() make_blog_array=()
# Process blog files # Process blog files
for blog_file in $(ls -har blog/*.blog); do for blog_file in $(ls -har blog/*.blog); do
# Compute the current blog file hash # Compute the current blog file hash
current_hash=$(md5sum "$blog_file" | awk '{print $1}') current_hash=$(md5sum "$blog_file" | awk '{print $1}')
@ -193,35 +207,36 @@ for blog_file in $(ls -har blog/*.blog); do
# Update the blog cache with the new hash # Update the blog cache with the new hash
blog_cache[$blog_file]=$current_hash blog_cache[$blog_file]=$current_hash
fi fi
done done
# Rebuild the blog cache file from scratch # Rebuild the blog cache file from scratch
: >| "$blog_cache_file" # Truncate the file before writing : >| "$blog_cache_file" # Truncate the file before writing
for name in "${(@k)blog_cache}"; do for name in "${(@k)blog_cache}"; do
echo "$name:${blog_cache[$name]}" >> "$blog_cache_file" echo "$name:${blog_cache[$name]}" >> "$blog_cache_file"
done done
} }
# PAGES CACHE # PAGES CACHE
# Returns the array pages_array()
function _pages_cache() { function _pages_cache() {
local debug=false local debug=false
# Create an associative array for the pages cache # Create an associative array for the pages cache
typeset -A pages_cache typeset -A pages_cache
# Load the existing pages cache # Load the existing pages cache
if [[ -f $pages_cache_file ]]; then if [[ -f $pages_cache_file ]]; then
while IFS=':' read -r name hash; do while IFS=':' read -r name hash; do
pages_cache[$name]=$hash pages_cache[$name]=$hash
if (${debug}) echo "${red}PAGES HASH VALUE: ${pages_cache[${name}]}${end}" if (${debug}) echo "${red}PAGES HASH VALUE: ${pages_cache[${name}]}${end}"
done < "$pages_cache_file" done < "$pages_cache_file"
fi fi
# Initialize the array for storing pages files to process # Initialize the array for storing pages files to process
tpl_array=() pages_array=()
# Process pages files # Process pages files
for file in $(ls -1bt *tpl); do for file in $(ls -1bt *tpl); do
# Compute the current blog file hash # Compute the current blog file hash
current_hash=$(md5sum "$file" | awk '{print $1}') current_hash=$(md5sum "$file" | awk '{print $1}')
@ -233,34 +248,117 @@ for file in $(ls -1bt *tpl); do
if (${debug}) echo "${red}2. pages_file: ${pages_cache[$file]}${end}" if (${debug}) echo "${red}2. pages_file: ${pages_cache[$file]}${end}"
if (${debug}) echo "${red}2. current_cache: ${current_hash}${end}" if (${debug}) echo "${red}2. current_cache: ${current_hash}${end}"
# 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
tpl_array+=("$file") pages_array+=("$file")
# Update the pages cache with the new hash # Update the pages cache with the new hash
pages_cache[$file]=$current_hash pages_cache[$file]=$current_hash
fi fi
done done
# Rebuild the pages cache file from scratch # Rebuild the pages cache file from scratch
: >| "$pages_cache_file" # Truncate the file before writing : >| "$pages_cache_file" # Truncate the file before writing
for name in "${(@k)pages_cache}"; do for name in "${(@k)pages_cache}"; do
echo "$name:${pages_cache[$name]}" >> "$pages_cache_file" echo "$name:${pages_cache[$name]}" >> "$pages_cache_file"
done done
} }
function _last_updated() { function _last_updated() {
tee < ${1} | sed -e "s|#updated|${TODAY}|" | sed -e "s|\#version|${QSGEN} ${VERSION}|" > ${1} tee < ${1} | sed -e "s|#updated|${TODAY}|" | sed -e "s|\#version|${QSGEN} ${VERSION}|" > ${1}
} }
function _file_to_lower() {
local filename=${1}
# Replace spaces with dashes
filename="${filename// /-}"
# Convert to lowercase and remove invalid characters
filename=$(echo "${filename}" | sed -e 's/^[^a-zA-Z0-9_.]+//g' -e 's/[^a-zA-Z0-9_-]+/-/g')
return ${filename}
}
function _pages() { function _pages() {
# This function generates all the pages # This function generates all the new and updated Pages
local debug=false local debug=false
local pages=${project_dir}/templates/${theme}/pages.tpl local pages=${project_dir}/templates/${theme}/pages.tpl
# Let's check if we can access the pages.tpl file.
# It not, exit script.
if [[ ! -f ${pages} ]]; then if [[ ! -f ${pages} ]]; then
echo "Unable to find the Pages template: ${pages}" echo "Unable to find the Pages template: ${pages}"
exit exit
else
# Read template once
pages_tpl="$(<${pages})"
fi
# Load the cache for Pages
_pages_cache
if (( ${#pages_array[@]} > 0 )); then
# If pages_array is not empty, we do work
local out="${www_root}"
for pages_in_array in ${pages_array[@]}
do
echo "${green}Generating Page: ${pages_in_array}${end}"
# Read the file once
local page_content="$(<${pages_in_array})"
# Grab the title from the Page
page_title=$( echo ${page_content} | head -2 | grep '#title' | cut -d= -f2 )
# Remove the #title line from the buffer. No longer needed.
page_content=$( echo ${page_content} | grep -v #title )
# HTML'ify the page content
page_content=$( ${engine} ${page_content} )
# Look for links, images and videos and convert them if present.
if [[ $( grep \#link ${page_content} ) ]]; then
echo "If #link is present, run _link: ${page_content}"
_link ${page_content}
elif [[ $( grep \#showimg ${page_content} ) ]]; then
echo "If #showimg is present, run _image: ${page_content}"
_image ${page_content}
elif [[ $( grep \#ytvideo ${page_content} ) ]]; then
echo "If #ytvideo is present, run _youtube: ${page_content}"
_youtube ${page_content}
fi
# Insert page_content into pages_tpl by replacing the BODY tag present there
page_tpl=$( echo ${page_tpl} | sed -e "s|BODY|${page_content}|" )
# Replace every #pagetitle in pages_tpl
pages_tpl=$( echo ${pages_tpl} | sed -e "s|#pagetitle|${page_title}|g" )
# Replace every #tagline in pages_tpl
pages_tpl=$( echo ${pages_tpl} | sed -e "s|#tagline|${site_tagline}|g" )
# Replace #updated with today's date
pages_tpl=$( _last_updated ${pages_tpl} )
# Replace #version with the Name and Version of the script
pages_tpl=$( echo ${pages_tpl} | sed -e "s|#version|${QSGEN} ${VERSION}|" )
# Run a cleanup if in case something was left out
_cleanup ${pages_tpl}
# Write pages_tpl to disk
echo "Writing ${pages_in_array} to disk."
# Always use lowercase for file names
pages_title_lower=$( _file_to_lower "${page_title}" )
echo "${pages_tpl}" > ${www_root}/${pages_title_lower%.}.html
done
fi fi
} }
@ -290,7 +388,7 @@ function _blogs() {
local sdate btitle ingress body blog_index blog_dir blog_url local sdate btitle ingress body blog_index blog_dir blog_url
echo "_blogs: _blog_list_for_index: Just before the for loop: make_blog_array" if (${debug}) echo "_blogs: _blog_list_for_index: Just before the for loop: make_blog_array"
for blog in ${make_blog_array[@]} for blog in ${make_blog_array[@]}
do do
if (${debug}) echo "0001: ${red}_blogs: _blog_list_for_index: Processing ${blog}${end}" if (${debug}) echo "0001: ${red}_blogs: _blog_list_for_index: Processing ${blog}${end}"