aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitali64 <vitali64pmemail@protonmail.com>2021-12-19 10:34:12 +0100
committerVitali64 <vitali64pmemail@protonmail.com>2021-12-19 10:34:12 +0100
commitc949eb3b5fd07c23c2ece2ea92466ed28290e165 (patch)
tree06bae794aeff6ab71e0ccca18de222beafe9fcb4
parent1cdae9a1928acbf35b3780be282afbbc2d5ae77c (diff)
downloadseen-c949eb3b5fd07c23c2ece2ea92466ed28290e165.tar.gz
seen-c949eb3b5fd07c23c2ece2ea92466ed28290e165.zip
Fix typo, reduce SLOC and add error handling
-rw-r--r--README.md14
-rwxr-xr-xmain.sh83
-rwxr-xr-xseen.sh83
-rw-r--r--www/articles/articletext.html32
-rw-r--r--www/index.html30
5 files changed, 153 insertions, 89 deletions
diff --git a/README.md b/README.md
index 0151c48..59022e4 100644
--- a/README.md
+++ b/README.md
@@ -13,16 +13,18 @@ You also need to create a config file. Put it in `articles/<name of file>.cfg`.
- Example :
<pre><code>
-
name="Hello World"
-
desc="Welcome to my blog!"
-
date="9th December 2021"
-
</code></pre>
And run the following:
- $ chmod +x main.sh
- $ ./main.sh
+ $ chmod +x seen.sh
+ $ ./seen.sh
+
+Seen supports options too, for more info, run the following:
+
+ $ ./seen.sh --help
+
+
diff --git a/main.sh b/main.sh
deleted file mode 100755
index b46e9cc..0000000
--- a/main.sh
+++ /dev/null
@@ -1,83 +0,0 @@
-#!/bin/sh
-
-# Seen, a simple static blog generator
-#
-# Vitali64 <vitali64pmemail@protonmail.com
-
-# START
-
-usage() {
- echo "Usage : ./seen [OPTIONS]"
- echo
- echo "--clear : Remove generated blog"
- echo "--rm : Remove everything and start from scratch"
-}
-
-echo -e "Seen, a simple static blog generator\n Licensed under the GNU GPLv3"
-
-if [ $1 = "--clear" ]; then
- echo "-> [$(date +%R)] Deleting generated blog ..."
- rm -rf "www/*"
- exit
-elif [ $1 = "--rm" ]; then
- echo "-> [$(date +%R)] Deleting all data ..."
- rm -rf "www/*" "articles/*"
- exit
-elif [ $1 = "" ]; then
- echo "-> [$(date +%R)] No options specified, generating blog ..."
-else
- echo "-> [$(date +%R)] Error : Invalid option, cannot continue."
- usage
- exit 1
-fi
-
-# Erase www/index.html and insert the header
-
-echo "-> [$(date +%R)] Inserting the header ..."
-
-cat "templates/header.html" > www/index.html
-
-# Detect articles
-
-echo "-> [$(date +%R)] Detecting articles' markdown file ..."
-articles="$(ls -1 articles/*.md|sed -e 's@.md@@g' -e 's@articles/@@g')"
-
-# 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" # Override the defaults
- mkdir "www/articles" 2&>/dev/null
- cat "templates/header.html" > "www/articles/${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" # Convert the markdown text to html
-
- echo "-> [$(date +%R)] Adding ${line} article to index.html ..."
- cat "templates/footer.html" >> "www/articles/${line}.html" # Insert the footer into the article page
-
- # Add an entry in 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"
-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 ..."
diff --git a/seen.sh b/seen.sh
new file mode 100755
index 0000000..7ae95a0
--- /dev/null
+++ b/seen.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+# Seen, a simple static blog generator
+#
+# Vitali64 <vitali64pmemail@protonmail.com
+
+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 " Licensed under the GNU GPLv3"
+
+if [ "$1" = "--clear" ]; then
+ echo "-> [$(date +%R)] Deleting generated blog ..."
+ rm -rf "www/*" || die "Cannot delete generated blog!"
+ exit
+elif [ "$1" = "--rm" ]; then
+ echo "-> [$(date +%R)] Deleting all data ..."
+ rm -rf "www/*" "articles/*" || die "Cannot delete www/* and articles/*!"
+ exit
+elif [ "$1" = "--help" ]; then
+ usage
+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" || 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
+
+ # 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 ..."
diff --git a/www/articles/articletext.html b/www/articles/articletext.html
new file mode 100644
index 0000000..cd84fa3
--- /dev/null
+++ b/www/articles/articletext.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+
+<html>
+
+ <body>
+ <h1>Blog</h1>
+ <div>
+
+<h1>Hello</h1>
+
+<p>Welcome to <code>seen</code> !</p>
+
+<p>It&rsquo;s a simple blog generator.</p>
+
+<p>Please refer to the README.md file for more information.</p>
+
+ </div>
+
+ <footer>Generated by `seen`, a static blog generator.</footer>
+
+ </main>
+
+ </body>
+ <style>
+div
+{
+ background-color: #ddd;
+ padding: 4pt;
+}
+ </style>
+
+</html>
diff --git a/www/index.html b/www/index.html
new file mode 100644
index 0000000..c7f33dd
--- /dev/null
+++ b/www/index.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+
+<html>
+
+ <body>
+ <h1>Blog</h1>
+ <div>
+
+ <div class="article">
+ <a href="articles/articletext.html"><h3>Hello World!</h3>9th December 2021</a>
+ <p>Welcome to seen! This is a test article.</p>
+ </div>
+
+
+ </div>
+
+ <footer>Generated by `seen`, a static blog generator.</footer>
+
+ </main>
+
+ </body>
+ <style>
+div
+{
+ background-color: #ddd;
+ padding: 4pt;
+}
+ </style>
+
+</html>