#!/bin/sh

## This shell-script aims to make it easy to generate your HTML webpage with your specified info.

## MINOR-FUNCTIONS are declared here:-
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intro() {
echo ""
echo "========  =======  =====  |====|  =====  ========  ======="
echo "||	  |        |   |  |    |  |   |  ||        |"
echo "||  ____  |----|   |   |  |====|  |---|  ||  ____  |----|"
echo "||  |  |  |        |   |  |       |   |  ||  |  |  |"
echo "||_____|  =======  |   |  |       |   |  ||_____|  |======"
echo "__________________________________________________________"
echo "VERSION: 1.0"
echo "__________________________________________________________"
echo ""
}

store_id() {
read -p "Type your prefered (or not) name for the HTML file: " FILE
}

## VARIABLES are initialized here:-
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

## Generate your HTML Templates [Here '\n' means 'New-Line or Line-Break', whereas '\t' mean 'Tab or Indentation']
HTML_TMPLT=$(cat << '_EOF_'
<!DOCTYPE html>\n
<html lang="en">\n
<head>\n
	\t<title>@TITLE</title>\n
	\t<link rel="stylesheet" href="style.css" />\n
	\t<meta name="description" content="@DESCRIBE" >\n
	\t<meta charset="utf8">\n
	\t<meta name="author" content="@AUTHOR" >\n
	\t<meta name="viewport" content="width=device-width, initial-scale=1.0" >\n
	\t<style>\n
		\t\tb {\n
			\t\t\tcolor: yellow;\n
		\t\t}\n
		\t\ti {\n
			\t\t\tcolor: orange;\n
		\t\t}\n
		\t\tu {\n
			\t\t\tcolor: red;\n
		\t\t}\n
	\t</style>\n
</head>\n
<body>\n
<main>\n
	\t<header>\n
		\t\t<h1>@TITLE</h1>\n
		\t\t<hr>\n
	\t</header>\n
<article>\n
WRITE YOUR ARTICLE HERE, WHERE <b>THIS IS A BOLD-TEXT</b> AND THIS IS <i>AN ITALIC-TEXT</i> AND THIS IS <u>AN UNDERLINED-TEXT</u> !!\n
</article>\n
	\t<footer>\n
		\t\t<hr>\n
		\t\t<h6>THANK YOU FOR VISITING</h6>\n
	\t</footer>\n
</main>\n
</body>\n
</html>

_EOF_
)

## Generate your RSS Templates [Here '\n' means 'New-Line or Line-Break', whereas '\t' mean 'Tab or Indentation']
RSS_TMPLT=$(cat << '_EOF_'
<?xml version="1.0" encoding="UTF-8"?>\n
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">\n
<channel>\n
	\t<title>@WEBNAME@</title>\n
	\t<link>@WEBLINK@</link>\n
	\t<description>@WEBDESC@</description>\n
	\t<atom:link href="@RSSLINK@" rel="self" type="application/rss+xml" />\n
	\t<!-- The following RSS-FEEDS are arranged in a 'NEWEST-FIRST' Order -->\n
	\n
	\t<item>\n
		\t\t<title>@TITLE@</title>\n
		\t\t<link>@LINK@</link>\n
		\t\t<description>@DESC@</description>\n
		\t\t<pubDate>@TIMESTAMP@</pubDate>\n
		\t\t<guid>@LINK@</guid>\n
		\t\t<category>@CAT@</category>\n
	\t</item>\n
</channel>\n
</rss>

_EOF_
)

## MAIN-FUNCTIONS are declared here:-
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
replace_html() {
## Replacing the placeholders with your inputs
read -p "Please state your title = " TITLE
sed -i "s/@TITLE/$TITLE/g" $FILE.html
echo ""

read -p "Please state the description = " DESCRIPTION
sed -i "s/@DESCRIBE/$DESCRIPTION/" $FILE.html
echo ""

read -p "Please tell the name of the author = " AUTHORIZED
sed -i "s/@AUTHOR/$AUTHORIZED/" $FILE.html
echo ""

echo ">>> HTML-File called '$FILE.html' has been generated"
}

