<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/env perl -w

# Copyright (c) 2017, 2018, 2021 Matthew R. Green
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

$rcsid = '$eterna: help2html.pl,v 1.9 2021/03/20 21:34:52 mrg Exp $';

# convert help files into help files.
#
# we give header and footer, and convert ^_&lt;foo&gt;^_ into a hyperlink to &lt;foo&gt;.

my $usage = "help2html.pl &lt;helpdir&gt; &lt;htmloutputdir&gt;\n";

my $debug = 0;
my $total_files = 1;

my %no_usage_list = (
	':' =&gt; 1,
	'alias/functions' =&gt; 1,
	'alias/quote' =&gt; 1,
	'alias/special' =&gt; 1,
	'alias/width' =&gt; 1,
	'basics' =&gt; 1,
	'bind/examples' =&gt; 1,
	'bind/keys' =&gt; 1,
	'bye' =&gt; 1,
	'channel' =&gt; 1,
	'commands' =&gt; 1,
	'etiquette' =&gt; 1,
	'exit' =&gt; 1,
	'expressions' =&gt; 1,
	'intro' =&gt; 1,
	'ircii/copyright' =&gt; 1,
	'ircii/environment_vars' =&gt; 1,
	'ircii/programming' =&gt; 1,
	'ircii/server_lists' =&gt; 1,
	'load/edit' =&gt; 1,
	'load/lynx_ircrc' =&gt; 1,
	'load/suggestions' =&gt; 1,
	'menus' =&gt; 1,
	'news' =&gt; 1,
	'newuser' =&gt; 1,
	'on/serial_numbers' =&gt; 1,
	'part' =&gt; 1,
	'signoff' =&gt; 1,
	'time' =&gt; 1
);

sub dirname($) {
	my ($path) = @_;

	$path =~ s,/[^/]*,,;
	return $path;
}

# eval this with $title set
sub header_from_title($) {
	my ($file) = @_;
	my $title = "ircII help";

	$file =~ s,^\./,,;
	if ($file ne ".") {
		$title .= " - $file";
	}

	my $header = "
&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"&gt;
&lt;!-- This is an automatically generated file.  Do Not Edit! --&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;$title&lt;/title&gt;
  &lt;/head&gt;
  &lt;h1&gt;$title&lt;/h1&gt;
";
	$header;
}

sub footer($$) {
	my ($prefix, $file) = @_;

	my $footer .= "\n&lt;p&gt;";

	if ($prefix eq "../../") {
		$footer .= "&lt;a href=\"../../$file.html\"&gt;Up index&lt;/a&gt;\n&lt;/html&gt;\n";
	} if ($prefix eq "../") {
		$footer .= "&lt;a href=\"../$file.html\"&gt;Up index&lt;/a&gt;\n&lt;/html&gt;\n";
	}

	$footer .= "&lt;a href=\"${prefix}index.html\"&gt;Top level index&lt;/a&gt;\n&lt;/html&gt;\n";

	return $footer;
}

