Created a new global function, zini, the Zsh INI reader. New breaking changes to config.example. Updated qsgen2 using zini and mapped old values to the new ones temporarily.

This commit is contained in:
Stig-Ørjan Smelror 2024-02-19 19:18:46 +01:00
parent 22be2e4a6d
commit 8b28ce9a96
3 changed files with 80 additions and 24 deletions

View File

@ -1,24 +1,26 @@
# Place this file in your project directory ; Place this file in your project directory
# This is the name of your site [site]
export site_name="The Site Name" ; This is the name of your site
# This is the site tag or subtitle name = "The Site Name"
export site_tagline="The Site Tagline" ; This is the tagline
export site_url="http://www.example.com" tagline = "The Site Tagline"
# At the moment, there is only 1 theme - minimal ; This is the URL of your site
export theme=minimal url = "https://www.example.com"
# This is the directory where you create your pages and blogs. ; This is where your HTML files go
# Your project working directory root = /path/to/www/dir
export project_dir=/path/to/your/project ; The theme of your site
# This is where qsgen2 outputs the parsed and generated files theme = theme_name
export www_root=/path/to/output/directory ; sitemap or not: true or false
# Do you want the blog on your front page or not? sitemap = true
# Values are true for yes and false for no ; Do you want the blog to appear on the front page
export blog_in_index=false ; true = yes and false = no
# Expected values are 'native' or 'markdown' blog = true
export generator=native
# Do you want to generate a sitemap.xml file? [project]
export sitemap=true ; This is where you work before you generate the outpu
# Language for messages from qsgen2 root = /path/to/working/project
# en_US, es_ES, nb_NO ; Languages: en_US, en_UK, es_ES, nb_NO, fr_FR
export language=nb_NO lang = en_US
; Use QStags (native) or Markdown (markdown)
generator = native

39
include/common/zini Normal file
View File

@ -0,0 +1,39 @@
# zini function to parse INI files and store their content in an associative array
zini() {
local ini_path="$1"
typeset -gA config
# Check if the file exists
if [[ ! -f "$ini_path" ]]; then
echo "Configuration file not found: $ini_path"
return 1
fi
local current_section=""
local line key value composite_key
# Read the INI file line by line
while IFS= read -r line || [[ -n $line ]]; do
line=$(echo $line | xargs) # Trim whitespace
# Skip empty lines and comments
[[ -z "$line" || "$line" == \;* ]] && continue
# Detect section headers
if [[ "$line" == \[*\]* ]]; then
current_section="${line:1:-1}"
else
# Parse key-value pairs
key=${line%%=*}
value=${line#*=}
key=$(echo $key | xargs) # Trim key
value=$(echo $value | xargs) # Trim value
# Store in associative array with 'section_key' format
composite_key="${current_section}_${key}"
config[$composite_key]="$value"
fi
done < "$ini_path"
# echo "Configuration loaded."
}

17
qsgen2
View File

@ -28,6 +28,7 @@ globaldebug=false
fpath=(${HOME}/bin/include/common ${HOME}/bin/include/qsgen2/lang $fpath) fpath=(${HOME}/bin/include/common ${HOME}/bin/include/qsgen2/lang $fpath)
# In this case, let's load the 'include' function # In this case, let's load the 'include' function
autoload include autoload include
autoload zini
# Including some colors to the script # Including some colors to the script
include common/colors include common/colors
@ -37,7 +38,8 @@ echo "${magenta}${blue_bg} ${QSGEN} ${end}${bold_white}${blue_bg}${VERSION} ${en
# 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 if [[ -f $(pwd)/config ]]; then
if (${globaldebug}); then echo "${red}Config file found and sourced${end}\n${yellow} - $(pwd)/config${end}"; fi if (${globaldebug}); then echo "${red}Config file found and sourced${end}\n${yellow} - $(pwd)/config${end}"; fi
builtin source $(pwd)/config # builtin source $(pwd)/config
zini $(pwd)/config
else else
echo "${red}Cannot find configuration file.${end}" echo "${red}Cannot find configuration file.${end}"
echo "${yellow} - Please create the file 'config' in your project directory.${end}" echo "${yellow} - Please create the file 'config' in your project directory.${end}"
@ -45,6 +47,19 @@ else
exit exit
fi fi
# Let's define the new config values to the old names until
# I've had time to change everything in the script.
site_name="${config[site_name]}"
site_tagline="${config[site_tagline]}"
site_url="${config[site_url]}"
theme="${config[site_theme]}"
sitemap="${config[site_sitemap]}"
www_root="${config[site_root]}"
blog_in_index="${config[site_blog]}"
project_dir="${config[project_root]}"
language="${config[project_lang]}"
generator="${config[project_generator]}"
# Load language as defined in config # Load language as defined in config
typeset -A qsgenlang typeset -A qsgenlang
lang_found=false lang_found=false