replace_rss() {
## Replacing the placeholders in RSS-file with your inputs
read -p "Please state the name of your website = " WEBSITE
sed -i "s/@WEBNAME@/$WEBSITE/" rss-feeds.xml
echo ""

read -p "Type the URL/Address of your Website = " URL
sed -i -e "s|@WEBLINK@|$URL|" rss-feeds.xml
echo ""

read -p "Please state the description of your website's RSS-feed = " DESCRIBE
sed -i "s/@WEBDESC@/$DESCRIBE/" rss-feeds.xml
echo ""

read -p "State the 'File-Location' where you will place the RSS-file in your website in the form of a URL (the DEFAULT NAME of the RSS-File is 'rss-feeds.xml') = " ATOM
sed -i -e "s|@RSSLINK@|$ATOM|" rss-feeds.xml
echo ""


## Replacing the placeholders in the "<item>" portion with your inputs
read -p "State the name of your article = " TOPIC
sed -i "s/@TITLE@/$TOPIC/" rss-feeds.xml
echo ""

read -p "Type the full URL/Address of your Article = " LINKED
sed -i -e "s|@LINK@|$LINKED|g" rss-feeds.xml
echo ""

read -p "Please give a short description of your article = " DETAIL
sed -i "s/@DESC@/$DETAIL/" rss-feeds.xml
echo ""

read -p "What is category of your Article = " CATEGORIZE
sed -i "s/@CAT@/$CATEGORIZE/" rss-feeds.xml
echo ""

echo "Now Generating a TimeStamp of '$(date -R)' in your RSS-Feed"
sed -i "s/@TIMESTAMP@/$(date -R)/" rss-feeds.xml
echo ""

echo ">>> RSS-File called 'rss-feeds.xml' has been generated"	
}

gen_html() {
## Generating HTML file with a set template
echo "Do you wish to generate a template HTML file to write something ?? (Perhaps an article ?)"
echo "_________________________________________________________________________________________"
while true; do
	read -p "Simply answer 'y' for YES or 'n' for NO: " QUERY1
	echo ""
	case $QUERY1 in
		[yY]*) store_id						## LOCATION== line-number 20
			   echo $HTML_TMPLT > $FILE.html
			   replace_html					## LOCATION== line-number 110
			   # ANY ADDITIONAL FUNCTIONALITIES
			   break
			   ;;
		[nN]*) echo "SKIPPING PROCESS, have a nice day"
			   break
			   ;;
		*) echo "ERROR, Please answer 'y' for YES or 'n' for NO";;
	esac	
done
}

rss_feedgen() {
## Generating a tailored RSS-Feed that'll be populated in the RSS-File with your inputs
read -p "State the NAME of your article = " NAME
sed -i "s/TITLE/$NAME/" rss-feeds.xml
echo ""

read -p "Type the full URL/Address of your Article = " URL
sed -i "s|LINKS|$URL|g" rss-feeds.xml
echo ""

read -p "Please give a short DESCRIPTION of your article = " RUNDOWN
sed -i "s/DESCRIPTION/$RUNDOWN/" rss-feeds.xml
echo ""

read -p "What is CATEGORY of your Article = " GROUP
sed -i "s/CATEGORY/$GROUP/" rss-feeds.xml
echo ""

echo "Now Generating a TIMESTAMP of '$(date -R)' in your RSS-Feed"
sed -i "s/TIMESTAMPS/$(date -R)/" rss-feeds.xml
echo ""

echo "___________________________________________________________________"
echo ">>> An RSS-Feed for your article has been generated in the RSS-File"
echo "___________________________________________________________________"
}


