#!/bin/sh
# This is a UNIX shell script to convert (most of) the html documents on
# <http://www.usenet.org.uk> into plain text (stdin into stdout). Note that the
# various documents (notably newsgroup charters) have been carefully formatted
# for use with this script, which is not suitable for conversion of HTML
# documents in general.

nawk    'BEGIN { PRE = 0; JOIN = 0; IGN = 0; FS = "\n"; OFS = ""; ORS = "" } \
	/^<\/pre>/ { PRE = 0; JOIN = 0 } \
	PRE==1 && IGN==0 { print $1, "PPP\n" } \
	PRE!=1 && IGN==0 && JOIN==0 { print "####" } \
	PRE!=1 && IGN==0 { JOIN = 1; print $1 } \
	PRE!=1 && IGN==0 && !(/^(<[^!][^<>]*>)+$/ || /<[^>]*$/) \
		{ JOIN = 0; print "\n" } \
	/^<pre>/ { PRE = 1 } \
	/^<style/ { IGN = 1 } \
	/^<\/style>/ { IGN = 0 } \
	END { print "\n" } ' \
	$1 | sed \
	-e '/^####.*<title>/d' \
	-e '\?^####.*|INDEX|</a>?d' \
	-e '\?^####.*HOME PAGE|</a>?d' \
	-e '/^####/s/<!---->[^<>]*<!---->//g' \
	-e '/^####/s/<!--\([^>]*\)-->/\1/g' \
	-e '/^####/s/<[^<>]*>//g' \
	-e '/^####.*&/s/&lt;/</g' \
	-e '/^####.*&/s/&gt;/>/g' \
	-e '/^####.*&/s/&quot;/"/g' \
	-e '/^####.*&/s/&amp;/&/g' \
	-e '/^####.*&/s/&nbsp;/ /g' \
	-e '/^####/s/^####//' \
	-e '/PPP$/s/PPP//'
