|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## 20220225 This export script takes the rendered website and uses pandoc |
| 4 | +## to convert to other formats such as HTML or PDF |
| 5 | + |
| 6 | +## The possible parameters are: |
| 7 | +## ** -l: locale (default en) |
| 8 | +## ** -t: type of file (default HTML) |
| 9 | +## ** -r: render folder (default .. a.k.a. root) |
| 10 | +## ** -p: pandoc parameters |
| 11 | +## ** -e: export folder (default export) |
| 12 | + |
| 13 | +## Load utils file |
| 14 | +UTILS_PATH='./utils.sh' |
| 15 | + |
| 16 | +## Include the functions |
| 17 | +source "${UTILS_PATH}" |
| 18 | + |
| 19 | +## Load config file (https://wiki.bash-hackers.org/howto/conffile#secure_it) |
| 20 | +CONFIG_PATH='./config/config.conf' |
| 21 | + |
| 22 | +## Check for malformed config instructions |
| 23 | +configReturn=(checkConfigFile) |
| 24 | + |
| 25 | +## Otherwise go on and source it: |
| 26 | +source "${CONFIG_PATH}" |
| 27 | + |
| 28 | +while getopts ":l:t:r:p:e:" opt; do |
| 29 | + case $opt in |
| 30 | + l) LOCALE="$OPTARG" |
| 31 | + ;; |
| 32 | + t) TYPE_FILE="$OPTARG" |
| 33 | + ;; |
| 34 | + r) RENDER_FOLDER="$OPTARG" |
| 35 | + ;; |
| 36 | + p) PANDOC_PARAMETERS="$OPTARG" |
| 37 | + ;; |
| 38 | + e) EXPORT_FOLDER="$OPTARG" |
| 39 | + ;; |
| 40 | + \?) echo "Invalid option -$OPTARG" >&2 |
| 41 | + exit 1 |
| 42 | + ;; |
| 43 | + esac |
| 44 | + |
| 45 | + case $OPTARG in |
| 46 | + -*) echo "Option $opt needs a valid argument" |
| 47 | + exit 1 |
| 48 | + ;; |
| 49 | + esac |
| 50 | +done |
| 51 | + |
| 52 | +## Folders used |
| 53 | +LOCALE_FOLDER=${LOCALE} |
| 54 | +CURRENT_FOLDER="${RENDER_FOLDER}/${SITE_FOLDER}/${LOCALE_FOLDER}" |
| 55 | +CURRENT_EXPORT_FOLDER="${EXPORT_FOLDER}/${SITE_FOLDER}/${LOCALE_FOLDER}" |
| 56 | +IMAGE_FOLDER="${RENDER_FOLDER}/${IMG_FOLDER}" |
| 57 | + |
| 58 | +## Data and index files |
| 59 | +INDEX_FILE="${CURRENT_FOLDER}/${SITE_INDEX}" |
| 60 | + |
| 61 | +## Step 1: check the type of file to work with, exit if not in the list |
| 62 | +typeExists=$(elementInWhere "${TYPE_FILE}" "${EXPORT_TYPES[@]}") |
| 63 | +[[ $? -ne 0 ]] && { echo "$TYPE_FILE file format not supported"; exit 99; } |
| 64 | + |
| 65 | +## Step 1.1: Copy the render folder to a temporary one to ease the work |
| 66 | +## But first, create the folder if it doesn't exist |
| 67 | +[ ! -d "${TEMP_FOLDER}" ] && mkdir ${TEMP_FOLDER} |
| 68 | +[ ! -d "${TEMP_FOLDER}/${SITE_FOLDER}" ] && mkdir ${TEMP_FOLDER}/${SITE_FOLDER} |
| 69 | +cp -R ${CURRENT_FOLDER} ${TEMP_FOLDER}/${SITE_FOLDER} |
| 70 | +cp -R ${IMAGE_FOLDER} ${TEMP_FOLDER} |
| 71 | + |
| 72 | +## Since you are at it, create the export folder and add the images there |
| 73 | +[ ! -d "${EXPORT_FOLDER}" ] && mkdir ${EXPORT_FOLDER} |
| 74 | +[ ! -d "${EXPORT_FOLDER}/${SITE_FOLDER}" ] && mkdir ${EXPORT_FOLDER}/${SITE_FOLDER} |
| 75 | +[ ! -d "${CURRENT_EXPORT_FOLDER}" ] && mkdir ${CURRENT_EXPORT_FOLDER} |
| 76 | +cp -R ${IMAGE_FOLDER} ${EXPORT_FOLDER} |
| 77 | + |
| 78 | +## Step 2: if HTML, search for all Markdown files and convert them one by one |
| 79 | +if [[ ${TYPE_FILE} == "HTML" ]]; then |
| 80 | + ## List all of the Markdown files into an array |
| 81 | + markdownFiles=$(find "${TEMP_FOLDER}" | grep "\.md$") |
| 82 | + readarray -t MARKDOWN_FILES <<<"$markdownFiles" |
| 83 | + |
| 84 | + ## Iterate through the array |
| 85 | + for fileIndex in "${!MARKDOWN_FILES[@]}" |
| 86 | + do : |
| 87 | + echo -e "** Converting: ${MARKDOWN_FILES[$fileIndex]}" |
| 88 | + |
| 89 | + ## Extract file name without extension |
| 90 | + filename=$(basename -- "${MARKDOWN_FILES[$fileIndex]}") |
| 91 | + filename="${filename%.*}" |
| 92 | + ## extension="${filename##*.}" |
| 93 | + ##echo -e "** Filename: ${filename}" |
| 94 | + |
| 95 | + ## Extract the path to the file |
| 96 | + fileFolder=$(dirname "${MARKDOWN_FILES[$fileIndex]}") |
| 97 | + |
| 98 | + ## Remove the first folder block |
| 99 | + fileFolder=${fileFolder#$TEMP_FOLDER} |
| 100 | + |
| 101 | + ## Add the export folder name |
| 102 | + fileFolder=$EXPORT_FOLDER/$fileFolder |
| 103 | + |
| 104 | + ## Fix the index block to link to HTML files |
| 105 | + ## TODO: this will not allow me to include links to ANY Markdown Files |
| 106 | + ## It could be a good idea to rethink this in the future |
| 107 | + ##if [[ "${filename}.md" == "${SITE_INDEX}" ]]; then |
| 108 | + sed -i "s/.md)/.html)/g" "${MARKDOWN_FILES[$fileIndex]}" |
| 109 | + ##fi |
| 110 | + |
| 111 | + ## Create the page folder, this trick allows two levels of depth |
| 112 | + ## such as the created by the generator, having more depth would |
| 113 | + ## require a different subfolder generator capable of detecting how |
| 114 | + ## many subfolders to create along the way |
| 115 | + [ ! -d "${fileFolder}" ] && mkdir ${fileFolder} |
| 116 | + ##[ ! -d "${CURRENT_EXPORT_FOLDER}/${filename}" ] && mkdir ${CURRENT_EXPORT_FOLDER}/${filename} |
| 117 | + |
| 118 | + pandoc -s ${MARKDOWN_FILES[$fileIndex]} --metadata pagetitle="${filename}" -o ${fileFolder}/${filename}.html |
| 119 | + ##pandoc ${MARKDOWN_FILES[$fileIndex]} -o ${CURRENT_EXPORT_FOLDER}/${filename}/${filename}.html |
| 120 | + done |
| 121 | +fi |
| 122 | + |
| 123 | +## Step 3: if PDF, remove the indexing part of all files and compose them together |
| 124 | +if [[ ${TYPE_FILE} == "PDF" ]]; then |
| 125 | + ## List all of the Markdown files into an array |
| 126 | + markdownFiles=$(find "${TEMP_FOLDER}" | grep "\.md$") |
| 127 | + readarray -t MARKDOWN_FILES <<<"$markdownFiles" |
| 128 | + |
| 129 | + ## Iterate through the array |
| 130 | + for fileIndex in "${!MARKDOWN_FILES[@]}" |
| 131 | + do : |
| 132 | + ## Work only if not dealing with the index file, which should be |
| 133 | + ## left out of the PDF generation |
| 134 | + |
| 135 | + ## Extract file name without extension |
| 136 | + filename=$(basename -- "${MARKDOWN_FILES[$fileIndex]}") |
| 137 | + filename="${filename%.*}" |
| 138 | + |
| 139 | + if [[ "${filename}.md" != "${SITE_INDEX}" ]]; then |
| 140 | + echo -e "** Adding: ${MARKDOWN_FILES[$fileIndex]}" |
| 141 | + |
| 142 | + ## Append the file at the bottom of the temp file |
| 143 | + cat "${MARKDOWN_FILES[$fileIndex]}" >> "${TEMP_FOLDER}/${TEMP_EXPORT_FILE}" |
| 144 | + fi |
| 145 | + done |
| 146 | + |
| 147 | + ## Remove all of the index blocks |
| 148 | + ## TODO: fix this to work with other locales by using information from the |
| 149 | + ## template.csv file instead of having it hardcoded in here |
| 150 | + ##sed -z -i 's/## Index\(.*\)##/##/g' "${TEMP_FOLDER}/${TEMP_EXPORT_FILE}" |
| 151 | + sed -i '/## Index/,/##/{//!d}' "${TEMP_FOLDER}/${TEMP_EXPORT_FILE}" |
| 152 | + sed -z -i 's/## Index//g' "${TEMP_FOLDER}/${TEMP_EXPORT_FILE}" |
| 153 | + |
| 154 | + ## TODO: deal with the video blocks by searching for a screenshot |
| 155 | + |
| 156 | + pandoc "${TEMP_FOLDER}/${TEMP_EXPORT_FILE}" --pdf-engine=xelatex -o "${EXPORT_FOLDER}/${PDF_EXPORT_FILE}" |
| 157 | + |
| 158 | + ## Open the file for inspection |
| 159 | + xdg-open "${EXPORT_FOLDER}/${PDF_EXPORT_FILE}" |
| 160 | +fi |
| 161 | + |
| 162 | +## XXX |
| 163 | + |
| 164 | +## We finished, delete the temporary folder |
| 165 | +[ -d "${TEMP_FOLDER}" ] && rm -fR ${TEMP_FOLDER} |
| 166 | + |
| 167 | +## Done |
| 168 | +echo -e "** Done" |
0 commit comments