gen_rss() {
## Generating RSS file with a set template
echo "_______________________________________________________________"
echo "Here are some options for creating RSS feeds for your article ?"
echo "_______________________________________________________________"
echo "1 --> Generate an RSS-file with the new content (IF YOU DO NOT HAVE AN RSS-FILE)"
echo "2 --> Generate a FEED for your article which is copied into the RSS-File"
echo "ANY OTHER NUMBER --> Do not generate RSS-Files or Feeds"
echo "_______________________________________________________________"
read -p "Simply select via typing the number and ENTER: " QUERY2
echo ""
case $QUERY2 in
	1) echo $RSS_TMPLT > rss-feeds.xml
	   replace_rss							## LOCATION== line-number 127
	   # ANY ADDITIONAL FUNCTIONALITIES
	   ;;
	2) ## Using SED to append after a specific line number with '\t' meaning new-tabs 
	   sed -i '8a\
	   \t<item>\
	   \t\t<title>TITLE<\/title>\
	   \t\t<link>LINKS<\/link>\
	   \t\t<description>DESCRIPTION<\/description>\
	   \t\t<pubDate>TIMESTAMPS<\/pubDate>\
	   \t\t<guid>LINKS<\/guid>\
	   \t\t<category>CATEGORY<\/category>\
	   \t<\/item>' rss-feeds.xml   
	   
	   rss_feedgen							## LOCATION== line-number 170
	   # ANY ADDITIONAL FUNCTIONALITIES
	   ;;
	*) echo "ABORTING PROCESS, have a nice day";;
esac	
}

mdcleanup() {
## This is the part where I delete as many peculiar HTML elements in order to clean up MARKDOWN file as much as possible with room for upgrades
HEADING=$(grep -oP '(?<=<h1>).*?(?=<\/h1>)' $FILE.html)
sed -i '/<!DOCTYPE\ html>/,/<\/header>/d' $FILE.md

sed -i "1i\
# $HEADING" $FILE.md   

sed -i '/<footer>/,/<\/html>/d' $FILE.md
		   
## This is the part where I convert the most common HTML-elements as much as possible with room for upgrades
sed -i 's/<br>//g' $FILE.md
sed -i 's/<article>//g; s/<\/article//g' $FILE.md
sed -i 's/<p>//g; s/<\/p>//g' $FILE.md
sed -i 's/<hr>/---/g' $FILE.md
sed -i 's/<b>/**/g; s/<\/b>/**/g' $FILE.md
sed -i 's/<i>/*/g; s/<\/i>/*/g' $FILE.md
sed -i 's/<u>/*/g; s/<\/u>/*/g' $FILE.md
sed -i -E 's/<a\s+href="([^"]+)">([^<]+)<\/a>/[\2](\1)/g' $FILE.md	# Converts HTML links to Markdown links
		   
## This is the part where I convert the headings as much as possible with room for upgrades
sed -i -E 's/<h2>([^<]+)<\/h2>/## \1/g' $FILE.md
sed -i -E 's/<h3>([^<]+)<\/h3>/### \1/g' $FILE.md
sed -i -E 's/<h4>([^<]+)<\/h4>/#### \1/g' $FILE.md
sed -i -E 's/<h5>([^<]+)<\/h5>/##### \1/g' $FILE.md
sed -i -E 's/<h6>([^<]+)<\/h6>/###### \1/g' $FILE.md
}

md_cnvrt() {
## Convert HTML article into it's MARKDOWN version
echo ""
echo "______________________________________________________________________________"
echo "Do you want to CONVERT your HTML-File into a MARKDOWN-File for your article ??"
echo "______________________________________________________________________________"
read -p "(Type "y" for "YES" or "n" for "NO"): " SELECT

case $SELECT in
	[yY]*) echo "The Markdown file name will be the same as the HTML one"
		   store_id							## LOCATION== line-number 20
		   echo "__________________________________________________________________________________________________"
		   echo "Converting HTML elements into their markdown versions (As much as possible, within my limitations)"
		   sleep 5s
		   echo ""
		   echo "Conversion finished (As much as possible, within my limitations), now please check the output and make your final adjustments MANUALLY"
		   cat "$FILE.html" > "$FILE.md"

		   mdcleanup							## LOCATION== line-number 241
		   ;;
	[nN]*) echo "--> You are probably just beginning to write your HTML-Article.";;
	*) echo "INVALID RESPONSE";;
esac	
}

## The CORE/MAIN OF THE SCRIPT lies here:-
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clear
intro
gen_html
gen_rss
md_cnvrt
