shell-scripts

Shell scripts mentioned on the concealed.world website.
git clone git://git.concealed.world/shell-scripts
Log | Files | Refs | README | LICENSE

mkarticle (6358B)


      1 #!/bin/bash
      2 # Use simple formatting to write my articles for me.
      3 # DEPENDS: Bash, GNU coreutils
      4 #
      5 #    Copyright (C) <2020>  <nixx@firemail.cc>
      6 #
      7 #    This program is free software: you can redistribute it and/or modify
      8 #    it under the terms of the GNU General Public License as published by
      9 #    the Free Software Foundation, either version 3 of the License, or
     10 #    (at your option) any later version.
     11 #
     12 #    This program is distributed in the hope that it will be useful,
     13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 #    GNU General Public License for more details.
     16 #
     17 #    You should have received a copy of the GNU General Public License
     18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     19 
     20 usage () {
     21 cat << EOF
     22 mkarticle
     23 =========
     24 
     25 Description:
     26 ------------
     27 A program for writing my HTML articles using a simpler syntax.
     28 
     29 Usage:
     30 -------
     31 OPTIONS:
     32 	mkarticle [-h]
     33 	mkarticle [--help]
     34 
     35 	-h, --help
     36 		Display this help message.
     37 
     38 IN FILE:
     39 	Placed on a new line:
     40 	- - - - - - - - - - -
     41 	@h2; - header 2
     42 	@h3; - header 3
     43 	
     44 	@p; - paragraph
     45 	@q; - quote
     46 	
     47 	@us; - start list*
     48 	@ue; - end list*
     49 	@l; - list item
     50 	
     51 	@ic <link> "<alt>"; - image center*
     52 	@il <link> "<alt>"; - image left*
     53 	@ir <link> "<alt>"; - image right*
     54 	
     55 	@cs; - codeblock start*
     56 	@ce; - codeblock end*
     57 
     58 	@hr; - line*
     59 	
     60 	Placed in-line:
     61 	- - - - - - - - - - -
     62 	@ts; - italic start
     63 	@te; - italic end
     64 	@bs; - bold start
     65 	@be; - bold end
     66 	
     67 	@a##s <link>; - start link
     68 		- @a##s may contain:
     69 			- 't' - target='_blank'
     70 			- 'x' - class='extlink'
     71 	@ae; - end link
     72 
     73 	- - - - - - - - - - -
     74 
     75 	* - No text follows this element after the ';' on the same line.
     76 EOF
     77 }
     78 
     79 invalid_string () {
     80 	echo -e "\e[0;91mInvalid Argument. See usage:\e[0m\n"
     81 }
     82 
     83 filename () {
     84 	filestring=$(echo "${1}" | tr '[:upper:]' '[:lower:]' | sed "s/ //g; s/,//g; s/'//g")
     85 	filestring="${filestring}.php"
     86 }
     87 
     88 edit () {
     89 	[[ -z $EDITOR ]] && EDITOR=vim
     90 	$EDITOR $1
     91 }
     92 
     93 handlelink () {
     94 	link=$(echo "${1}" | sed 's|\/|\\/|g')
     95 }
     96 
     97 handlealt () {
     98 	[[ $1 == *'"'* ]] && alt="$(echo "${1}" | cut -d \" -f 2 | cut -d \" -f 1)" || alt="$(echo "${1}" | cut -d \  -f 3 | cut -d \; -f 1)"
     99 }
    100 
    101 format () {
    102 	length=$(wc -l $1 | cut -d \  -f 1)
    103 	count=1
    104 
    105 	while [[ $count -le $length ]]; do
    106 		line=$(cat "${1}" | head -n $count | tail -n 1 | sed 's/^\ *//g; s/\ *$//g')
    107 
    108 		## NEWLINE ELEMENTS
    109 		if [[ $line == '@h2;'* ]]; then
    110 			line="$(echo "${line}" | sed 's/@h2\;/<h2>/g')</h2>"
    111 		elif [[ $line == '@h3;'* ]]; then
    112 			line="$(echo "${line}" | sed 's/@h3\;/<h3>/g')</h3>"
    113 		elif [[ $line == '@p;'* ]]; then
    114 			line="$(echo "${line}" | sed 's/@p\;/<p>/g')</p>"
    115 		elif [[ $line == '@q;'* ]]; then
    116 			line="$(echo "${line}" | sed "s/@q\;/<p class='quote'>/g")</p>"
    117 		elif [[ $line == '@us;' ]]; then
    118 			line="$(echo "${line}" | sed 's/@us\;/<ul>/g')"
    119 		elif [[ $line == '@ue;' ]]; then
    120 			line="$(echo "${line}" | sed 's/@ue\;/<\/ul>/g')"
    121 		elif [[ $line == '@l;'* ]]; then
    122 			line="$(echo "${line}" | sed "s/@l\;/<li class='asidesubitem'>/g")</li>"
    123 		elif [[ $line == '@ic '* ]]; then
    124 			link="$(echo "${line}" | cut -d \  -f 2)"
    125 			handlelink ${link}
    126 			handlealt "${line}"
    127 			line="$(echo "${line}" | sed "s/@ic /<a href='$link' target='_blank'><img alt='${alt}' class='centerimg' src='$link' \/><\/a>;/g" | cut -d \; -f 1)"
    128 		elif [[ $line == '@il '* ]]; then
    129 			link="$(echo "${line}" | cut -d \  -f 2)"
    130 			handlelink ${link}
    131 			handlealt "${line}"
    132 			line="$(echo "${line}" | sed "s/@il /<a href='$link' target='_blank'><img alt='${alt}' class='leftimg' src='$link' \/><\/a>;/g" | cut -d \; -f 1)"
    133 		elif [[ $line == '@ir '* ]]; then
    134 			link="$(echo "${line}" | cut -d \  -f 2)"
    135 			handlelink ${link}
    136 			handlealt "${line}"
    137 			line="$(echo "${line}" | sed "s/@ir /<a href='$link' target='_blank'><img alt='${alt}' class='rightimg' src='$link' \/><\/a>;/g" | cut -d \; -f 1)"
    138 		elif [[ $line == '@cs;' ]]; then
    139 			line="$(echo "${line}" | sed "s/@cs\;/<pre class='codeblock'>/g")"
    140 		elif [[ $line == '@ce;' ]]; then
    141 			line="$(echo "${line}" | sed 's/@ce\;/<\/pre>/g')"
    142 		elif [[ $line == '@hr;' ]]; then
    143 			line="$(echo "${line}" | sed 's/@hr\;/<hr \/>/g')"
    144 		fi
    145 
    146 		## INLINE ELEMENTS
    147 		[[ $line == *"@ts;"* ]] && line="$(echo "${line}" | sed 's/@ts\;/<i>/g')"
    148 		[[ $line == *"@te;"* ]] && line="$(echo "${line}" | sed 's/@te\;/<\/i>/g')"
    149 		[[ $line == *"@bs;"* ]] && line="$(echo "${line}" | sed 's/@bs\;/<b>/g')"
    150 		[[ $line == *"@be;"* ]] && line="$(echo "${line}" | sed 's/@be\;/<\/b>/g')"
    151 
    152 		if [[ $line == *"@a"* ]]; then
    153 			line="$(echo "${line}" | sed "s/@as /<a href='/g; s/@ats /<a target='_blank' href='/g; s/@axs /<a class='extlink' href='/g; s/@axts /<a class='extlink' target='_blank' href='/g; s/@atxs /<a target='_blank' class='extlink' href='/g; s/@ae\;/<\/a>/g")"
    154 			numlinks=$(echo "${line}" | grep -o "</a>" | wc -l)
    155 			linkcount=1
    156 			string=$(echo "${line}" | awk -F '<a ' '{print $1}')
    157 			newline=${string}
    158 			((linkcount++))
    159 			while [[ $linkcount -le $numlinks ]]; do 
    160 				string=$(echo "${line}" | awk -v n="$linkcount" -F '<a ' '{print $n}' | sed "s/\;/'\>/")
    161 				newline="${newline}<a ${string}"
    162 				((linkcount++))
    163 			done
    164 			string=$(echo "${line}" | awk -v n="$linkcount" -F '<a ' '{print $n}' | sed "s/\;/'\>/")
    165 			newline="${newline}<a ${string}"
    166 			line=${newline}
    167 		fi
    168 
    169 		echo "${line}" >> $filestring
    170 		((count++))
    171 	done
    172 
    173 	sed -i "1 i <?php include \$root.'includes/seemore.php'; echo '<a href=\"'.\$seemorepath.'\"><h1>${title}</h1></a>'; # Article Title ?>" $filestring
    174 	echo "<p class=\"datetext\">$(date +%Y.%m.%d)</p>" >> $filestring
    175 }
    176 
    177 main () {
    178 	proceed='n'
    179 	while [[ $proceed != 'y' ]]; do
    180 		read -p 'Title for this article?: ' title
    181 		title=$(echo $title | sed 's/"//g; s/\\//g; s/\///g; s/</\&lt/g; s/>/\&gt/g')
    182 		filename "${title}"
    183 
    184 		find $filestring >/dev/null; [[ $(echo $?) == 0 ]] && proceed='n' && read -p 'You are going to overwrite an existing article. Are you sure? (y/N): ' proceed || proceed='y'
    185 		[[ $proceed == 'yes' || $proceed == 'Yes' || $proceed == 'Y' ]] && proceed='y'
    186 		[[ $proceed == 'y' ]] && rm $filestring
    187 	done
    188 
    189 	temp=$(mktemp)
    190 	edit $temp
    191 	format $temp
    192 	rm $temp
    193 }
    194 
    195 while [[ $(echo "${1}" | cut -c1) == '-' ]]; do
    196 	case "$1" in
    197 		"-h"|"--help"	)
    198 			usage
    199 			exit 0
    200 			;;
    201 		* )
    202 			invalid_string
    203 			usage
    204 			exit 1
    205 			;;
    206 	esac
    207 done
    208 [[ $# == 0 ]] && main