#!/bin/bash # Script to concatenate all code from source directories for LaTeX inclusion # Detect project root (parent of paper directory) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" OUTPUT_FILE="$PROJECT_ROOT/paper/build/concatenated_code.tex" # Create output directory if it doesn't exist mkdir -p "$(dirname "$OUTPUT_FILE")" # Clear the output file > "$OUTPUT_FILE" # Function to add a file to the concatenated output add_file() { local filepath="$1" local relpath="${filepath#$PROJECT_ROOT/}" local escaped_path="${relpath//_/\\_}" # Add section header and code listing (no language-specific highlighting) echo "\\subsection{${escaped_path}}" >> "$OUTPUT_FILE" echo "\\begin{lstlisting}[caption={${escaped_path}}]" >> "$OUTPUT_FILE" # Sanitize UTF-8: convert to ASCII with transliteration to avoid invalid UTF-8 bytes iconv -f UTF-8 -t ASCII//TRANSLIT "$filepath" >> "$OUTPUT_FILE" 2>/dev/null || cat "$filepath" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" echo "\\end{lstlisting}" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" } # Add header cat >> "$OUTPUT_FILE" << 'EOF' % Auto-generated code concatenation % DO NOT EDIT MANUALLY - Generated by concat_code.sh \section{Source Code} EOF # Process each directory echo "Concatenating code from source directories..." # Backend find "$PROJECT_ROOT/backend" -type f \( -name "*.py" -o -name "*.js" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" \) | sort | while read -r file; do add_file "$file" done # Experiments find "$PROJECT_ROOT/experiments" -type f \( -name "*.py" -o -name "*.js" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" \) | sort | while read -r file; do add_file "$file" done # Docker find "$PROJECT_ROOT/docker" -type f \( -name "*.py" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" -o -name "Dockerfile*" \) | sort | while read -r file; do add_file "$file" done # Web/src find "$PROJECT_ROOT/web/src" -type f \( -name "*.js" -o -name "*.jsx" -o -name "*.ts" -o -name "*.tsx" \) | sort | while read -r file; do add_file "$file" done echo "Code concatenation complete: $OUTPUT_FILE"