summaryrefslogtreecommitdiff
path: root/seen.sh
blob: 0ecf44610cdd390c3fe851b6cf1ab1ae0020bc08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh

#    Seen, a simple static blog generator
#
#    Copyright (C) 2021 Vitali64 <vitali64pmemail@protonmail.com
#    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 <https://www.gnu.org/licenses/>.

usage() {
	echo "Usage   : ./seen [OPTIONS]"
	echo
	echo "--help  : Print this help message"
	echo "--clear : Remove generated blog"
	echo "--rm    : Remove everything and start from scratch"
}

die() {
	echo "-> [$(date +%R)] (!) ERROR: Unexpected error, please report : $1 (!)"
	exit 1
}

echo "Seen, a simple static blog generator" 
echo "    Copyright (C) 2021 Vitali64 <vitali64pmemail@protonmail.com"
echo "    This program is free software: you can redistribute it and/or modify"
echo "    it under the terms of the GNU General Public License as published by"
echo "    the Free Software Foundation, either version 3 of the License, or"
echo "    (at your option) any later version."
echo 
echo "    This program is distributed in the hope that it will be useful,"
echo "    but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
echo "    GNU General Public License for more details."
echo 
echo "    You should have received a copy of the GNU General Public License"
echo "    along with this program.  If not, see <https://www.gnu.org/licenses/>."

if [ "$1" = "--clear" ]; then
	echo "-> [$(date +%R)] Deleting generated blog ..."
	rm -Rf www/* || die "Cannot delete generated blog!"
	exit 0
elif [ "$1" = "--rm" ]; then
	echo "-> [$(date +%R)] Deleting all data ..."
	rm -Rf www/* articles/* || die "Cannot delete www/* and articles/*!"
	exit 0
elif [ "$1" = "--help" ]; then
	usage
	exit 0
elif [ "$1" = "" ]; then
	echo "-> [$(date +%R)] No options specified, generating blog ..."
else
	echo "-> [$(date +%R)] (!) ERROR : Invalid option, cannot continue. (!)"
	usage
	exit 1
fi

# Create the www/ folder if removed
mkdir -p "www" || die "Cannot create www/ directory: mkdir -p returns an error!"

# Erase www/index.html and insert the header
echo "-> [$(date +%R)] Inserting the header ..."
cat "templates/header.html" > www/index.html || die "Cannot insert header into index.html!"

# Detect articles
echo "-> [$(date +%R)] Detecting articles' markdown file ..."
articles="$(find articles/*.md|sed -e 's@.md@@g' -e 's@articles/@@g')" || die "Unknown error"

# Set the defaults
name="(!) no name (!)"
desc="(!) no description (!)"
date="1st January 1970"

# In the 'for' loop
for line in ${articles}
do
	. "articles/${line}.cfg" || die "Cannot read ${line}.cfg file!" # Override the defaults
	mkdir "www/articles" -p
	echo "-> [$(date +%R)] Inserting header to ${line}.html ..."
	cat "templates/header.html" > "www/articles/${line}.html" || die "Cannot insert header into ${line}.html!" # Erase <article>.html and insert the header
	echo "-> [$(date +%R)] Converting ${line}.md to html ..."
	markdown "articles/${line}.md" >> "www/articles/${line}.html" || hoedown "articles/${line}.md" >> "www/articles/${line}.html" || die "Cannot convert ${line}.md to html!" # Convert the markdown text to html
	echo "-> [$(date +%R)] Inserting footer to ${line}.html ..."
	cat "templates/footer.html" >> "www/articles/${line}.html" || die "Cannot insert footer into ${line}.html!" # Insert the footer into the article page
	cp -r templates/*.css www/. # Add css files if present

	# Add an entry in index.html
	echo "-> [$(date +%R)] Adding ${line} entry to index.html ..."
	sed \
		-e "s@path-of-article@articles/${line}@" \
		-e "s@name-of-article@${name}@" \
		-e "s@date-of-article@${date}@" \
		-e "s@description-of-article@${desc}@" \
		"templates/article.html">> "www/index.html" || die "Cannot add entry to index.html!"
done

# Insert the footer into the index.html
echo "-> [$(date +%R)] Inserting the footer ..."
cat "templates/footer.html" >> "www/index.html"
echo "-> [$(date +%R)] Generation made successfully!"
echo "Enjoy your blog. Exiting ..."