add embed funtionality, refactor regex into variable
This commit is contained in:
parent
0e74eb2595
commit
93c88daf78
30
mige.sh
30
mige.sh
@ -3,6 +3,10 @@
|
||||
DIR_ASSETS=${DIR_ASSETS:-assets}
|
||||
DIR_CONTENT=${DIR_CONTENT:-in}
|
||||
DIR_DIST=${DIR_DIST:-!out}
|
||||
DIR_EMBED=${DIR_EMBED:-embed}
|
||||
REGEX_EMBED="^##(.*?)\|.*"
|
||||
REGEX_VARIABLE="^([0-9]+)=(.+)$"
|
||||
SEPARATOR_EMBED="|"
|
||||
TEMPLATE_HEADER=${TEMPLATE_HEADER:-templates/header.html}
|
||||
TEMPLATE_FOOTER=${TEMPLATE_FOOTER:-templates/footer.html}
|
||||
SOURCEDIR=$PWD
|
||||
@ -14,6 +18,7 @@ check_files () {
|
||||
if [ ! -f "$SOURCEDIR/$TEMPLATE_FOOTER" ]; then echo "Header template '$TEMPLATE_FOOTER' doesn't exist."; exit 2; fi
|
||||
if [ ! -d "$SOURCEDIR/$DIR_CONTENT" ]; then echo "Content directory '$DIR_CONTENT' doesn't exist. Create it and add some content!"; exit 2; fi
|
||||
if [ ! -d "$SOURCEDIR/$DIR_DIST" ]; then mkdir "$SOURCEDIR/$DIR_DIST"; echo "Directory '$DIR_DIST' created."; fi
|
||||
if [ ! -d "$SOURCEDIR/$DIR_EMBED" ]; then mkdir "$SOURCEDIR/$DIR_EMBED"; echo "Directory '$DIR_EMBED' created."; fi
|
||||
if [[ $OPTION_ASSETS == true && ! -s "$SOURCEDIR/$DIR_ASSETS" ]]; then echo "Directory '$DIR_ASSETS' doesn't exist. Create it and add some assets!"; exit 2; fi
|
||||
}
|
||||
|
||||
@ -27,6 +32,25 @@ read_options() {
|
||||
done
|
||||
}
|
||||
|
||||
replace_embeds () {
|
||||
while read match; do
|
||||
IFS=$SEPARATOR_EMBED read -ra parts <<< "$match"
|
||||
file_embed="$SOURCEDIR/$DIR_EMBED/$(echo "${parts[0]}$SEPARATOR_EMBED" | sed -r "s/$REGEX_EMBED/\1/")"
|
||||
params=("${parts[@]:1}")
|
||||
|
||||
if [ -f $file_embed ]; then
|
||||
expr_replace=""
|
||||
for index in "${!params[@]}"; do
|
||||
expr_replace+="s/\$$(($index+1))/${params[index]}/g;"
|
||||
done
|
||||
expr_replace+="s/\n//g;"
|
||||
sed -i "s/$match/$(sed -z "$expr_replace" $file_embed)/g" $1
|
||||
else
|
||||
echo -e "\e[38;5;208mWarning: embed file '$embed_file' doesn't exist, skipping.\e[0m"
|
||||
fi
|
||||
done <<< $(grep -P $REGEX_EMBED $1)
|
||||
}
|
||||
|
||||
process_file () {
|
||||
local timep=$(date +%s%N)
|
||||
local sourcediresc=${SOURCEDIR//\//\\/}
|
||||
@ -35,14 +59,16 @@ process_file () {
|
||||
local file_output=$( echo "$1" | sed "s/$sourcediresc\/$DIR_CONTENT/$sourcediresc\/$DIR_DIST/" )
|
||||
cat "$SOURCEDIR/$TEMPLATE_HEADER" "$1" "$SOURCEDIR/$TEMPLATE_FOOTER" > $file_output
|
||||
while read -r variable; do
|
||||
if [[ $variable =~ ^([0-9]+)=(.+)$ ]]; then
|
||||
if [[ $variable =~ $REGEX_VARIABLE ]]; then
|
||||
local variable_name=${BASH_REMATCH[1]}
|
||||
local variable_value=${BASH_REMATCH[2]}
|
||||
sed -i "s/\$$variable_name/$variable_value/g;/$variable/d" $file_output
|
||||
else
|
||||
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
|
||||
fi
|
||||
done < <(grep -P '^\d+=.+$' $1)
|
||||
done < <(grep -P $REGEX_VARIABLE $file_output)
|
||||
|
||||
replace_embeds $file_output
|
||||
|
||||
timep=$((($(date +%s%N) - $timep)/1000000))
|
||||
echo -e "\e[32mFile $1 processed to $file_output ($timep ms).\e[0m"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user