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
export site_name="The Site Name"
# This is the site tag or subtitle
export site_tagline="The Site Tagline"
export site_url="http://www.example.com"
# At the moment, there is only 1 theme - minimal
export theme=minimal
# This is the directory where you create your pages and blogs.
# Your project working directory
export project_dir=/path/to/your/project
# This is where qsgen2 outputs the parsed and generated files
export www_root=/path/to/output/directory
# Do you want the blog on your front page or not?
# Values are true for yes and false for no
export blog_in_index=false
# Expected values are 'native' or 'markdown'
export generator=native
# Do you want to generate a sitemap.xml file?
export sitemap=true
# Language for messages from qsgen2
# en_US, es_ES, nb_NO
export language=nb_NO
[site]
; This is the name of your site
name = "The Site Name"
; This is the tagline
tagline = "The Site Tagline"
; This is the URL of your site
url = "https://www.example.com"
; This is where your HTML files go
root = /path/to/www/dir
; The theme of your site
theme = theme_name
; sitemap or not: true or false
sitemap = true
; Do you want the blog to appear on the front page
; true = yes and false = no
blog = true
[project]
; This is where you work before you generate the outpu
root = /path/to/working/project
; Languages: en_US, en_UK, es_ES, nb_NO, fr_FR
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)
# In this case, let's load the 'include' function
autoload include
autoload zini
# Including some colors to the script
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
if [[ -f $(pwd)/config ]]; then
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
echo "${red}Cannot find configuration file.${end}"
echo "${yellow} - Please create the file 'config' in your project directory.${end}"
@ -45,6 +47,19 @@ else
exit
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
typeset -A qsgenlang
lang_found=false