213 lines
6.1 KiB
Bash
Executable File
213 lines
6.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Function to install docgen
|
|
install_docgen() {
|
|
echo "Creating virtual environment..."
|
|
python3 -m venv docgen
|
|
source docgen/bin/activate
|
|
echo "Installing openscad_docsgen..."
|
|
pip install openscad_docsgen
|
|
echo "Installation completed."
|
|
}
|
|
# Function to uninstall (deactivate the virtual environment)
|
|
uninstall_docgen() {
|
|
echo "Deactivating virtual environment..."
|
|
deactivate
|
|
echo "Virtual environment deactivated."
|
|
}
|
|
|
|
# INI Parser
|
|
parse_ini() {
|
|
# Check if file exists and is readable
|
|
local ini_file="$1"
|
|
if [[ ! -f "$ini_file" ]] || [[ ! -r "$ini_file" ]]; then
|
|
echo "Error: Cannot read file $ini_file" >&2
|
|
return 1
|
|
fi # Changed this brace from } to fi
|
|
|
|
# Initialize section variable
|
|
local current_section=""
|
|
|
|
# Read the file line by line
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
# Trim whitespace
|
|
line="${line##*( )}"
|
|
line="${line%%*( )}"
|
|
|
|
# Skip empty lines and comments
|
|
[[ -z "$line" ]] || [[ "$line" == \#* ]] && continue
|
|
|
|
# Check if it's a section
|
|
if [[ "$line" =~ ^\[(.*)\]$ ]]; then
|
|
current_section="${BASH_REMATCH[1]}"
|
|
continue
|
|
fi
|
|
|
|
# Parse key-value pairs
|
|
if [[ "$line" =~ ^([^=]+)=(.*)$ ]]; then
|
|
local key="${BASH_REMATCH[1]}"
|
|
local value="${BASH_REMATCH[2]}"
|
|
|
|
# Trim whitespace from key and value
|
|
key="${key##*( )}"
|
|
key="${key%%*( )}"
|
|
value="${value##*( )}"
|
|
value="${value%%*( )}"
|
|
|
|
# Remove quotes if present
|
|
value="${value#\"}"
|
|
value="${value%\"}"
|
|
value="${value#\'}"
|
|
value="${value%\'}"
|
|
|
|
# Create variable name
|
|
local var_name
|
|
if [[ -n "$current_section" ]]; then
|
|
# If in a section, prefix variable with section name
|
|
var_name="${current_section}_${key}"
|
|
else
|
|
var_name="$key"
|
|
fi
|
|
|
|
# Make variable name safe for bash
|
|
var_name=$(echo "$var_name" | sed 's/[^a-zA-Z0-9_]/_/g')
|
|
|
|
# Export the variable
|
|
#declare -g "$var_name=$value"
|
|
eval "export $var_name='$value'"
|
|
fi
|
|
done < "$ini_file"
|
|
}
|
|
# Parse config.ini
|
|
parse_ini config.ini
|
|
|
|
# Debug: Print all variables in the environment
|
|
echo "Environment variables after parsing:"
|
|
printenv | grep -E "project_name|template|doc_folder|version"
|
|
|
|
|
|
compose_template() {
|
|
# Define variables
|
|
#project_name="Shelving Unit"
|
|
template=".template.md"
|
|
#doc_folder="docs"
|
|
doc_folder=$project_doc_folder
|
|
|
|
# Create temp files
|
|
tmp_overview=$(mktemp)
|
|
tmp_dependencies=$(mktemp)
|
|
tmp_usage=$(mktemp)
|
|
tmp_installation=$(mktemp)
|
|
tmp_toc=$(mktemp)
|
|
tmp_main=$(mktemp)
|
|
tmp_table_of_contents=$(mktemp)
|
|
tmp_topics=$(mktemp)
|
|
tmp_alpha_index=$(mktemp)
|
|
tmp_sidebar=$(mktemp)
|
|
|
|
# Read content into temp files
|
|
|
|
|
|
|
|
process_md "Overview" "$tmp_overview"
|
|
process_md "Dependencies" "$tmp_dependencies"
|
|
process_md "Usage" "$tmp_usage"
|
|
process_md "Installation" "$tmp_usage"
|
|
# process_md "$project_main" "$tmp_main"
|
|
|
|
# cat "$doc_folder/shelf.scad.md" > "$tmp_main"
|
|
|
|
# Replace image links in main
|
|
# sed -i 's|images/|docs/images/|g' "$tmp_main"
|
|
if [ -f "$doc_folder/${project_main}.md" ]; then
|
|
cat "$doc_folder/${project_main}.md" | sed 's|images/|docs/images/|g' > "$tmp_main"
|
|
fi
|
|
process_md "TOC" "$tmp_toc"
|
|
process_md "_Sidebar" "$tmp_table_of_contents"
|
|
process_md "Topics" "$tmp_topics"
|
|
process_md "AlphaIndex" "$tmp_alpha_index"
|
|
process_md "_Sidebar" "$tmp_sidebar"
|
|
|
|
# Use sed with file reads
|
|
sed -e "s|{{project_name}}|$project_name|g" \
|
|
-e "s|{{project_version}}|$project_version|g" \
|
|
-e "/{{overview}}/r $tmp_overview" -e "/{{overview}}/d" \
|
|
-e "/{{dependencies}}/r $tmp_dependencies" -e "/{{dependencies}}/d" \
|
|
-e "/{{usage}}/r $tmp_usage" -e "/{{usage}}/d" \
|
|
-e "/{{installation}}/r $tmp_usage" -e "/{{installation}}/d" \
|
|
-e "/{{main}}/r $tmp_main" -e "/{{main}}/d" \
|
|
-e "/{{toc}}/r $tmp_toc" -e "/{{toc}}/d" \
|
|
-e "/{{table_of_contents}}/r $tmp_table_of_contents" -e "/{{table_of_contents}}/d" \
|
|
-e "/{{topics}}/r $tmp_topics" -e "/{{topics}}/d" \
|
|
-e "/{{alpha_index}}/r $tmp_alpha_index" -e "/{{alpha_index}}/d" \
|
|
-e "/{{sidebar}}/r $tmp_sidebar" -e "/{{sidebar}}/d" \
|
|
"$template" > README.md
|
|
|
|
# Clean up temp files
|
|
rm -f "$tmp_overview" "$tmp_dependencies" "$tmp_usage" "$tmp_toc" "$tmp_table_of_contents" "$tmp_topics" "$tmp_alpha_index" "$tmp_sidebar"
|
|
|
|
echo "README.md has been generated."
|
|
|
|
}
|
|
|
|
|
|
# Function to run openscad-docsgen with your specified options
|
|
run_docsgen() {
|
|
# Check if we are in the virtual environment
|
|
if [ -z "$VIRTUAL_ENV" ] || [ ! "$(basename "$VIRTUAL_ENV")" == "docgen" ]; then
|
|
echo "Activating docgen virtual environment..."
|
|
source docgen/bin/activate || { echo "Failed to activate virtual environment. Make sure 'docgen' exists."; return 1; }
|
|
fi
|
|
echo "Running openscad-docsgen..."
|
|
#openscad-docsgen -m -I -P "Metalib" -f -p wiki -i -t -m -s
|
|
openscad-docsgen -f -P "$project_name"
|
|
echo "Documentation generation completed."
|
|
compose_template
|
|
echo "Documentation composition completed"
|
|
}
|
|
|
|
|
|
process_md() {
|
|
local src_file="$doc_folder/$1.md"
|
|
local dest_file="$2"
|
|
if [ -f "$src_file" ]; then
|
|
if [ "$1" == "$project_main" ]; then
|
|
sed 's|images/|docs/images/|g' "$src_file" > "$dest_file"
|
|
else
|
|
cat "$src_file" > "$dest_file"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
|
|
# Main script logic
|
|
echo "OpenSCAD DocsGen Script"
|
|
echo "----------------------"
|
|
echo "1. Doc Generation"
|
|
echo "2. Install docgen"
|
|
echo "3. Uninstall docgen"
|
|
echo "q. Exit"
|
|
|
|
while true; do
|
|
read -p "Choose an option (1/2/3/q): " choice
|
|
|
|
case $choice in
|
|
1)
|
|
run_docsgen
|
|
;;
|
|
2)
|
|
install_docgen
|
|
;;
|
|
3)
|
|
uninstall_docgen
|
|
;;
|
|
q)
|
|
echo "Exiting script. Goodbye!"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Invalid option. Please try again."
|
|
;;
|
|
esac
|
|
done
|