/ process-files.sh
process-files.sh
1 #!/bin/zsh 2 3 # Function to recursively convert .md files using mdcat and save to the corresponding location in book_directory 4 convert_md_files() { 5 local src_directory="$1" 6 local book_directory="$2" 7 8 # Iterate over each item in the source directory 9 for item in "$src_directory"/*; 10 do 11 if [ -d "$item" ]; 12 then 13 # Create the corresponding directory in the book_directory 14 mkdir -p "$book_directory/$(basename "$item")" 15 # If the item is a directory, recursively call this function 16 convert_md_files "$item" "$book_directory/$(basename "$item")" 17 elif [ -f "$item" ] && [[ "$item" == *.md ]]; 18 then 19 base_name=$(basename "$item" .md) 20 # Create a text file with the raw content of the .md file 21 cat "$item" > "$book_directory/$base_name.md" 22 echo "Raw content copied: $item to $book_directory/$base_name.md" 23 24 # Convert the markdown file to a text file using mdcat 25 mdcat "$item" > "$book_directory/$base_name.md.txt" 26 echo "Converted: $item to $book_directory/$base_name.md.txt" 27 fi 28 done 29 } 30 31 # Function to recursively sign files in a directory 32 sign_files_in_directory() { 33 local directory="$1" 34 35 # Iterate over each item in the directory 36 for item in "$directory"/*; 37 do 38 echo "$item\n" 39 40 if [ -d "$item" ]; 41 then 42 # If the item is a directory, recursively call this function 43 sign_files_in_directory "$item" 44 elif [ -f "$item" ]; 45 then 46 # Create a detached signature for the file 47 gpg --output "$item.asc" --detach-sign "$item" 48 echo "Signed: $item" 49 fi 50 done 51 } 52 53 # Write robots.txt file 54 write_robots() { 55 local directory="$1" 56 echo "User-agent: * 57 Disallow: 58 Disallow: /assets 59 Disallow: /theme 60 Sitemap: https://news.awfulsec.com/sitemap.xml" > "$directory/robots.txt" 61 } 62 63 # Check if two directories were provided as arguments 64 if [ -z "$1" ] || [ -z "$2" ]; 65 then 66 echo "Usage: $0 <src_directory> <book_directory>" 67 exit 1 68 fi 69 70 # Call the functions with the provided directories 71 mdbook build 72 write_robots "$2" 73 mdbook-sitemap-generator -d news.awfulsec.com -o book/sitemap.xml 74 convert_md_files "$1" "$2" 75 sign_files_in_directory "$2" 76 tar czvf website.tar.gz book