Plan 9 from Bell Labs’s /usr/web/sources/contrib/fst/piggy.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#! /bin/rc -x

rfork n

version = '.0003'
xpix = 100

fn mkthumb{
	jpg -tc $1 | resample -x $xpix | tojpg > $2
}

fn mkheader{
	echo '<html>'
	echo '  <!-- generated by piggy version '^$version^' -->'
	echo '	<head>'
	echo '		<title>'^$1^'</title>'
	echo '	</head>'
	echo '	<body>'
	echo '	<table border="0" cellpadding="0" cellspacing="0" width="100%" height="700">'
	echo '	  <tr>'
	echo '	    <td width="100%" height="100">'
	echo '	    <p align="center">'^$2^'</p></td>'
	echo '	  </tr>'

	echo '    <tr>'
	echo '      <td width="100%" height="100">'
	echo '       <center>'
	for (a in `{seq $pages}) {
		if (! ~ $a 1) {
			echo '|'
		}

		if (~ $3 $a) {
			echo '<b>Page '^$a^'</b>'
		}
		if not {
			echo '<a href="page_'^$a^'.html">'
			echo 'Page '^$a
			echo '</a>'
		}
	}
	echo '       </center>'
	echo '      </td>'
	echo '    </tr>'

	echo '	  <tr>'
	echo '	    <td width="100%" height="600">'
	echo '	    <table border="0" cellpadding="0" cellspacing="0" width="100%" height="600">'
}

fn mkrow{
	echo '     <tr>'
	for (p in `{seq $1 $2}) {
		echo '	<td width="33%" height="150">'
		echo '		<center><a href="' ^ $jpegs($p) ^ '" >'
		echo '			<img border="1" src="' ^ $thumbs($p) ^ '" width="100" height="75" >'
		echo '		</a></center>'
		echo '	</td>'
	}
	echo '      </tr>'
}

fn mkfooter{
	echo '    </table>'
	echo '    </td>'
	echo '  </tr>'
	echo '  </table>'
	echo ' </body>'
	echo '</html>'
}

fn mkindex {
	echo '<html>'
	echo ' <!-- generated by piggy version '^$version^' -->'
	echo ' <head>'
	echo '  <title>'^$1^'</title>'
	echo ' </head>'
	echo ' <body>'
	echo '  <table border="0" cellpadding="0" cellspacing="0" width="100%" height="300">'
	echo '   <tr>'
	echo '    <td width="100%" height="100">'
	echo '     <p align="center">'^$2^'</p>'
	echo '    </td>'
	echo '   </tr>'
	echo '   <tr>'
	echo '    <td width="100%" height="100">'
	echo '     <center>'
	for (a in `{seq $pages}) {
		if (! ~ $a 1) {
			echo '|'
		}

		echo '<a href="page_'^$a^'.html">'
		echo 'Page '^$a
		echo '</a>'
	}
	echo '     </center>'
	echo '    </td>'
	echo '   </tr>'
	echo '  </table>'
	echo ' </body>'
	echo '</html>'
}

fn mktopindex {
	echo '<html>'
	echo ' <!-- generated by piggy version '^$version^' -->'
	echo ' <head>'
	echo '  <title>'^$1^'</title>'
	echo ' </head>'
	echo ' <body>'
	echo '  <table border="0" cellpadding="0" cellspacing="0" width="100%" height="300">'
	echo '   <tr>'
	echo '    <td width="100%" height="100">'
	echo '     <p align="center">'^$2^'</p>'
	echo '    </td>'
	echo '   </tr>'
	echo '   <tr>'
	echo '    <td width="100%" height="100">'
	echo '     <center>'
	indexes=(*/index.html)
	for (a in `{seq 1 $#indexes}) {
		pagetitle=`{grep '<title>' $indexes($a) | sed 's/<title>(.*)<.title>/\1/'}
		if (! ~ $pagetitle '') {
			if (! ~ $a 1) {
				echo '|'
			}
			echo '<a href="'^$indexes($a)^'">'
			echo $pagetitle
			echo '</a>'
		}
	}
	echo '     </center>'
	echo '    </td>'
	echo '   </tr>'
	echo '  </table>'
	echo ' </body>'
	echo '</html>'
}


gtitle='Picture Gallery'
yesindex='0'
yesthumb='0'
yestopindex='0'

while(~ $1 -*) {
	switch($1){
	case -t
		yesthumb='1'
	case -i
		yesindex='1'
	case -T
		shift
		gtitle=$1
	case -g
		yestopindex='1'
	case *
	}

	shift
}

if (~ $yestopindex '1') {
	> _index.html
	mktopindex $gtitle $gtitle >> _index.html
	exit
}

jpegs=(`{ls -t *.jpg | grep -v '^piggythmb_'})
# thumbs='piggythmb_'^$jpegs
thumbs='piggythmb_'^`{seq $#jpegs}^'.jpg'
pages=`{echo $#jpegs ' 8 + 9 / p' | dc}

if (~ $yesthumb '1') {
	for (i in `{seq $#jpegs}) {
		mkthumb $jpegs($i) $thumbs($i)
	}
}

for (x in `{seq $pages}) {
	ptitle='Gallery '^$x^' of '^$pages
	outfile='page_'^$x^'.html'
	>$outfile
	mkheader $gtitle $ptitle $x >> $outfile
	range=`{echo $x ' 1 - 9 * 1 + p ' $x ' 9 * p' | dc}
	if (test $range(2) -gt $#jpegs) {
		range=($range(1) $#jpegs)
	}

	for (i in `{seq $range(1) 3 $range(2)}) {
		j = `{echo $i ' 2 + p' | dc}
		if (test $j -gt $range(2)) {
			j = $range(2)
		}

		mkrow $i  $j >> $outfile
	}
	mkfooter >> $outfile
}

if (~ $yesindex '1') {
	> _index.html
	mkindex $gtitle $gtitle >> _index.html
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.