shite

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

shite (6603B)


      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 | sed 's/^=> ([a-zA-Z0-9\-:\/\.@?=~]+) (.*)$/<a href="\1">\2<\/a>/' >> $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>['$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 '$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 | sed 's/^=> ([a-zA-Z0-9\-:\/\.@?=~]+) (.*)$/<a href="\1">\2<\/a>/' >> $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 	if (! test -d publish/gmi)
    231 		mkdir -p publish/gmi/post
    232 	postcontent=`'
    233 '{cat id_content}
    234 	for (p in $postcontent) {
    235 		id=`{echo $p | sed 's/ .*//'}
    236 		title=`{grep '^'$id id_title | sed 's/^[0-9] //'}
    237 		content=`{echo $p | sed 's/^[0-9] //'}
    238 		mkpost $id $"title $content
    239 	}
    240 }
    241 
    242 if (~ $1 'mk') {
    243 	#this will create tags and tag_id
    244 	join tag id uniq
    245 	#get all titles and contents
    246 	join title content
    247 	#get id and title
    248 	join id title
    249 	#id with content
    250 	join id content
    251 	mkallposts
    252 	mkpostlist
    253 	mktaglist
    254 	mkgenpages
    255 }
    256 if (~ $1 'init') {
    257 	touch postdb
    258 	mkdir -p content
    259 	mkdir -p publish/html/^$postURLbase
    260 	mkdir -p publish/gmi/^$postURLbase
    261 }
    262 if (~ $1 'clean') {
    263 	rm -r publish
    264 	rm ids titles id_content id_title tag_id tags title_content
    265 }