Add comprehensive theme documentation and improve migration script

- Add THEMES-HOWTO.md: Complete guide for creating and customizing themes
- Remove theme sections from how-it-works.md to avoid duplication
- Update migration script to place all blog posts in single directory
- Streamline documentation structure for better organization
This commit is contained in:
2025-05-31 03:00:50 +02:00
parent f72fe18873
commit 81ffa53d70
4 changed files with 971 additions and 765 deletions

View File

@ -620,11 +620,13 @@ _clean_output_dir() {
_log INFO "Found preserve file: $preserve_file"
_log DEBUG "Performing selective cleaning with file preservation"
# Create temporary directory to store preserved files
local temp_preserve_dir=$(mktemp -d)
# Use in-memory arrays to store file paths and content
local files_to_preserve=()
local preserved_file_paths=()
local preserved_file_contents=()
local files_preserved=0
# Read preserve patterns and backup matching files
# Read preserve patterns and collect matching files
while IFS= read -r pattern || [[ -n "$pattern" ]]; do
# Skip empty lines and comments
[[ -z "$pattern" || "$pattern" == \#* ]] && continue
@ -640,17 +642,16 @@ _clean_output_dir() {
if [[ -f "$file" ]]; then
# Get relative path from output directory
local rel_path="${file#$output_dir/}"
local preserve_path="$temp_preserve_dir/$rel_path"
# Create directory structure in temp location
mkdir -p "$(dirname "$preserve_path")"
# Copy file to preserve location
if cp "$file" "$preserve_path"; then
# Read file content into memory
local file_content
if file_content=$(<"$file"); then
preserved_file_paths+=("$rel_path")
preserved_file_contents+=("$file_content")
files_preserved=$((files_preserved + 1))
_log DEBUG "Preserved file: $rel_path"
_log DEBUG "Preserved file in memory: $rel_path"
else
_log WARNING "Failed to preserve file: $rel_path"
_log WARNING "Failed to read file for preservation: $rel_path"
fi
fi
done < <(find "$output_dir" -name "$pattern" -type f -print0 2>/dev/null)
@ -658,30 +659,37 @@ _clean_output_dir() {
done < "$preserve_file"
# Remove the output directory
_log DEBUG "Removing output directory contents (preserving $files_preserved files)"
_log DEBUG "Removing output directory contents (preserving $files_preserved files in memory)"
rm -rf "$output_dir"
# Recreate output directory
mkdir -p "$output_dir"
if [[ $? -ne 0 ]]; then
_log ERROR "Failed to recreate output directory: $output_dir"
rm -rf "$temp_preserve_dir"
return 1
fi
# Restore preserved files
# Restore preserved files from memory
if [[ $files_preserved -gt 0 ]]; then
_log DEBUG "Restoring $files_preserved preserved files"
if [[ -d "$temp_preserve_dir" ]]; then
# Copy preserved files back, maintaining directory structure
(cd "$temp_preserve_dir" && find . -type f -exec cp --parents {} "$output_dir/" \; 2>/dev/null)
_log INFO "Restored $files_preserved preserved files"
fi
_log DEBUG "Restoring $files_preserved preserved files from memory"
for ((i=0; i<${#preserved_file_paths[@]}; i++)); do
local rel_path="${preserved_file_paths[i]}"
local file_content="${preserved_file_contents[i]}"
local full_path="$output_dir/$rel_path"
# Create directory structure if needed
mkdir -p "$(dirname "$full_path")"
# Write file content back
if printf '%s' "$file_content" > "$full_path"; then
_log DEBUG "Restored file from memory: $rel_path"
else
_log WARNING "Failed to restore file from memory: $rel_path"
fi
done
_log INFO "Restored $files_preserved preserved files from memory"
fi
# Clean up temporary directory
rm -rf "$temp_preserve_dir"
else
# No preserve file - do complete cleaning as before
_log DEBUG "No preserve file found - performing complete cleaning"
@ -794,14 +802,6 @@ _copy_static_files() {
_generate_index_page() {
_log DEBUG "Entered _generate_index_page"
local temp_yaml_file
temp_yaml_file=$(mktemp -t qsgen3_index_yaml.XXXXXX)
if [[ $? -ne 0 || -z "$temp_yaml_file" || ! -f "$temp_yaml_file" ]]; then
_log ERROR "Failed to create temporary file for index YAML. Exiting."
return 1
fi
trap "_log DEBUG \"Cleaning up temporary index YAML file: ${temp_yaml_file}\"; rm -f \"${temp_yaml_file}\"; trap - EXIT INT TERM HUP" EXIT INT TERM HUP
_log DEBUG "Created temporary YAML file for index: $temp_yaml_file"
_log INFO "Generating index page..."
local content_posts_dir="${QSG_CONFIG[paths_content_dir]}/posts"
local output_index_file="${QSG_CONFIG[paths_output_dir]}/index.html"
@ -892,119 +892,87 @@ _generate_index_page() {
if [[ ${#sorted_posts[@]} -eq 0 ]]; then
_log DEBUG "No posts found to add to index YAML."
yaml_lines+=( "$(printf ' [] # Explicitly empty list')" )
yaml_lines+=( "$(printf ' [] # Explicit empty list for posts')" )
else
for detail_line in "${sorted_posts[@]}"; do
# Original detail_line format: "${sort_key}|${title}|${post_url}|${date:-N/A}|${summary:-}"
# Using Zsh specific split for robustness with special characters in fields
local parts_array=( ${(s[|])detail_line} )
local post_title_val="${parts_array[2]}"
local post_url_val="${parts_array[3]}"
local post_date_val="${parts_array[4]}"
local post_summary_val="${parts_array[5]}"
for post_line in "${sorted_posts[@]}"; do
# Parse the post_line: sort_key|title|url|date|summary
IFS='|' read -r sort_key title url date summary <<< "$post_line"
yaml_lines+=( "$(printf ' - title: "%s"' "$(_yaml_escape_val "$post_title_val")")" )
yaml_lines+=( "$(printf ' url: "%s"' "$(_yaml_escape_val "$post_url_val")")" )
yaml_lines+=( "$(printf ' date: "%s"' "$(_yaml_escape_val "$post_date_val")")" )
yaml_lines+=( "$(printf ' summary: "%s"' "$(_yaml_escape_val "$post_summary_val")")" )
yaml_lines+=( "$(printf ' - post_title: "%s"' "$(_yaml_escape_val "$title")")" )
yaml_lines+=( "$(printf ' post_url: "%s"' "$(_yaml_escape_val "$url")")" )
yaml_lines+=( "$(printf ' post_date: "%s"' "$(_yaml_escape_val "$date")")" )
yaml_lines+=( "$(printf ' post_summary: "%s"' "$(_yaml_escape_val "$summary")")" )
done
fi
local temp_yaml_content
temp_yaml_content="$(IFS=$'\n'; echo "${yaml_lines[*]}")"
temp_yaml_content+=$'\n' # Ensure a final trailing newline for the whole block
_log DEBUG "Generated comprehensive YAML for index (first 300 chars):\n${temp_yaml_content:0:300}..."
# Join YAML lines into a single string
local yaml_content
OIFS="$IFS"; IFS=$'\n'
yaml_content="${yaml_lines[*]}"
IFS="$OIFS"
# Write YAML to temporary file
_log DEBUG "Full comprehensive YAML for index before writing to file (raw, first 1000 chars to avoid excessive logging):\n${temp_yaml_content:0:1000}"
_log DEBUG "Generated YAML content for index (first 300 chars): ${yaml_content:0:300}..."
if ! printf '%s' "$temp_yaml_content" > "$temp_yaml_file"; then
_log ERROR "Failed to write comprehensive YAML to temporary file: $temp_yaml_file"
return 1 # Trap will clean up temp_yaml_file
fi
_log DEBUG "Successfully wrote comprehensive YAML to $temp_yaml_file"
# Build Pandoc command array
local pandoc_cmd_index=("pandoc")
_log DEBUG "In _generate_index_page: PROJECT_ROOT='$PROJECT_ROOT'"
local pandoc_cmd_index=(
pandoc
"--metadata-file" "$temp_yaml_file"
)
# Add CSS if specified (pandoc_css_path_arg is derived in main)
if [[ -n "${QSG_CONFIG[pandoc_css_path_arg]:-}" ]]; then
pandoc_cmd_index+=("--css" "${QSG_CONFIG[pandoc_css_path_arg]}")
_log DEBUG "Using CSS for index: ${QSG_CONFIG[pandoc_css_path_arg]}"
# Add CSS if theme CSS is available
if [[ -n "${QSG_CONFIG[pandoc_css_path_arg]}" ]]; then
local expected_css_file="${QSG_CONFIG[paths_output_dir]}${QSG_CONFIG[pandoc_css_path_arg]}"
if [[ -f "$expected_css_file" ]]; then
pandoc_cmd_index+=("--css" "${QSG_CONFIG[pandoc_css_path_arg]}")
_log DEBUG "Added CSS to index page: ${QSG_CONFIG[pandoc_css_path_arg]}"
else
_log WARNING "Expected CSS file not found: $expected_css_file. CSS will not be linked in index page."
fi
else
_log DEBUG "No CSS specified for index page."
_log DEBUG "No theme CSS path configured. Index page will not include CSS."
fi
# Add remaining Pandoc options for index page
pandoc_cmd_index+=(
"--template=${layout_index_file}"
"--output=${output_index_file}"
"--standalone"
)
_log INFO "Generating $output_index_file using template $layout_index_file with YAML from $temp_yaml_file"
# Add metadata via process substitution instead of temporary file
pandoc_cmd_index+=("--metadata-file" "/dev/stdin")
pandoc_cmd_index+=("--template=${layout_index_file}")
pandoc_cmd_index+=("--output=${output_index_file}")
pandoc_cmd_index+=("--standalone")
_log DEBUG "Pandoc command for index page will be constructed as follows:"
_log DEBUG "Base command: pandoc"
_log DEBUG "CSS arg if theme applies: --css ${QSG_CONFIG[pandoc_css_path_arg]:-(not set or expected CSS file not found in output)}"
_log DEBUG "Metadata file arg: --metadata-file ${temp_yaml_file}"
_log DEBUG "Metadata file arg: --metadata-file /dev/stdin"
_log DEBUG "Template arg: --template=${layout_index_file}"
_log DEBUG "Output arg: --output=${output_index_file}"
_log DEBUG "Other args: --standalone"
_log DEBUG "Final pandoc_cmd_index array: ${(q+)pandoc_cmd_index}"
local pandoc_run_stderr_file
pandoc_run_stderr_file=$(mktemp -t pandoc_stderr_index.XXXXXX)
if [[ -z "$pandoc_run_stderr_file" || ! -e "$pandoc_run_stderr_file" ]]; then # -e to check existence, could be pipe not file
_log CRITICAL "Failed to create temporary file for Pandoc stderr (index). mktemp failed."
# Attempt to clean up the main temp YAML file before exiting, if it was created.
if [[ -n "$temp_yaml_file" && -f "$temp_yaml_file" ]]; then
_log DEBUG "Cleaning up temporary index YAML file ($temp_yaml_file) due to mktemp failure for stderr file."
rm -f "$temp_yaml_file"
fi
return 1
fi
# This temp stderr file is short-lived, trap for it isn't strictly necessary if cleaned up reliably.
local pandoc_exit_code=0
# Execute Pandoc, redirecting its stderr to the temp file
local stderr_content=""
# Execute Pandoc with YAML content piped to stdin, capturing stderr in memory
_log DEBUG "Executing Pandoc command for index page with set -x..."
set -x # Enable command tracing
if "${pandoc_cmd_index[@]}" 2> "$pandoc_run_stderr_file"; then
pandoc_exec_status=0
else
pandoc_exec_status=$?
fi
# Use process substitution to capture stderr without temporary files
exec 3>&1 # Save stdout to fd 3
stderr_content=$( { printf '%s\n' "$yaml_content" | "${pandoc_cmd_index[@]}" 1>&3; } 2>&1 )
pandoc_exit_code=$?
exec 3>&- # Close fd 3
set +x # Disable command tracing
_log DEBUG "Pandoc execution finished. Original exit status from Pandoc: $pandoc_exec_status"
pandoc_exit_code=$pandoc_exec_status # Use the captured status
local stderr_content=""
if [[ -s "$pandoc_run_stderr_file" ]]; then
stderr_content=$(<"$pandoc_run_stderr_file")
fi
rm -f "$pandoc_run_stderr_file"
_log DEBUG "Pandoc execution finished. Original exit status from Pandoc: $pandoc_exit_code"
if [[ $pandoc_exit_code -ne 0 ]]; then
_log ERROR "Pandoc command failed for '$output_index_file' with exit code: $pandoc_exit_code."
if [[ -n "$stderr_content" ]]; then
_log ERROR "Pandoc stderr:\n$stderr_content"
fi
_log ERROR "YAML content passed to Pandoc was in: $temp_yaml_file"
_log ERROR "YAML content dump (first 500 chars):\n$(head -c 500 "$temp_yaml_file" 2>/dev/null || echo 'Failed to read YAML dump')"
_log ERROR "YAML content passed to Pandoc (first 500 chars):\n${yaml_content:0:500}"
return $pandoc_exit_code # Return Pandoc's non-zero exit code
elif [[ -n "$stderr_content" ]]; then
# Pandoc exited 0 but wrote to stderr. Check for known fatal error patterns.
if echo "$stderr_content" | grep -q -iE 'YAML parse exception|template error|could not find|error reading file'; then
_log ERROR "Pandoc reported a critical error for '$output_index_file' (exit code 0). Treating as failure."
_log ERROR "Pandoc stderr:\n$stderr_content"
_log ERROR "YAML content passed to Pandoc was in: $temp_yaml_file"
_log ERROR "YAML content dump (first 500 chars):\n$(head -c 500 "$temp_yaml_file" 2>/dev/null || echo 'Failed to read YAML dump')"
_log ERROR "YAML content passed to Pandoc (first 500 chars):\n${yaml_content:0:500}"
return 1 # Force a failure status
else
_log WARNING "Pandoc succeeded for '$output_index_file' (exit code 0) but produced stderr (non-critical):\n$stderr_content"
@ -1039,7 +1007,7 @@ _process_markdown_files() {
if [[ -z "$source_file" ]]; then continue; fi
_log DEBUG "Processing Markdown file: $source_file"
local relative_path_from_content_root="${source_file#$content_dir/}"
local relative_path_from_content_root="${source_file#${QSG_CONFIG[paths_content_dir]}/}"
if [[ "$content_dir" == "$source_file" ]]; then
relative_path_from_content_root=$(basename "$source_file")
elif [[ "$content_dir" == "/" && "$source_file" == /* ]]; then
@ -1220,14 +1188,6 @@ _process_markdown_files() {
_generate_rss_feed() {
_log DEBUG "Entered _generate_rss_feed"
local temp_rss_yaml_file
temp_rss_yaml_file=$(mktemp -t qsgen3_rss_yaml.XXXXXX)
if [[ $? -ne 0 || -z "$temp_rss_yaml_file" || ! -f "$temp_rss_yaml_file" ]]; then
_log ERROR "Failed to create temporary file for RSS YAML. Exiting."
return 1
fi
trap "_log DEBUG \"Cleaning up temporary RSS YAML file: ${temp_rss_yaml_file}\"; rm -f \"${temp_rss_yaml_file}\"; trap - EXIT INT TERM HUP" EXIT INT TERM HUP
_log DEBUG "Created temporary YAML file for RSS: $temp_rss_yaml_file"
if [[ "${QSG_CONFIG[build_options_generate_rss]}" != "true" ]]; then
_log INFO "RSS feed generation is disabled in configuration. Skipping."
return 0
@ -1316,92 +1276,78 @@ _generate_rss_feed() {
yaml_lines+=( "$(printf 'feed_title: "%s"' "$(_yaml_escape_val "$site_title_for_feed Feed")")" ) # For <title> in RSS
yaml_lines+=( "$(printf 'current_rfc822_date: "%s"' "$(_yaml_escape_val "$current_rfc822_date")")" ) # For <pubDate> of the feed itself
yaml_lines+=( "$(printf 'current_year: "%s"' "$(date +%Y)")" )
yaml_lines+=( "$(printf 'pagetitle: "%s"' "$(_yaml_escape_val "$site_title_for_feed Feed")")" ) # For Pandoc's HTML writer
yaml_lines+=( "$(printf 'posts:')" )
if [[ ${#sorted_posts[@]} -eq 0 ]]; then
_log INFO "No posts found to include in RSS feed after filtering."
yaml_lines+=( "$(printf ' [] # Explicitly empty list')" )
else
for detail_line in "${sorted_posts[@]}"; do
local parts=( ${(s[|])detail_line} ) # Zsh specific split
local p_title="${parts[2]:-}"
local p_url="${parts[3]:-}"
local p_date_rfc822="${parts[4]:-}"
local p_summary="${parts[5]:-}"
yaml_lines+=( "$(printf ' - post_title: "%s"' "$(_yaml_escape_val "$p_title")")" )
yaml_lines+=( "$(printf ' post_url: "%s"' "$(_yaml_escape_val "$p_url")")" )
yaml_lines+=( "$(printf ' post_date_rfc822: "%s"' "$(_yaml_escape_val "$p_date_rfc822")")" )
yaml_lines+=( "$(printf ' post_summary: "%s"' "$(_yaml_escape_val "$p_summary")")" )
# Add other fields like guid if needed, e.g., using post_url as guid
yaml_lines+=( "$(printf ' post_guid: "%s"' "$(_yaml_escape_val "$p_url")")" )
if [[ ${#sorted_posts[@]} -eq 0 ]]; then
_log DEBUG "No posts found to add to RSS YAML."
yaml_lines+=( "$(printf ' [] # Explicit empty list for posts')" )
else
for post_line in "${sorted_posts[@]}"; do
# Parse the post_line: sort_key|title|url|rfc822_date|summary
IFS='|' read -r sort_key title url rfc822_date summary <<< "$post_line"
yaml_lines+=( "$(printf ' - post_title: "%s"' "$(_yaml_escape_val "$title")")" )
yaml_lines+=( "$(printf ' post_url: "%s"' "$(_yaml_escape_val "$url")")" )
yaml_lines+=( "$(printf ' post_date_rfc822: "%s"' "$(_yaml_escape_val "$rfc822_date")")" )
yaml_lines+=( "$(printf ' post_summary: "%s"' "$(_yaml_escape_val "$summary")")" )
done
fi
local temp_yaml_content
temp_yaml_content="$(IFS=$'\n'; echo "${yaml_lines[*]}")"
temp_yaml_content+=$'\n' # Ensure a final trailing newline
# Join YAML lines into a single string
local yaml_content
OIFS="$IFS"; IFS=$'\n'
yaml_content="${yaml_lines[*]}"
IFS="$OIFS"
_log DEBUG "Generated comprehensive YAML for RSS (first 300 chars):\n${temp_yaml_content:0:300}..."
if ! printf '%s' "$temp_yaml_content" > "$temp_rss_yaml_file"; then
_log ERROR "Failed to write comprehensive RSS YAML to temporary file: $temp_rss_yaml_file"
return 1
fi
_log DEBUG "Successfully wrote comprehensive RSS YAML to $temp_rss_yaml_file"
_log DEBUG "Generated YAML content for RSS (first 300 chars): ${yaml_content:0:300}..."
# Build Pandoc command array for RSS
local pandoc_cmd_rss=(
pandoc
"--metadata-file" "$temp_rss_yaml_file"
"pandoc"
"--to" "html5"
"--metadata" "pagetitle=RSS Feed"
"--metadata-file" "/dev/stdin"
"--template=${layout_rss_file}"
"--output=${output_rss_file}"
"--to" "html5" # Use HTML5 writer for custom XML template processing
"--standalone"
)
_log INFO "Generating $output_rss_file using template $layout_rss_file with YAML from $temp_rss_yaml_file"
local pandoc_run_rss_stderr_file
pandoc_run_rss_stderr_file=$(mktemp -t pandoc_stderr_rss.XXXXXX)
if [[ -z "$pandoc_run_rss_stderr_file" || ! -e "$pandoc_run_rss_stderr_file" ]]; then
_log CRITICAL "Failed to create temporary file for Pandoc stderr (RSS). mktemp failed."
if [[ -n "$temp_rss_yaml_file" && -f "$temp_rss_yaml_file" ]]; then
_log DEBUG "Cleaning up temporary RSS YAML file ($temp_rss_yaml_file) due to mktemp failure for stderr file."
rm -f "$temp_rss_yaml_file"
fi
return 1
fi
_log DEBUG "Pandoc command for RSS feed will be constructed as follows:"
_log DEBUG "Base command: pandoc --to html5 --metadata pagetitle=\"RSS Feed\""
_log DEBUG "Metadata file arg: --metadata-file /dev/stdin"
_log DEBUG "Template arg: --template=${layout_rss_file}"
_log DEBUG "Output arg: --output=${output_rss_file}"
_log DEBUG "Other args: --standalone"
_log DEBUG "Final pandoc_cmd_rss array: ${(q+)pandoc_cmd_rss}"
local pandoc_exit_code_rss=0
# Provide a dummy input to Pandoc since content is from metadata, redirect stderr
local pandoc_exec_status_rss
if echo "<!-- RSS feed generated by qsgen3 -->" | "${pandoc_cmd_rss[@]}" 2> "$pandoc_run_rss_stderr_file"; then
pandoc_exec_status_rss=0
else
pandoc_exec_status_rss=$?
fi
pandoc_exit_code_rss=$pandoc_exec_status_rss
local stderr_content_rss=""
if [[ -s "$pandoc_run_rss_stderr_file" ]]; then
stderr_content_rss=$(<"$pandoc_run_rss_stderr_file")
fi
rm -f "$pandoc_run_rss_stderr_file"
# Execute Pandoc with YAML content piped to stdin, capturing stderr in memory
_log DEBUG "Executing Pandoc command for RSS feed with set -x..."
set -x # Enable command tracing
# Use process substitution to capture stderr without temporary files
exec 3>&1 # Save stdout to fd 3
stderr_content_rss=$( { printf '%s\n' "$yaml_content" | "${pandoc_cmd_rss[@]}" 1>&3; } 2>&1 )
pandoc_exit_code_rss=$?
exec 3>&- # Close fd 3
set +x # Disable command tracing
_log DEBUG "Pandoc execution finished for RSS. Original exit status from Pandoc: $pandoc_exit_code_rss"
if [[ $pandoc_exit_code_rss -ne 0 ]]; then
_log ERROR "Pandoc command failed for '$output_rss_file' with exit code: $pandoc_exit_code_rss."
if [[ -n "$stderr_content_rss" ]]; then
_log ERROR "Pandoc stderr:\n$stderr_content_rss"
fi
_log ERROR "YAML content passed to Pandoc for RSS was in: $temp_rss_yaml_file"
_log ERROR "RSS YAML content dump (first 500 chars):\n$(head -c 500 "$temp_rss_yaml_file" 2>/dev/null || echo 'Failed to read RSS YAML dump')"
_log ERROR "YAML content passed to Pandoc for RSS (first 500 chars):\n${yaml_content:0:500}"
return $pandoc_exit_code_rss # Return Pandoc's non-zero exit code
elif [[ -n "$stderr_content_rss" ]]; then
if echo "$stderr_content_rss" | grep -q -iE 'YAML parse exception|template error|could not find|error reading file'; then
_log ERROR "Pandoc reported a critical error for '$output_rss_file' (exit code 0). Treating as failure."
_log ERROR "Pandoc stderr:\n$stderr_content_rss"
_log ERROR "YAML content passed to Pandoc for RSS was in: $temp_rss_yaml_file"
_log ERROR "RSS YAML content dump (first 500 chars):\n$(head -c 500 "$temp_rss_yaml_file" 2>/dev/null || echo 'Failed to read RSS YAML dump')"
_log ERROR "YAML content passed to Pandoc for RSS (first 500 chars):\n${yaml_content:0:500}"
return 1 # Force a failure status
else
_log WARNING "Pandoc succeeded for '$output_rss_file' (exit code 0) but produced stderr (non-critical):\n$stderr_content_rss"