feat: Major initialization and configuration improvements

- Renamed config file to site.conf with backward compatibility
- Added robust config file loading with error handling
- Implemented blog index caching system
- Added configuration validation for required settings
- Improved debug output and error messages
- Added directory creation for project and site roots
- Added git repository detection to prevent accidental runs in wrong directories
- Updated README.md to reflect configuration changes
- Bumped version to 0.5.0
This commit is contained in:
Stig-Ørjan Smelror 2025-05-18 12:37:54 +02:00
parent d9dc9c47e5
commit f5e33cb424
2 changed files with 143 additions and 34 deletions

View File

@ -30,9 +30,10 @@ You cannot mix native and Markdown when creating your projects.
Copy ```qsgen2``` and the directory ```include``` to ${HOME}/bin/. Copy ```qsgen2``` and the directory ```include``` to ${HOME}/bin/.
Then create you project directory, for example ```${HOME}/sites/new-site```. Then create you project directory, for example ```${HOME}/sites/new-site```.
Copy the file ```config``` and the directory ```themes``` to your project directory. Copy the file ```site.conf``` (or ```config``` for backward compatibility) and the directory ```themes``` to your project directory.
You first have to configure your site and this is done in the file [config](config). You first have to configure your site and this is done in the file [site.conf](site.conf).
You can use the provided ```config.example``` as a template.
Do the necessary changes. Do the necessary changes.
Create a directory named ```blog``` if you want to have blogs on your site. Create a directory named ```blog``` if you want to have blogs on your site.
@ -72,7 +73,7 @@ The themes engine is not 100% complete because there still are some static text
#### Blog Index #### Blog Index
There is an option in the file ```config``` if you want to show the blog on the front page or not. If set to false, it will write a file in ```$www_root/blog/index.html``` that contains, in reverse order, the blogs written. There is an option in the file ```site.conf``` if you want to show the blog on the front page or not. If set to false, it will write a file in ```$www_root/blog/index.html``` that contains, in reverse order, the blogs written.
To do this it uses the file ```blog_list.tpl``` as a template for how to format this list and then it inserts this into the file ```blog_index.tpl``` which then is written to disk. To do this it uses the file ```blog_list.tpl``` as a template for how to format this list and then it inserts this into the file ```blog_index.tpl``` which then is written to disk.

166
qsgen2
View File

