shite

rc static blog generator
git clone git://git.2f30.org/shite
Log | Files | Refs | README

shite (6424B)


      1 #!/bin/rc
      2 #
      3 #the shite database file
      4 sdb=postdb
      5 
      6 #if any of the base strings are not empty it will generate output
      7 #of that type in the publish/ subdirectory
      8 HTML_BASE=https://baseurl
      9 GEMINI_BASE=gemini://baseurl
     10 
     11 #a title that will be used in the html/gmi headers
     12 BLOG_TITLE=someblog
     13 
     14 #we start by setting URL with correct BASEs
     15 homeURLhtml=$HTML_BASE/home.html
     16 postsURLhtml=$HTML_BASE/posts.html
     17 tagsURLhtml=$HTML_BASE/tags.html
     18 homeURLgem=$GEMINI_BASE/home.gmi
     19 postsURLgem=$GEMINI_BASE/posts.gmi
     20 tagsURLgem=$GEMINI_BASE/tags.gmi
     21 postURLbase=post/
     22 
     23 #takes two parameters (date/tag and id/content) and then creates
     24 #two files. one is named as the first parameter with a final s
     25 #and the other has the association of that parameter with
     26 #the second parameter type.
     27 #There is a final parameter that if it is equal to "uniq"
     28 #it only keeps distinct vals from the first column.
     29 fn join {
     30 	#here we will split es on space normally (tags and ids are separated by one)
     31 	if (~ $3 uniq) es=`{ndb/query -a -f $sdb type post $1 | sort | uniq}
     32 	if not {
     33 		#here we are splitting on newline (literally as i discovered)
     34 		#since titles can contain spaces.
     35 		es=`'
     36 '{ndb/query -a -f $sdb type post $1}
     37 	}
     38 	cat /dev/null > $1^s
     39 	cat /dev/null > $1^_^$2
     40 	for (e in $es) {
     41 		echo $e >> $1^s
     42 		joined =`{ndb/query -a -f $sdb $1 $e $2}
     43 		echo $e $joined >> $1^_^$2
     44 	}
     45 }
     46 
     47 fn GemHeader {
     48 	echo '# '$BLOG_TITLE'->'$1
     49 }
     50 
     51 fn GemFooter {
     52 	echo '- - - - - - - - -'
     53 	if (! ~ HTML_BASE '') {
     54 		echo 'This content is also hosted over '
     55 		echo '=> '$HTML_BASE' http'
     56 	}
     57 }
     58 
     59 fn GemMenu {
     60 	echo '## navigate'
     61 	echo '=> '$homeURLgem' home'
     62 	echo '=> '$postsURLgem' posts'
     63 	echo '=> '$tagsURLgem' tags'
     64 }
     65 
     66 fn HTMLHeader {
     67 	echo '<html>'
     68 	echo '<head> <link rel="stylesheet" type="text/css" href="'$HTML_BASE'/css/dsp.css"></head>'
     69 	echo '<title>'$BLOG_TITLE'->'$1'</title>'
     70 	echo '<body>'
     71 }
     72 
     73 fn HTMLMenu {
     74 	echo '<div id="navbar">'
     75 	echo '<a href="'^$homeURLhtml^'">home</a> | <a href="'^$postsURLhtml^'">posts</a> | <a href="'^$tagsURLhtml^'">tags</a><br>'
     76 	echo '- - - - - - - - -<br>'
     77 	echo '</div>'
     78 }
     79 
     80 fn HTMLFooter {
     81 	echo '- - - - - - - - -<br>'
     82 	if (! ~ GEMINI_BASE '') {
     83 		echo 'This content is also hosted over <a href="'$GEMINI_BASE'"> gemini </a><br>'
     84 	}
     85 	echo '</body>'
     86 	echo '</html>'
     87 }
     88 
     89 
     90 fn mkgenpages {
     91 	es=`'
     92 '{ndb/query -a -f $sdb type page}
     93 	for (e in $es) {
     94 		title=`{echo $e | sed 's/.*title=//' | sed 's/ .*//g'}
     95 		content=`{echo $e | sed 's/.*content=//' | sed 's/ .*//g'}
     96 		if (! ~ $HTML_BASE '') {
     97 			out='publish/html/'$title'.html'
     98 			HTMLHeader $"title > $out
     99 			HTMLMenu >> $out
    100 			echo '<pre>' >> $out
    101 			cat $content >> $out
    102 			echo '</pre>' >> $out
    103 			HTMLFooter >> $out
    104 		}
    105 		if (! ~ $GEMINI_BASE '') {
    106 			out='publish/gmi/'$title'.gmi'
    107 			GemHeader $"title > $out
    108 			GemMenu >> $out
    109 			cat $content >> $out
    110 			GemFooter >> $out
    111 		}
    112 	}
    113 }
    114 
    115 fn mktaglist {
    116 	if (! ~ $HTML_BASE '') {
    117 		out=publish/html/tags.html
    118 		HTMLHeader tags > $out
    119 		HTMLMenu >> $out
    120 		echo '<h1> Tag Listing </h1>' >> $out
    121 		tagid=`'
    122 	'{cat tag_id}
    123 		for (t in $tagid) {
    124 			tag=`{echo $t | sed 's/ .*//'}
    125 			ids=`{echo $t | sed 's/^[a-zA-Z0-9]+ //'}
    126 			echo '<h2> '^$tag^' </h2>' >> $out
    127 			echo '<ul>' >> $out
    128 			for (id in $ids) {
    129 				title=`{grep '^'$id id_title|sed 's/^[0-9] //'}
    130 				datestr=`{ndb/query -a -f $sdb id $id date}
    131 				echo '<li><a href="'$HTML_BASE^'/'^$postURLbase^$id'.html">['$id':'$datestr':'$"title']</a></li>' >> $out
    132 				echo >> $out
    133 			}
    134 			echo '</ul>' >> $out
    135 		}
    136 		HTMLFooter >> $out
    137 	}
    138 	if (! ~ $GEMINI_BASE '') {
    139 		out=publish/gmi/tags.gmi
    140 		GemHeader tags > $out
    141 		GemMenu >> $out
    142 		echo '# Tag Listing ' >> $out
    143 		tagid=`'
    144 	'{cat tag_id}
    145 		for (t in $tagid) {
    146 			tag=`{echo $t | sed 's/ .*//'}
    147 			ids=`{echo $t | sed 's/^[a-zA-Z0-9]+ //'}
    148 			echo '## '$tag >> $out
    149 			for (id in $ids) {
    150 				title=`{grep '^'$id id_title|sed 's/^[0-9] //'}
    151 				datestr=`{ndb/query -a -f $sdb id $id date}
    152 				echo '=>'$GEMINI_BASE^'/'^$postURLbase^$id'.gmi ['$id':'$datestr':'$"title']' >> $out
    153 			}
    154 		}
    155 		GemFooter >> $out
    156 	}
    157 	
    158 }
    159 	
    160 fn mkpostlist {
    161 	if (! ~ $HTML_BASE '') {
    162 		out=publish/html/posts.html
    163 		HTMLHeader posts > $out
    164 		HTMLMenu >> $out
    165 		echo '<h1> Post Listing </h1>' >> $out
    166 		echo '<div id="postlist">' >> $out
    167 		echo '<ol>' >> $out
    168 		idtitle=`'
    169 '{cat id_title}
    170 		for (i in $idtitle) {
    171 			id=`{echo $i | sed 's/ .*//'}
    172 			title=`{echo $i | sed 's/^[0-9] //'}
    173 			datestr=`{ndb/query -a -f $sdb id $id date}
    174 			echo '<li><a href='^$HTML_BASE^'/'^$postURLbase^$id'.html>['$id':'$datestr'] '$"title'</a></li>' >> $out
    175 		}	
    176 		echo '</ol>' >> $out
    177 		echo '</div>' >> $out
    178 		HTMLFooter >> $out
    179 	}
    180 	if (! ~ $GEMINI_BASE '') {
    181 		out=publish/gmi/posts.gmi
    182 		GemHeader posts > $out
    183 		GemMenu >> $out
    184 		echo '# Post Listing' >> $out
    185 		idtitle=`'
    186 '{cat id_title}
    187 		for (i in $idtitle) {
    188 			id=`{echo $i | sed 's/ .*//'}
    189 			title=`{echo $i | sed 's/^[0-9] //'}
    190 			datestr=`{ndb/query -a -f $sdb id $id date}
    191 			echo '=>'^$GEMINI_BASE^'/'^$postURLbase^$id'.gmi ['$id':'$datestr'] '$"title >> $out
    192 		}
    193 		GemFooter >> $out	
    194 	}	
    195 }
    196 
    197 #expects a post content formatted line like
    198 #number path
    199 fn mkpost {
    200 	id=$1
    201 	title=$2
    202 	content=$3
    203 	if (! ~ $HTML_BASE '') {
    204 		out=publish/html/post/$id.html
    205 		HTMLHeader $"title > $out
    206 		HTMLMenu >> $out
    207 		echo '<h1>'^$"title^'</h1>' >> $out
    208 		echo '<div type=''post''>' >> $out
    209 		echo '<pre>' >> $out
    210 		cat $content >> $out
    211 		echo '</pre>' >> $out
    212 		echo '</div>' >> $out
    213 		HTMLFooter >> $out
    214 	}
    215 	if (! ~ $GEMINI_BASE '') {
    216 		out=publish/gmi/post/$id.gmi
    217 		GemHeader $"title > $out
    218 		GemMenu >> $out
    219 		echo '# '^$"title >> $out
    220 		cat $content >> $out
    221 		GemFooter >> $out
    222 	}	
    223 }
    224 
    225 #mkallposts extracts the title content and id and passes
    226 #them to mkpost
    227 fn mkallposts {
    228 	if (! test -d publish/html)
    229 		mkdir -p publish/html/post
    230 	postcontent=`'
    231 '{cat id_content}
    232 	for (p in $postcontent) {
    233 		id=`{echo $p | sed 's/ .*//'}
    234 		title=`{grep '^'$id id_title | sed 's/^[0-9] //'}
    235 		content=`{echo $p | sed 's/^[0-9] //'}
    236 		mkpost $id $"title $content
    237 	}
    238 }
    239 
    240 if (~ $1 'mk') {
    241 	#this will create tags and tag_id
    242 	join tag id uniq
    243 	#get all titles and contents
    244 	join title content
    245 	#get id and title
    246 	join id title
    247 	#id with content
    248 	join id content
    249 	mkallposts
    250 	mkpostlist
    251 	mktaglist
    252 	mkgenpages
    253 }
    254 if (~ $1 'init') {
    255 	touch postdb
    256 	mkdir -p content
    257 	mkdir -p publish/html/^$postURLbase
    258 	mkdir -p publish/gmi/^$postURLbase
    259 }
    260 if (~ $1 'clean') {
    261 	rm -r publish
    262 	rm ids titles id_content id_title tag_id tags title_content
    263 }