sssg/sssg.sh

102 lines
2.5 KiB
Bash
Raw Normal View History

2020-10-25 07:51:56 +00:00
#!/bin/bash
# Copyright (C) 2020 Ortega Froysa, Nicolás <nicolas@ortegas.org>
# Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function parse {
echo -n "Generating _site/$1 ... "
touch _site/$1
# get the variables
2020-10-25 07:57:33 +00:00
last_find="$(grep -n "^%%%$" $1)"
2020-10-25 07:51:56 +00:00
vars=()
if ! [ -z $last_find ]
then
2020-10-25 07:57:33 +00:00
end_line="$(expr ${last_find%%:*} - 1)"
2020-10-25 07:51:56 +00:00
for (( i=1; i<=$end_line; i++ ))
do
vars+=("$(sed "$i!d" $1)")
done
fi
# import templates
2020-10-25 07:57:33 +00:00
for i in $(grep -n "^#/" $1)
2020-10-25 07:51:56 +00:00
do
2020-10-25 07:57:33 +00:00
end_line="$(expr ${i%%:*} - 1)"
2020-10-25 07:51:56 +00:00
if [ -z $last_find ]
then
sed -n "1,$end_line{p}" $1 >> _site/$1
else
2020-10-25 07:57:33 +00:00
start_line="$(expr ${last_find%%:*} + 1)"
2020-10-26 08:42:41 +00:00
if [ $start_line -le $end_line ]
then
sed -n "$start_line,$end_line{p}" $1 >> _site/$1
fi
2020-10-25 07:51:56 +00:00
fi
2020-10-25 07:57:33 +00:00
#inc_file="$(sed "$i!d" $1)"
inc_file=${i#*:}
2020-10-25 07:51:56 +00:00
cat _templates/${inc_file:2} >> _site/$1
last_find="$i"
done
2020-10-25 07:57:33 +00:00
start_line="$(expr ${last_find%%:*} + 1)"
2020-10-25 07:51:56 +00:00
sed -n "$start_line,\$p" $1 >> _site/$1
# replace variables
for i in "${vars[@]}"
do
var_name=${i%%=*}
var_contents=${i#*=}
sed -i "s/\${$var_name}/$var_contents/g" _site/$1
done
echo "OK"
}
function generate {
# create directories
rm -rf _site
for i in $(find . -type d -not -path "./_templates" \
-not -path "./_site" -not -path "./_templates/*" \
-not -path "./_site/*")
do
dir_path=${i:2}
echo -n "Creating directory _site/$dir_path ... "
mkdir -p _site/$dir_path
echo "OK"
done
# generate files
for i in $(find . -type f -not -path "./_templates/*" -not -path \
"./_site/*")
do
file_path=${i:2}
if [ ${file_path: -4} == ".htm" ] ||
[ ${file_path: -4} == ".HTM" ] ||
[ ${file_path: -5} == ".html" ] ||
[ ${file_path: -5} == ".HTML" ]
then
# parse HTML/CSS files
parse $file_path
else
# files that don't need parsing
echo -n "Copying file to _site/$file_path ... "
cp $file_path _site/$file_path
echo "OK"
fi
done
}
generate