@ -16,7 +16,7 @@
############################################################################### ###############################################################################
############################################################################### ###############################################################################
VERSION="0.4.4" # Sat-2024-07-10 VERSION="0.5.0" # Sun-2025-05-18
QSGEN="Quick Site Generator 2" QSGEN="Quick Site Generator 2"
# Set to true or false # Set to true or false
@ -36,14 +36,33 @@ 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}"
# Check for, and source, the config file for this specific website # Check for, and source, the config file for this specific website
if [[ -f $(pwd)/config ]]; then config_loaded=false
if (${globaldebug}); then echo "${red}Config file found and sourced${end}\n${yellow} - $(pwd)/config${end}"; fi if [[ -f $(pwd)/site.conf ]]; then
zini $(pwd)/config if (${globaldebug}); then _msg debug "Config file found and sourced: $(pwd)/site.conf"; fi
else if zini $(pwd)/site.conf; then
echo "${red}Cannot find configuration file.${end}" config_loaded=true
echo "${yellow} - Please create the file 'config' in your project directory.${end}" else
echo "${yellow} - See 'config.example' in the git source tree.${end}" _msg info "Failed to load site.conf"
exit fi
fi
# Fallback to 'config' if site.conf not found or failed to load
if [[ ${config_loaded} == false && -f $(pwd)/config ]]; then
if (${globaldebug}); then _msg debug "Legacy config file found and sourced: $(pwd)/config"; fi
_msg info "Warning: Using legacy 'config' file. Consider renaming to 'site.conf'"
if zini $(pwd)/config; then
config_loaded=true
else
_msg info "Failed to load legacy config file"
fi
fi
# Exit if no config file was loaded
if [[ ${config_loaded} == false ]]; then
_msg error "Cannot find or load configuration file."
_msg info "Please create 'site.conf' in your project directory."
_msg info "You can use 'config.example' as a template."
exit 1
fi fi
# Load language as defined in config # Load language as defined in config
@ -63,13 +82,24 @@ if [[ ${lang_found} == "false" ]]; then
source "${HOME}/bin/include/qsgen2/lang/en_US" source "${HOME}/bin/include/qsgen2/lang/en_US"
fi fi
# Debug: Show loaded configuration
if (${globaldebug}); then if (${globaldebug}); then
qsconfig=$( cat $(pwd)/config | grep -v \# | awk '{print substr($0, index($0, " ") + 1)}' ) _msg debug "=== Loaded Configuration ==="
echo "Content of Config file" for key value in ${(kv)config}; do
for qslines in ${qsconfig} _msg debug "${key}: ${value}"
do done
echo "${yellow}${qslines}${end}" _msg debug "==========================="
# Also show raw config file content
local config_file="$(pwd)/site.conf"
[[ ! -f "${config_file}" ]] && config_file="$(pwd)/config"
if [[ -f "${config_file}" ]]; then
_msg debug "=== Raw Config File ==="
cat "${config_file}" | grep -v '^\s*#' | while read -r line; do
_msg debug "${line}"
done done
_msg debug "======================"
fi
fi fi
function _msg() { function _msg() {
@ -123,12 +153,44 @@ fi
blog_cache_file="${config[project_root]}/.blog_cache" blog_cache_file="${config[project_root]}/.blog_cache"
pages_cache_file="${config[project_root]}/.pages_cache" pages_cache_file="${config[project_root]}/.pages_cache"
# Let's check if qsgen2 can generate this site by checking if 'generator' is available # Validate required configuration
if [[ ! ${config[project_generator]} ]] || [[ -d $(pwd)/.git ]]; then required_configs=(
_msg debug "_qsgen2_msg_3" "project_generator"
exit "project_root"
"site_root"
"site_theme"
"site_name"
"site_url"
"project_lang"
)
# Check for missing required configurations
missing_configs=()
for key in ${required_configs[@]}; do
if [[ -z "${config[$key]}" ]]; then
missing_configs+=("$key")
fi
done
if [[ ${#missing_configs[@]} -gt 0 ]]; then
_msg error "Missing required configuration values:"
for key in ${missing_configs[@]}; do
_msg error "- $key"
done
exit 1
fi fi
# Check if we're in a git repository
if [[ -d $(pwd)/.git ]]; then
_msg info "Warning: Running in a git repository directory. Make sure this is intended."
_msg info "If you want to generate the site, run from the project root directory."
exit 1
fi
# Create necessary directories if they don't exist
mkdir -p "${config[project_root]}"
mkdir -p "${config[site_root]}"
# We define the variable 'engine' based on what's in the 'config' file. # We define the variable 'engine' based on what's in the 'config' file.
if [[ ${config[project_generator]} == "native" ]]; then if [[ ${config[project_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
@ -247,6 +309,44 @@ function _list_blogs() {
# BLOG CACHE # BLOG CACHE
blog_cache_file="${config[project_root]}/.blogindex.cache"
function _update_blog_cache() {
# This function updates the blog cache with the current timestamp
if [[ ${globaldebug} == "true" ]]; then
local debug=true
else
local debug=false
fi
if (${debug}) _msg debug "${0:t}_msg_1" " ${blog_cache_file}"
date +%s > "${blog_cache_file}"
}
function _is_blog_cache_stale() {
# Returns 0 (success) if cache is stale or doesn't exist, 1 (failure) otherwise
if [[ ${globaldebug} == "true" ]]; then
local debug=true
else
local debug=false
fi
if [[ ! -f "${blog_cache_file}" ]]; then
if (${debug}) _msg debug "${0:t}_msg_2" " ${blog_cache_file}"
return 0
fi
local cache_time=$(<"${blog_cache_file}")
local current_time=$(date +%s)
# Consider cache stale if it's older than 1 hour (3600 seconds)
if (( current_time - cache_time > 3600 )); then
if (${debug}) _msg debug "${0:t}_msg_3" " ${blog_cache_file}"
return 0
fi
return 1
}
function _blog_cache() { function _blog_cache() {
if [[ ${globaldebug} == "true" ]]; then if [[ ${globaldebug} == "true" ]]; then
@ -853,6 +953,7 @@ function _blog_idx_for_index() {
if (${debug}) _msg debug "${0:t}_msg_8" " ${config[project_root]}/blog/index.tmp.html" if (${debug}) _msg debug "${0:t}_msg_8" " ${config[project_root]}/blog/index.tmp.html"
#if (${debug}) _msg debug "${0:t}_msg_9" " ${blog_list_content}" #if (${debug}) _msg debug "${0:t}_msg_9" " ${blog_list_content}"
echo ${blog_list_content} > ${config[project_root]}/blog/index.tmp.html echo ${blog_list_content} > ${config[project_root]}/blog/index.tmp.html
_update_blog_cache
} }
@ -864,14 +965,10 @@ function _blog_index() {
local debug=false local debug=false
fi fi
# This function generates the www_root/blog/index.html file that gets its data from _blog_list_for_index() # Always update if we have new/updated blogs or cache is stale
# ${new_updated_blogs} comes from the function _blogs if anything new or updated is detected if [[ ${new_updated_blogs} == "true" ]] || _is_blog_cache_stale; then
if [[ ${config[site_blog]} == "false" ]] && [[ ${new_updated_blogs} = "true" ]]; then
if (${debug}) _msg debug "${0:t}_msg_1" "${config[site_blog]}" if (${debug}) _msg debug "${0:t}_msg_1" "${config[site_blog]}"
if (${debug}) _msg debug "${0:t}_msg_2" "${new_updated_blogs}" if (${debug}) _msg debug "${0:t}_msg_2" "${new_updated_blogs}"
if (${debug}) _msg debug "${0:t}_msg_3"
if (${debug}) _msg debug "${0:t}_msg_4" " ${config[site_blog]}"
_msg std "${0:t}_msg_5" " ${config[site_root]}/blog/index.html" _msg std "${0:t}_msg_5" " ${config[site_root]}/blog/index.html"
@ -887,11 +984,15 @@ function _blog_index() {
_msg debug "${0:t}_msg_8" " ${config[site_root]}/blog/index.html" _msg debug "${0:t}_msg_8" " ${config[site_root]}/blog/index.html"
_msg debug "${0:t}_msg_9" " ${#blog_index_content}" _msg debug "${0:t}_msg_9" " ${#blog_index_content}"
fi fi
echo "$blog_index_content" > ${config[site_root]}/blog/index.html
_f_last_updated ${config[site_root]}/blog/index.html
# Create blog directory if it doesn't exist
mkdir -p "${config[site_root]}/blog"
echo "$blog_index_content" > "${config[site_root]}/blog/index.html"
_f_last_updated "${config[site_root]}/blog/index.html"
# Update the cache
_update_blog_cache
fi fi
} }
function _add_blog_list_to_index() { function _add_blog_list_to_index() {
@ -918,8 +1019,12 @@ function _sitemap() {
local debug=false local debug=false
fi 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, Pages, or if cache is stale
if ([[ ${config[site_sitemap]} == "true" ]] && ( [[ ${new_updated_blogs} == "true" ]] || [[ ${new_updated_pages} == "true" ]] )) || [[ ${sitemap_force} == "true" ]]; then if ([[ ${config[site_sitemap]} == "true" ]] &&
([[ ${new_updated_blogs} == "true" ]] ||
[[ ${new_updated_pages} == "true" ]] ||
_is_blog_cache_stale)) ||
[[ ${sitemap_force} == "true" ]]; then
setopt extendedglob setopt extendedglob
@ -1003,6 +1108,9 @@ function _sitemap() {
echo '</urlset>' >> "${sitemap_page}" echo '</urlset>' >> "${sitemap_page}"
_msg std " - ${p_file}" _msg std " - ${p_file}"
# Update the blog cache after generating sitemap
_update_blog_cache
if (${debug}); then _msg debug "${0:t}_msg_2" " ${sitemap_file}"; fi if (${debug}); then _msg debug "${0:t}_msg_2" " ${sitemap_file}"; fi
# Start of the XML file for the main sitemap # Start of the XML file for the main sitemap