sub convert_one_file($$$);
sub convert_one_file($$$) {
	my ($subdir, $file, $outdir) = @_;

	my $inpath;
	if ($subdir eq ".") {
		$inpath = $file;
	} else {
		$inpath = $subdir . "/" . $file;
	}
	$inpath =~ s,^\./,,g;

	if ( ! -d $outdir ) {
		mkdir $outdir, 0755 or die "couldn't mkdir $outdir";
	}

	my $slashes = $file;
	$slashes =~ s,\./\./,/.,g;
	$slashes =~ s,^\./,,g;
	$slashes =~ s/[^\/]//g;
	my $prefix = "";
	my $length = length($slashes);
	for (my $i = 0; $i &lt; $length; $i++) {
		$prefix .= "../";
	}

	print "outdir $outdir file $file subdir $subdir -&gt; inpath $inpath\n"
		if $debug;
	if (-d $inpath) {
		my @index_list;
		my $file_dirname = "$outdir/" . dirname($file);
		my $outdir_path = "$outdir/" . $file;

		if ( ! -d $outdir_path ) {
			mkdir $outdir_path, 0755 or die "couldn't mkdir $outdir_path";
		}
		print "file_dirname $file_dirname file $file outdir_path $outdir_path\n"
			if $debug;

		opendir(my $dh, $inpath) or die "opendir: $!\n";
		while (readdir $dh) {
			next if $_ =~ /^\./;
			my $this_file = $file . "/" . $_;
			push @index_list, $_;
			convert_one_file(".", $this_file, $outdir);
		}

		# create index.html in the subdir, and file.html in the topdir
		my $header = header_from_title($file);
		my $index_path = $outdir_path . "/index.html";
		open OUTI, "&gt;$index_path" or die "couldn't open for write $index_path: $!\n"
			or die "couldn't open $index_path";
		print "creating $index_path\n"
			if $debug;
		print OUTI $header;
		for $f (@index_list) {
			print OUTI "&lt;br&gt;&lt;a href=\"$f.html\"&gt;$f&lt;\/a&gt;\n";
		}
		print OUTI footer($prefix, $file);
		close OUTI;

		if ($file ne ".") {
			my $name_path = $outdir_path . ".html";
			open OUTN, "&gt;$name_path" or die "couldn't open for write $name_path: $!\n"
				or die "couldn't open $name_path";
			print "creating $name_path\n"
				if $debug;
			print OUTN $header;
			for $f (@index_list) {
				print OUTN "&lt;br&gt;&lt;a href=\"$file/$f.html\"&gt;$f&lt;\/a&gt;\n"
			}
			print OUTN footer($prefix, $file);
			close OUTN;
		}
		return;
	}
	my $outpath = $outdir . "/" . $file . ".html";
	print "inpath $inpath outpath $outpath\n"
		if $debug;

	$total_files++;
	open IN, $inpath or die "couldn't open $inpath: $!\n";
	open OUT, "&gt;$outpath" or die "couldn't open for write $outpath: $!\n";
	print "creating $outpath\n"
		if $debug;

	my $title = $file;
	my $header = header_from_title($title);

	print OUT $header;

	# phases:
	# 1 - beginning, looking for Usage
	# 2 - got Usage, doing main doc
	# 3 - doing SEE ALSO, etc.
	# 4 - special case no "Usage:" files 
	my $phase = 1;
	# these help files don't have a Usage:
	if ($no_usage_list{$inpath}) {
		$phase = 2;
	}

	my $in_pre = 0;

	my $in;
	while ($in = &lt;IN&gt;) {
		if ($in =~ /^!/) {
			chomp $in;
			print OUT "&lt;!-- $in --&gt;\n";
			next;
		}
		$in =~ s/&amp;/\&amp;amp;/;	# first!
		$in =~ s/"/\&amp;quot;/;
		$in =~ s/'/\&amp;apos;/;
		$in =~ s/&lt;/\&amp;lt;/;
		$in =~ s/&gt;/\&amp;gt;/;
		$in =~ s/^$/&lt;p&gt;\n/;
		$in =~ s/^$/&lt;p&gt;\n/;
		$in =~ s/\002//g;
		if ($phase == 1) {
			$in =~ s/$/\n&lt;p&gt;\n/;
		} elsif ($phase == 2) {
			if ($inpath eq "alias/functions" &amp;&amp; $in =~ m/^  [A-Z]+\([^)]+\)/) {
				$phase = 4;
			}

			# display regions with 4-leading spaces as "pre".
			if ($in =~ m/^    (.*)/) {
				if (!$in_pre) {
					print OUT "&lt;pre&gt;\n";
				}
				$in_pre = 1;
			} else {
				if ($in_pre) {
					print OUT "&lt;/pre&gt;\n";
				}
				$in_pre = 0;
			}
		}
		if ($phase == 4) {
			if ($in =~ m/^  ([A-Z]+\([^)]*\))\s+(.*)/) {
				$func = $1;
				print OUT "&lt;p&gt;&lt;br&gt;$func&lt;br&gt;\n";
				$in = $2;
			} else {
				$in =~ s/^\s+/ /;
			}
		}

		# convert "^_one^_ ^_two^_" into "one/two", recursively
		# ugly do it converting "^_ ^_" into "/"
		$in =~ s/ /\//g;
		$in =~ s/([^]+?)/'&lt;a href="' . $prefix . lc($1) . '.html"&gt;' . lc($1) . '&lt;\/a&gt;'/ge;

		print OUT $in;

		$phase = 3 if $in =~ /^See Also:/;
		$phase = 2 if $in =~ /^Usage:/;
	}
	print OUT footer($prefix, dirname($inpath));
}

sub main() {
	if ($#ARGV != 1) {
		print STDERR $usage;
		exit 1;
	}
	my $helpdir = $ARGV[0];
	my $htmldir = $ARGV[1];

	print "$0 ($rcsid)\n";

	chdir($helpdir) or die "couldn't chdir $helpdir";
	convert_one_file(".", ".", $htmldir);
	
	print "$0: created $total_files files\n";
}

main();
</pre></body></html>