add support for global template variables
This commit is contained in:
parent
f3e9accde8
commit
95340ca307
31
mige.sh
31
mige.sh
@ -9,6 +9,7 @@ REGEX_VARIABLE="^([0-9]+)=(.+)$"
|
|||||||
SEPARATOR_EMBED="|"
|
SEPARATOR_EMBED="|"
|
||||||
TEMPLATE_HEADER=${TEMPLATE_HEADER:-templates/header.html}
|
TEMPLATE_HEADER=${TEMPLATE_HEADER:-templates/header.html}
|
||||||
TEMPLATE_FOOTER=${TEMPLATE_FOOTER:-templates/footer.html}
|
TEMPLATE_FOOTER=${TEMPLATE_FOOTER:-templates/footer.html}
|
||||||
|
TEMPLATE_VARS=${TEMPLATE_VARS:-.vars}
|
||||||
SOURCEDIR=$PWD
|
SOURCEDIR=$PWD
|
||||||
|
|
||||||
OPTION_ASSETS=false
|
OPTION_ASSETS=false
|
||||||
@ -41,26 +42,26 @@ replace_variables() {
|
|||||||
if [[ $variable =~ $REGEX_VARIABLE ]]; then
|
if [[ $variable =~ $REGEX_VARIABLE ]]; then
|
||||||
local variable_name=${BASH_REMATCH[1]}
|
local variable_name=${BASH_REMATCH[1]}
|
||||||
local variable_value=${BASH_REMATCH[2]}
|
local variable_value=${BASH_REMATCH[2]}
|
||||||
sed -i "s/\$$variable_name/$variable_value/g;/$variable/d" $1
|
sed -i "s|\$$variable_name|$variable_value|g;\|$variable|d" $1
|
||||||
else
|
else
|
||||||
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
|
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
|
||||||
fi
|
fi
|
||||||
done < <(grep -P $REGEX_VARIABLE $1)
|
done < <(grep -P $REGEX_VARIABLE $1 | sort -gr)
|
||||||
}
|
}
|
||||||
|
|
||||||
replace_embeds () {
|
replace_embeds () {
|
||||||
while read match; do
|
while read match; do
|
||||||
IFS=$SEPARATOR_EMBED read -ra parts <<< "$match"
|
IFS=$SEPARATOR_EMBED read -ra parts <<< "$match"
|
||||||
file_embed="$SOURCEDIR/$DIR_EMBED/$(echo "${parts[0]}$SEPARATOR_EMBED" | sed -r "s/$REGEX_EMBED/\1/")"
|
file_embed="$SOURCEDIR/$DIR_EMBED/$(echo "${parts[0]}$SEPARATOR_EMBED" | sed -r "s|$REGEX_EMBED|\1|")"
|
||||||
params=("${parts[@]:1}")
|
params=("${parts[@]:1}")
|
||||||
|
|
||||||
if [ -f "$file_embed" ]; then
|
if [ -f "$file_embed" ]; then
|
||||||
expr_replace=""
|
expr_replace=""
|
||||||
for index in "${!params[@]}"; do
|
for index in "${!params[@]}"; do
|
||||||
expr_replace+="s/\$$(($index+1))/${params[index]}/g;"
|
expr_replace+="s|\$$(($index+1))|${params[index]}|g;"
|
||||||
done
|
done
|
||||||
expr_replace+="s/\n//g;"
|
expr_replace+="s|\n||g;"
|
||||||
sed -i "s/$match/$(sed -z "$expr_replace" $file_embed)/g" $1
|
sed -i "s|$match|$(sed -z "$expr_replace" $file_embed)|g" $1
|
||||||
else
|
else
|
||||||
echo -e "\e[38;5;208mWarning: embed file '$file_embed' doesn't exist, skipping.\e[0m"
|
echo -e "\e[38;5;208mWarning: embed file '$file_embed' doesn't exist, skipping.\e[0m"
|
||||||
fi
|
fi
|
||||||
@ -71,9 +72,13 @@ process_file () {
|
|||||||
local timep=$(date +%s%N)
|
local timep=$(date +%s%N)
|
||||||
local sourcediresc=${SOURCEDIR//\//\\/}
|
local sourcediresc=${SOURCEDIR//\//\\/}
|
||||||
|
|
||||||
mkdir -p $( dirname "$1" | sed "s/$sourcediresc\/$DIR_CONTENT/$sourcediresc\/$DIR_DIST/" )
|
mkdir -p $( dirname "$1" | sed "s|$sourcediresc/$DIR_CONTENT|$sourcediresc/$DIR_DIST|" )
|
||||||
local file_output=$( echo "$1" | sed "s/$sourcediresc\/$DIR_CONTENT/$sourcediresc\/$DIR_DIST/" )
|
local file_output=$( echo "$1" | sed "s|$sourcediresc/$DIR_CONTENT|$sourcediresc/$DIR_DIST|" )
|
||||||
cat "$SOURCEDIR/$TEMPLATE_HEADER" "$1" "$SOURCEDIR/$TEMPLATE_FOOTER" > $file_output
|
cat "$SOURCEDIR/$TEMPLATE_HEADER" "$1" "$SOURCEDIR/$TEMPLATE_FOOTER" > $file_output
|
||||||
|
|
||||||
|
if [ -f "$2" ]; then
|
||||||
|
cat "$2" >> $file_output
|
||||||
|
fi
|
||||||
|
|
||||||
if [ $OPTION_EMBEDS = true ]; then
|
if [ $OPTION_EMBEDS = true ]; then
|
||||||
replace_embeds $file_output
|
replace_embeds $file_output
|
||||||
@ -95,8 +100,14 @@ process_assets () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
main () {
|
main () {
|
||||||
|
local file_template_vars="$SOURCEDIR/$TEMPLATE_VARS"
|
||||||
|
if [ ! -f "$file_template_vars" ]; then
|
||||||
|
echo -e "\e[38;5;208mWarning: template vars file '$file_template_vars' doesn't exist, skipping.\e[0m"
|
||||||
|
file_template_vars=""
|
||||||
|
fi
|
||||||
|
|
||||||
while IFS= read -d '' file_content; do
|
while IFS= read -d '' file_content; do
|
||||||
process_file "${file_content}" </dev/null
|
process_file "${file_content}" "${file_template_vars}" </dev/null
|
||||||
done < <(find "$SOURCEDIR/$DIR_CONTENT" -type f -print0)
|
done < <(find "$SOURCEDIR/$DIR_CONTENT" -type f -print0)
|
||||||
|
|
||||||
if [ $OPTION_ASSETS = true ]; then
|
if [ $OPTION_ASSETS = true ]; then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user